A satellite map of an archipelago is an R-by-C binary grid where 1 is land and 0 is sea. A landmass is a maximal group of land cells connected to one another through shared edges (up, down, left, or right; diagonal touches do not connect).
Report the number of cells in the largest landmass. If there is no land at all, report 0.
Line 1: two integers R and C.
Next R lines: each contains C space-separated integers, each 0 or 1.
A single integer: the cell count of the largest 4-connected landmass, or 0 if there is none.
Example 1
Input
3 4 1 1 0 0 1 0 0 1 0 0 1 1
Expected
3
Explanation
The top-left landmass has 3 cells; the bottom-right landmass (the two bottom cells plus the one above the rightmost) also has 3 cells. The largest has 3 cells.
Example 2
Input
2 2 0 0 0 0
Expected
0
Explanation
There is no land, so the answer is 0.
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 →