You're given a 2D grid filled with 0s and 1s. A '1' represents land, and a '0' represents water. An island is a group of adjacent land cells connected horizontally or vertically (not diagonally). Your goal is to find the largest possible area of any single island in the grid — that is, the maximum number of connected 1s in any region.
To solve this, you need to identify each distinct island and measure its size. Since you only want the size of the largest island, you can explore each island one by one and track the maximum size encountered.
The most efficient and intuitive approach to finding island sizes is to use depth-first search. You start from any cell with a 1 and recursively explore all four directions (up, down, left, right) to count the size of the connected region.
Each time you visit a land cell (1), mark it as visited (e.g., by setting it to 0) so it won't be counted again. This prevents infinite loops and double-counting.
The DFS function returns the total area of the island by summing 1 (the current cell) plus the results of recursive calls to its four neighbors. Each complete DFS traversal returns the size of one island.
Loop through every cell in the grid. When you find a land cell (1), initiate a DFS starting from that cell. Let the DFS return the area of that island, and update your global max area if this new value is larger. After visiting every cell, the largest island area will be stored in max_area.
Time Complexity: O(m × n), where m is the number of rows and n is the number of columns. Every cell is visited at most once.
Space Complexity: O(m × n) in the worst case due to the call stack from recursion, if the entire grid is one giant island.
- If the grid is entirely water (all 0s), the result is 0.
- If the grid is entirely land (all 1s), the result is m × n.
- Small grids like 1x1 still work correctly with the DFS-based approach.
This problem is a perfect example of how DFS can be used to explore 2D space. By using DFS to explore and mark visited land, we efficiently isolate each island and calculate its area, ensuring optimal performance and correctness.