Top 150 InterviewEasy

Max Area of Island

Detailed guide and Python implementation for the 'Max Area of Island' problem.

Problem Statement

Easy

You are given an m x n binary matrix grid. An island is a group of 1s (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.

Write a function maxAreaOfIsland(grid: List[List[int]]) -> int.

Constraints
  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 50
  • grid[i][j] is 0 or 1

Examples

Example 1
Input
grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output
6
Explanation

The maximum area of an island is 6, located in the lower-right part of the grid.

Example 2
Input
grid = [[0,0,0,0,0,0,0,0]]
Output
0
Explanation

There are no islands in the grid, so the max area is 0.

Need a Hint?
Consider using Graphs-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.