991. Max Area of Island

EasyGraphsGridDFSGraph

Given a 2D grid of 1s (land) and 0s (water), return the maximum area (number of cells) of any island, where an island is a maximal group of 1s connected horizontally or vertically. If there is no land, return 0. The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the maximum island area.

Examples

Example 1
Input: {"grid":[[1,1,0,0],[1,0,0,1],[0,0,1,1]]}
Output: 3
Explanation: The largest island has 3 cells.
Example 2
Input: {"grid":[[0,0],[0,0]]}
Output: 0
Explanation: No land.
Example 3
Input: {"grid":[[1,1],[1,1]]}
Output: 4
Explanation: One island of 4.

Constraints

Asked by

MetaAdobeAmazonOracleGoogleBloomberg
Solve this problem in the editor →