Problems/695. Max Area of Island

695. Max Area of Island

MediumMatrixGraphDFSBFS

You are given a 2D binary grid containing 1s (representing land) and 0s (representing water). Find and return the area of the largest island in the grid. If there are no islands, return 0.

The area of an island is the total number of connected land cells in it, grouped either horizontally or vertically.

Example 1
Input: grid = [ [0, 0, 1, 0, 0], [1, 1, 1, 0, 1], [0, 1, 0, 0, 1] ]
Output: 5
The largest island is the one on the left/middle with an area of 5 cells (connected land cells).
Example 2
Input: grid = [ [0, 0, 0], [0, 0, 0] ]
Output: 0
No land cells exist in the grid, so the maximum area is 0.
Example 3
Input: grid = [ [1, 1], [1, 1] ]
Output: 4
All cells are land and connected, so the single island has an area of 4.
Visualizer

Visualizer will appear here