Skip to content
Veltoryx Technologies
FreeProblem 108● MediumLeetCode #695 ↗3 min readUpdated 30 October 2026

Max Area of Island

Find the maximum area of a connected island in a 2D binary grid using DFS.

01The problem

You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

The area of an island is the number of cells with a value 1 in the island.

Return the maximum area of an island in grid. If there is no island, return 0.

InputOutputWhy
grid = [[0,0,1,0,0],[0,1,1,1,0],[0,0,1,0,0],[1,1,0,0,0]]5The cross-shaped island in rows 0-2 has area 5 (1+3+1). Second island has area 2.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • grid[i][j] is either 0 or 1.

02How to spot it

  • "Max area of an island": Extension of Number of Islands (#200). Instead of simply counting components, the DFS sums the area of each component: 1 + area(up) + area(down) + area(left) + area(right).

03Pseudocode

text
area(r, c):
  if outside or cell != 1: return 0
  cell = 0
  return 1 + area of the 4 neighbours
best = max of area(r, c) over all cells

04Watch it work

Visualizer · step through the algorithmgrid = [[0,0,1,0,0],[0,1,1,1,0],[0,0,1,0,0],[1,1,0,0,0]]

grid

00100
01110
00100
11000

max area

0

Find max island area: compute area of each island with DFS, zeroing visited land.

Pseudocode

  1. area(r, c):
  2. if outside or cell != 1: return 0
  3. cell = 0
  4. return 1 + area of the 4 neighbours
  5. best = max of area(r, c) over all cells

Variables

best
0
step 1 / 4

05Step-by-step dry run

We trace on grid = [[0,0,1,0,0],[0,1,1,1,0],[0,0,1,0,0],[1,1,0,0,0]]:

Scan cellLand?DFS area sumbest updated
(0, 0) to (0, 1)0-0
(0, 2)11 + (1 + 1 + 1) + 1 = 5max(0, 5) = 5
(3, 0)11 + 1 = 2max(5, 2) = 5

Output: 5.

06Java solution

java
public class Solution {
    public static int maxAreaOfIsland(int[][] grid) {
        int maxArea = 0;
        int m = grid.length;
        int n = grid[0].length;

        for (int r = 0; r < m; r++) {
            for (int c = 0; c < n; c++) {
                if (grid[r][c] == 1) {
                    maxArea = Math.max(maxArea, area(grid, r, c));
                }
            }
        }
        return maxArea;
    }

    private static int area(int[][] grid, int r, int c) {
        if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || grid[r][c] != 1) {
            return 0;
        }
        grid[r][c] = 0;
        return 1 + area(grid, r + 1, c)
                 + area(grid, r - 1, c)
                 + area(grid, r, c + 1)
                 + area(grid, r, c - 1);
    }

    public static void main(String[] args) {
        int[][] grid = {
            {0, 0, 1, 0, 0},
            {0, 1, 1, 1, 0},
            {0, 0, 1, 0, 0},
            {1, 1, 0, 0, 0}
        };
        System.out.println(maxAreaOfIsland(grid));
    }
}
Expected output
5

07Why it works

The recurrence 1 + sum(neighborAreas) naturally sums the size of the connected component. Sinking each cell grid[r][c] = 0 immediately before recursing ensures no cell is counted more than once.

08Time and space

  • Time complexity: O(m * n). Each cell is checked by the loop and visited by DFS at most once.
  • Space complexity: O(m * n) recursion stack memory for the deepest connected component.

09Edge cases

  • All zeros: Returns 0.
  • All ones: Returns m * n.

10Common mistakes

  • Forgetting to sink the cell: Calling area without changing grid[r][c] = 0 causes immediate infinite recursion between adjacent cells.

11Other approaches

  • BFS with Queue: Keep a running count of cells dequeued in the current island.

12In the interview

  • Opening line: "I will scan each cell. When I find 1, I run a DFS that sinks the cell to 0 and returns 1 + area of 4 neighbors. I track the maximum area seen."

13Quiz

What is the base case return value when a neighbor is water (0) or outside the grid?

  • (Recommended) 0
  • 1
  • -1
  • null

Answer: 0. Water and out-of-bounds cells contribute 0 area to the island.

14Solve it yourself

Open LeetCode #695 and submit the solution.