Loading...
You are given a grid of non-negative integers with m rows and n columns. A cell with value 0 is blocked. A cell with a positive value belongs to a component.
A component is a maximal group of positive cells connected 4-directionally (up, down, left, or right). The sum of a component is the total of its cell values.
Return the largest component sum in the grid, or 0 if every cell is 0.
Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
Output: 7
Explanation: The largest connected group of positive cells has values 3 and 4, for a total of 7.
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
Output: 1
Explanation: Two separate single-cell components each have value 1, so the largest sum is 1.
gridgrid[i][j] ≤10Click "Run" to test with sample cases or "Submit" to run all tests.