You are given a map of a server center, represented as a m * n integer matrix&nbsp;grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.

Return the number of servers&nbsp;that communicate with any other server.

&nbsp;
Example 1:



Input: grid = [[1,0],[0,1]]
Output: 0
Explanation:&nbsp;No servers can communicate with others.

Example 2:



Input: grid = [[1,0],[1,1]]
Output: 3
Explanation:&nbsp;All three servers can communicate with at least one other server.


Example 3:



Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Output: 4
Explanation:&nbsp;The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.


&nbsp;
Constraints:


	m == grid.length
	n == grid[i].length
	1 &lt;= m &lt;= 250
	1 &lt;= n &lt;= 250
	grid[i][j] == 0 or 1


class Solution:
    def countServers(self, grid: List[List[int]]) -> int:
        def helper(row,col,count):
            for c in range(len(grid[0])):
                if c == col:
                    continue
                if grid[row][c] == 1:
                    count += 1
                    return count
            for r in range(len(grid)):
                if r == row:
                    continue
                if grid[r][col] == 1:
                    count += 1
                    return count
            return count
        count = 0
        for row in range(len(grid)):
            for col in range(len(grid[0])):
                if grid[row][col] == 1:
                    count = helper(row,col,count)
        return count
class Solution {
    int []parent;
    int []rank;
    public int countServers(int[][] grid) {
        parent=new int[grid.length*grid[0].length];
        rank=new int[grid.length*grid[0].length];
        for(int i=0;i<parent.length;i++){
            parent[i]=i;
            rank[i]=0;
        }
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j]==1){
                    check(i,j,grid);
                }
            }
        }
        int count=0;
        for(int i=0;i<parent.length;i++){
            if(parent[i]!=i||rank[i]>0){
              count++;
            }
        }
        return count;
    }
    public void check(int sr,int sc,int [][]grid){
       int mbox=sr*grid[0].length+sc;
        for(int i=sr;i<grid.length;i++){
            if(grid[i][sc]==1){
                int cbox=i*grid[0].length+sc;
                int xl=find(mbox);
                int yl=find(cbox);
                if(xl!=yl){
                    union(xl,yl);
                }
            }
        }
        for(int j=sc;j<grid[0].length;j++){
            if(grid[sr][j]==1){
                int cbox=sr*grid[0].length+j;
                int xl=find(mbox);
                int yl=find(cbox);
                if(xl!=yl){
                    union(xl,yl);
                }
            }
        }
    }
    int find(int x){
        if(parent[x]==x){
            return x;
        }else{
            parent[x]=find(parent[x]);
            return parent[x];
        }
    }
    void union(int x,int y){
        if(rank[x]>rank[y]){
            parent[y]=x;
        }else if(rank[y]>rank[x]){
            parent[x]=y;
        }else{
            parent[x]=y;
            rank[y]++;
        }
    }
}
class Solution {
public:
    int countServers(vector<vector<int>>& grid) {
        int n=grid.size(),m=grid[0].size();
        vector<vector<bool>>visited(n,vector<bool>(m,false));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(grid[i][j] && !visited[i][j])
                {
                    bool flag=false;
                    for(int x=0;x<n;x++)
                    {
                        if(x!=i && grid[x][j] && !visited[x][j])
                            visited[x][j]=true,flag=true;
                        if(!flag && x!=i && visited[x][j])
                            flag=true;
                    }
                    for(int x=0;x<m;x++)
                    {
                        if(x!=j && grid[i][x] && !visited[i][x])
                            visited[i][x]=true,flag=true;
                        if(!flag && x!=j && visited[i][x])
                            flag=true;
                    }
                    if(flag)
                        visited[i][j]=true;
                }
            }
        }
        int ans=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(visited[i][j])
                    ans++;

        return ans;
    }
};
var countServers = function(grid) {
    const m = grid.length;
    const n = grid[0].length;
    
    const lastRows = [];
    const lastCols = [];
    
    const set = new Set();
    
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] === 1) {                
                const currIdx = (i * n) + j;
               
                const rowNeiIdx = lastRows[i];
                
                if (rowNeiIdx != null) {
                    set.add(currIdx).add(rowNeiIdx);
                }
                
                const colNeiIdx = lastCols[j];
                
                if (colNeiIdx != null) {
                    set.add(currIdx).add(colNeiIdx);
                }
                
                lastRows[i] = currIdx;
                lastCols[j] = currIdx;
            }
        }
    }
    
    return set.size; 
};
Count Servers that Communicate