Maximum Area of an Island
Problem statement
Given a rectangular binary grid, an island is a maximal group of 1 cells connected vertically or horizontally.
Return the largest island area, measured as its number of cells. Return 0 when the grid contains no land. Do not modify the input grid.
Function
maxAreaOfIsland(grid: int[][]) → intExamples
Example 1
grid = [[0,0,1,0],[1,1,1,0],[0,1,0,0]]return = 5The central island contains five connected land cells.
Example 2
grid = [[0,0],[0,0]]return = 0There is no land.
Example 3
grid = [[1,0,1],[0,1,0],[1,0,1]]return = 1Diagonal cells are not connected, so every island has area one.
Constraints
1 <= rows, columns <= 500.- Every cell is
0or1.