Imagine you're a cartographer analyzing satellite imagery of an archipelago! You have a 2D grid map where '1' represents land and '0' represents water. Your mission is to count how many separate islands exist in this region.

An island is formed by connecting adjacent land cells ('1's) either horizontally or vertically (diagonal connections don't count). Think of it like walking from one piece of land to another - you can only move up, down, left, or right.

Example: If you see connected land masses, they form a single island. Separate land masses surrounded by water are different islands.

The entire grid is surrounded by water, so any land on the edges is still considered part of an island if connected to other land.

Input & Output

example_1.py β€” Basic Islands
$ Input: grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
β€Ί Output: 1
πŸ’‘ Note: All the 1's are connected horizontally or vertically, forming one large island surrounded by water.
example_2.py β€” Multiple Islands
$ Input: grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
β€Ί Output: 3
πŸ’‘ Note: There are three separate islands: one 2x2 island in top-left, one single cell in middle, and one 1x2 island in bottom-right.
example_3.py β€” Single Cell
$ Input: grid = [["1"]]
β€Ί Output: 1
πŸ’‘ Note: Edge case with just one cell containing land forms exactly one island.

Visualization

Tap to expand
🏝️ Island Discovery ExpeditionIsland 1Island 2Island 3ExplorerDrone exploringResult: 3 separate islands discovered!
Understanding the Visualization
1
Systematic Scan
Scan the map methodically from top-left to bottom-right looking for undiscovered land
2
Island Discovery
When you spot land ('1'), this could be a new island! Increment your island counter
3
Complete Exploration
Deploy your drone to explore every connected piece of this island using DFS/BFS
4
Mark Territory
Mark all explored land as 'visited' (change to '0') so you don't count it again
5
Continue Search
Resume scanning from where you left off to find the next undiscovered island
Key Takeaway
🎯 Key Insight: By using graph traversal (DFS/BFS), we can explore each connected island completely in one pass, ensuring we count each island exactly once while achieving optimal O(m Γ— n) time complexity.

Time & Space Complexity

Time Complexity
⏱️
O(m Γ— n)

Each cell is visited exactly once, either during grid scan or BFS traversal

n
2n
βœ“ Linear Growth
Space Complexity
O(min(m, n))

Queue size can be at most min(m,n) in worst case when BFS explores diagonally

n
2n
⚑ Linearithmic Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≀ m, n ≀ 300
  • grid[i][j] is '0' or '1'
  • Grid cells contain only characters '0' and '1'
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
67.8K Views
Very High Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen