Count Servers that Communicate

Medium

Description

You are given a map of a server center, represented as an m x n integer matrix grid. A server can communicate with other servers if it shares a row or column. Return the number of servers that can communicate with at least one other server.

Examples

Input:grid = [[1,0],[0,1]]
Output:0
Explanation:

No servers share row/column.

Input:grid = [[1,0,0,1,0],[0,0,0,0,0],[0,0,0,1,0]]
Output:3
Explanation:

There are 4 servers total at positions (0,0), (0,3), and (2,3). The server at (0,0) can communicate with the server at (0,3) because they share row 0. The server at (0,3) can communicate with both the server at (0,0) in the same row and the server at (2,3) in the same column 3. The server at (2,3) can communicate with the server at (0,3) in the same column. Therefore, all 3 servers can communicate with at least one other server.

Input:grid = [[1,0,0],[0,0,0],[0,0,1]]
Output:0
Explanation:

There are 2 servers at positions (0,0) and (2,2). The server at (0,0) is alone in row 0 and column 0. The server at (2,2) is alone in row 2 and column 2. Since they don't share any row or column, neither server can communicate with the other, so 0 servers can communicate.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 250

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!