<p>You are given a map of a server center, represented as a <code>m * n</code> integer matrix&nbsp;<code>grid</code>, 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.<br />
<br />
Return the number of servers&nbsp;that communicate with any other server.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-6.jpg" style="width: 202px; height: 203px;" /></p>

<pre>
<strong>Input:</strong> grid = [[1,0],[0,1]]
<strong>Output:</strong> 0
<b>Explanation:</b>&nbsp;No servers can communicate with others.</pre>

<p><strong class="example">Example 2:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/11/13/untitled-diagram-4.jpg" style="width: 203px; height: 203px;" /></strong></p>

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

<p><strong class="example">Example 3:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-1-3.jpg" style="width: 443px; height: 443px;" /></p>

<pre>
<strong>Input:</strong> grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
<strong>Output:</strong> 4
<b>Explanation:</b>&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&#39;t communicate with any other server.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m &lt;= 250</code></li>
	<li><code>1 &lt;= n &lt;= 250</code></li>
	<li><code>grid[i][j] == 0 or 1</code></li>
</ul>

class DS:
    def __init__(self,n):
        self.n=n
        self.parent=[x for x in range(n)]
        self.size=[1 for _ in range(n)]
    def findP(self,node):
        if(self.parent[node]==node):
            return node
        self.parent[node]=self.findP(self.parent[node])
        return self.parent[node]
    def union(self,node1,node2):
        node1P=self.findP(node1)
        node2P=self.findP(node2)
        if(node1P==node2P):
            return
        if(self.size[node1P]<self.size[node2P]):
            self.parent[node1P]=node2P
            self.size[node2P]+=self.size[node1P]
        else:
            self.parent[node2P]=node1P
            self.size[node1P]+=self.size[node2P]
        return
    def getSize(self,node):
        return self.size[node]
    def printPS(self):
        print(self.parent)
        print(self.size)
class Solution:
    def countServers(self, grid: List[List[int]]) -> int:
        maxr=0
        rows=len(grid)
        cols=len(grid[0])
        # for row,col in grid:
        #     maxr=max(maxr,row)
        obj=DS(rows+cols)
        servers=[]
        mapCoordinate=defaultdict(int)
        for row in range(rows):
            for col in range(cols):
                if(grid[row][col]==1):
                    colNum=rows+col
                    obj.union(row,colNum)
                    mapCoordinate[row]+=1
                    mapCoordinate[colNum]+=1
                    # servers.append([row,col])
        cnt=0
        obj.printPS()
        # for node in mapCoordinate:
        #     if(obj.findP(node)==node and obj.getSize(node)>2):
        #         cnt+=1+obj.getSize(node)-2
            # if(obj.getSize(row)>1 or obj.getSize(col+rows)>1):
            #     cnt+=1
        map=defaultdict(int)
        for node in range(rows+cols):
            map[obj.findP(node)]+=mapCoordinate[node]
        for node,val in map.items():
            if(val>2):
                cnt+=val//2
        return cnt

        
18000