Count connected components of land cells.
Scan grid; when you find a '1', increment answer and DFS/BFS to flood-fill and mark the entire island as water.
Each island is one connected component in 4-direction adjacency; flood-fill visits it exactly once.
Count connected components of land cells.
Scan grid; when you find a '1', increment answer and DFS/BFS to flood-fill and mark the entire island as water.
Each island is one connected component in 4-direction adjacency; flood-fill visits it exactly once.