grid is a map with m rows and n columns: 1 is land and 0 is water. Land cells that touch along a side (up, down, left or right) belong to the same island; cells that meet only at a corner do not. Everything outside the grid is water.
An island's area is the number of land cells in it. Return the area of the largest island, or 0 if the grid has no land.
There are four islands, of areas 3 (top left), 3 (top right), 4 (bottom left) and 1 (bottom right). The top-right island and the bottom-left one meet only at a corner, so they stay separate rather than forming an island of 7.
The ring is one island. The water in the middle is not part of it.
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 50grid[i][j] is 0 or 1.Scan the grid. Each time you reach land you haven't visited, you've found a new island: explore all of it and count its cells.
An island's area is the number of land cells you can walk to from any one of its cells, moving up, down, left or right over land. So pick a land cell, explore outward from it with a flood fill, and count the cells you reach. That count is the island's area.
Time O(m · n)Space O(m · n)
The full walkthrough and code are part of Premium
4