You are given a grid of rows rows and cols columns where each cell is 1 (land) or 0 (water). An island is a maximal group of land cells connected up/down/left/right (not diagonally). The area of an island is the number of land cells it contains.
Report the area of the largest island, or 0 if there is no land.
Line 1: two integers rows and cols.
The next rows lines each contain cols integers (each 0 or 1), the grid.
A single integer: the maximum island area, or 0 if the grid has no land.
Example 1
Input
3 4 1 1 0 0 1 0 0 1 0 0 1 1
Expected
3
Explanation
The top-left island {(0,0),(0,1),(1,0)} has area 3. The bottom-right island {(1,3),(2,2),(2,3)} also has area 3. The largest area is 3.
Example 2
Input
2 2 0 0 0 0
Expected
0
Explanation
There is no land at all, so the largest island area 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 →