You get a rectangular map, grid, where every cell holds the character '1' for land or '0' for water.
Land cells that touch along a side (up, down, left or right) belong to the same island. Cells that meet only at a corner do not. Treat everything beyond the border as water, and return how many separate islands the map has.
Example 1
Input: grid = [["1","1","0","0","1"],["1","0","0","1","1"],["0","0","1","0","0"]] Output: 3 Why: a block at the top left, a block at the top right, and one lone cell in the middle
2 more examples and the constraints
Example 2
Input: grid = [["1","1","1"],["0","0","1"],["1","1","1"]] Output: 1 Why: the land bends around like an S, but every step is side by side
Example 3
Input: grid = [["1","0","1"],["0","1","0"],["1","0","1"]] Output: 5 Why: the land cells touch only at corners, so each one is its own island
Constraints
- 1 ≤ rows, columns ≤ 300
- Each cell is the character
'0'or'1' - Only side-by-side cells connect; diagonal ones do not
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Recursive DFSO(m·n)
- Union-FindO(m·n·α)
- BFS flood fillO(m·n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for a 300 × 300 grid | Verdict |
|---|---|---|---|
| Recursive DFS | O(m·n) | Fast, but deep recursion can crash | |
| Union-Find | O(m·n·α) | Passes; best when land arrives over time | |
| BFS flood fill | O(m·n) | Best; no recursion to overflow |
m = rows, n = columns, α = inverse Ackermann (under 5 for any real input).
Edge cases to test
- All water. No
'1'anywhere: the answer is 0, and the flood fill never runs. - All land. One island covering the grid. This is the case that breaks a deep recursion.
- Corner contact. A checkerboard of land: every land cell is its own island, because diagonals do not connect.
- A single row or column. Bounds checks must hold when there is only one row or one column, like
["1","0","1","1"]. - Island inside a ring. A ring of land around water with land in the middle is two islands, not one.
Common mistakes
- Counting diagonal neighbours as connected.
- Comparing cells with the number 1 instead of the character
'1', so nothing ever matches. - Marking a cell visited only when it leaves the BFS queue, so the same cell is queued many times.
How to talk through it
- 1Clarify. Do diagonals count? May I change the grid, or must it stay as it was? How big can it get?
- 2Name the pattern. Each island is a connected group of land cells, so counting islands means counting flood fills.
- 3Pick the traversal. Start with recursive DFS, then say why BFS with a queue is safer on a 300 × 300 grid.
- 4Code the scan and the sink. Count when the scan meets land, then sink the island so the scan skips the rest of it.
- 5Test out loud. Run all water, all land and a checkerboard before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?