Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Max Area of Island

Medium
graphdfsbfsmatrix

Given a 0/1 grid, return the area (cell count) of the largest island, or 0 if there is no land. Islands connect 4-directionally.

Input

  • Line 1: r c
  • Next r lines: the grid rows

Output: the maximum island area.

Example 1
Input:
4 5
0 1 0 0 1
1 1 0 1 1
0 0 0 1 1
0 0 0 0 0
Output:
5

The right-side island has 5 cells.

Example 2
Input:
1 1
0
Output:
0

Constraints

  • 1 ≤ r, c ≤ 50
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.