A watchtower keeper checks a rectangular grid of signal posts every night. Grid position (i, j) holds 1 if the post's beacon is lit and 0 if it is dark. A lit beacon is called solitary if no other beacon anywhere in its row is lit and no other beacon anywhere in its column is lit either. Given tonight's grid, count how many solitary beacons are lit.
m and n — the number of rows and columns.m lines contains n integers, each either 0 or 1, giving one row of the grid.Print a single integer: the number of solitary beacons.
Example 1
Input
3 3 1 0 0 0 0 1 1 0 0
Expected
1
Explanation
Row 0 has a single lit beacon at column 0, but column 0 also has a lit beacon in row 2, so that beacon is not solitary (its column has two lit posts, and by symmetry row 2's beacon at column 0 is disqualified too). Row 1's beacon at column 2 is the only lit post in row 1, and column 2 has no other lit post, so it counts. Total = 1.
Example 2
Input
3 3 1 0 0 0 1 0 0 0 1
Expected
3
Explanation
This is a diagonal grid: every row has exactly one lit beacon and every column has exactly one lit beacon, so all three lit posts are solitary. Total = 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →