Count the Islands
A map is given as a list of equal-length strings, one per row: # is land and . is water. Land cells that touch up, down, left or right belong to the same island; cells that only touch diagonally do not.
Return the number of islands.
Examples
["##...",
"##...",
"..#..",
"...##"] → 3
["#####"] → 1
["#.#",
".#.",
"#.#"] → 5In the last example, no two land cells share a side, so each one is an island of its own.
Constraints
- Up to 100 rows and 100 columns; the grid may be empty.
- Aim for O(n) in the number of cells: visit each cell a constant number of times.
Console
›Run to see what your code prints
Run checks the 3 visible cases here, in your browser. Submit runs all 7 on the judge, 4 of them hidden, and measures how your solution scales.
Expected is the reference solution's answer, from the judge. An input that breaks the statement's other rules may get any answer.