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
Visualization
Time & Space Complexity
Each cell is visited exactly once, either during grid scan or BFS traversal
Queue size can be at most min(m,n) in worst case when BFS explores diagonally
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'