Count Communicating Servers
Problem statement
A binary matrix represents servers: grid[r][c] = 1 means a server is present. Two servers can communicate when they share a row or column.
Return the number of servers that can communicate with at least one other server.
Function
countCommunicatingServers(grid: int[][]) → intExamples
Example 1
grid = [[1,0],[0,1]]return = 0Neither server shares a row or column.
Example 2
grid = [[1,0],[1,1]]return = 3All three servers communicate.
Example 3
grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0]]return = 4Every server belongs to a shared row or column.
Constraints
0 <= rows, columns <= 1000.- Every cell is
0or1.