Loading...
You are given an m x n binary matrix grid, where every cell is 0 or 1.
A region is a group of 1 cells connected 4-directionally: up, down, left, or right. Cells touching only at a corner are not connected. The size of a region is how many cells it contains.
Return the size of the largest region in grid, or 0 if the grid contains no 1 at all.
Input: 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]]
Output: 6
Explanation: The largest region is the block of six 1s in rows 3 and 4, columns 4 and 5
plus rows 2 and 3, column 4. No region has more than six cells. Note the 1 at row 0,
column 2 touches nothing, so it forms a region of size 1 on its own.
Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0
Explanation: There is no 1 anywhere, so there is no region.
m = grid.lengthn = grid[i].lengthm, n ≤50grid[i][j] is either 0 or 1.Click "Run" to test with sample cases or "Submit" to run all tests.