Given a 2D grid of integers where 0 represents water and any nonzero value represents land, count the number of islands. An island is a maximal group of land cells connected horizontally or vertically (not diagonally), bounded by water or the grid edge.
Input
grid: 2D array of integers (0 = water, nonzero = land)
Output
The number of distinct islands.
Examples
Example 1
Inputgrid = [[0, 2, 3], [4, 5, 6], [7, 8, 9]]
Output1
Explanation: All non-zero cells are connected as one island.
Example 2
Inputgrid = [[0]]
Output0
Constraints
Cells connect only horizontally or vertically.
The grid may be modified during traversal.