Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix

Max Area of Island

mediumgraphdfsgrid

Problem

You are given a grid of 0s and 1s. A cell with value 1 is land; an island is a maximal group of land cells connected by up/down/left/right moves. The area of an island is the number of cells in it. Return the area of the largest island, or 0 if there is no land.

  • The grid has between 1 and 60 rows and between 1 and 60 columns.
  • Each cell is the integer 0 or 1.

Examples

  • grid = [[1,1,0,0,1],[0,1,0,1,1],[0,0,0,1,0],[1,0,0,1,0]] → 5. Three islands: the top-left cluster has 3 cells, the bottom-left corner is a lone cell, and the cluster snaking down the right side has 5.
  • grid = [[0,1,0],[1,1,1],[0,1,0]] → 5. A plus sign is one island of five cells.
  • grid = [[0,0],[0,0]] → 0. No land.

Hints

  1. This is Number of Islands, except that instead of counting islands you need to measure each one.
  2. A flood fill naturally visits every cell of one island exactly once. What does its return value need to be?
  3. DFS/BFS from each unvisited land cell, have the fill return the number of cells it consumed, and keep the running maximum.

Last reviewed 8 September 2026