Number of Islands
Given an m x n grid of '1' (land) and '0' (water) characters, count the number of islands. An island is a maximal group of land cells connected horizontally or vertically; the whole grid is surrounded by water.
Open official problem prompt ↗Count how many distinct connected groups of land cells exist in the grid.
Imagine an aerial photo of the sea dotted with land. Drop a dye that spreads across touching land tiles; every time you must start a new drop of dye, you have found another island.
- Input
- grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
- Output
- 3
- Why
- The top-left 2x2 block, the single center cell, and the bottom-right pair are three separate connected land groups.
m == grid.lengthn == grid[i].length1 <= m, n <= 300grid[i][j] is '0' or '1'