Number of Islands
Each unvisited land cell launches a flood fill that sinks its whole island.
Approach
Scan the grid; when you hit unvisited land, increment the count and DFS/BFS to mark every connected land cell visited (flood fill). Union-Find is an alternative that merges adjacent land cells and counts components.
Time complexity
O(rows·cols)
Space complexity
O(rows·cols) worst case
Common mistake
Not marking cells visited (or not marking them immediately), causing recounting or infinite recursion.
See it run, step by step
Generate an interactive lesson for Number of Islands — trace every variable and watch the algorithm execute until it clicks.
Start a lesson on Number of Islands