Loading problem...
You are given a 2D binary matrix grid of dimensions m x n, where each cell contains either 0 (representing water) or 1 (representing land). A land region is defined as a collection of land cells (1s) that are connected horizontally or vertically (4-directional connectivity). Diagonal connections do not count as valid adjacencies.
The size of a land region is measured by counting the total number of land cells it contains.
Your task is to determine the size of the largest land region present in the grid. If the grid contains no land cells (i.e., the entire grid is water), return 0.
Important Notes:
0 or 1.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]]6The grid contains multiple distinct land regions. The largest connected land region consists of 6 cells. Note that although there are other land cells scattered throughout the grid, they form smaller separate regions. The answer is 6, not 11, because land cells must be connected 4-directionally (horizontally or vertically), not diagonally.
grid = [[0,0,0,0,0,0,0,0]]0The entire grid consists only of water cells (0s). Since there are no land cells present, there are no land regions, and the maximum area is 0.
grid = [[1,1,0],[1,1,0],[0,0,0]]4There is exactly one land region in the top-left portion of the grid, consisting of 4 connected land cells arranged in a 2x2 square. This is the largest (and only) land region, so the answer is 4.
Constraints