Loading...
Loading...
You are given a two-dimensional list of integers grid with m rows and n columns. Every cell is either 0 or 1.
Call a cell equal to 1 linked if there is at least one other cell equal to 1 in the same row or in the same column.
Return the number of linked cells.
Input: grid = [[1, 0],
[0, 1]]
Output: 0
Explanation: The two 1 cells are at [0][0] and [1][1]. They are in different rows and different columns, so neither one is linked.
Input: grid = [[1, 0],
[1, 1]]
Output: 3
Explanation: The cells [0][0] and [1][0] share column 0. The cells [1][0] and [1][1] share row 1. Every 1 has another 1 in its row or column, so all three are linked.
Input: grid = [[1, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]
Output: 4
Explanation: The two 1 cells in row 0 share a row. The two 1 cells in column 2 share a column. The cell [3][3] is the only 1 in row 3 and the only 1 in column 3, so it is not linked.
m = grid.lengthn = grid[i].lengthm ≤250n ≤250grid[i][j] is 0 or 1