Practice · Graphs · 2 of 3

Largest Island

You are given grid, a map made of 1s (land) and 0s (water). An island is a group of land cells joined up, down, left or right, and its area is the number of cells in it.

Return the area of the largest island, or 0 if there is no land at all.

Example 1

Input
grid = [[0, 1, 1, 0, 0], [0, 1, 0, 0, 1], [1, 0, 0, 1, 1], [1, 1, 0, 1, 0]]
Output
4
The island on the right has 4 cells. The ones at the top left and the bottom left have 3 each.

Example 2

Input
grid = [[0, 0, 0], [0, 0, 0]]
Output
0
There is no land.

Constraints

  • The grid has 1 to 200 rows and 1 to 200 columns.
  • Every cell is 0 or 1.