<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 Solution:
    def countServers(self, grid: List[List[int]]) -> int:
        m = len(grid) # rows
        n = len(grid[0]) # columns
        oneCoordinates = [(i, j) for i in range(m) for j in range(n) if grid[i][j]]
        serverIndices = {coordinate: i for i, coordinate in enumerate(oneCoordinates)} 

        uf = UnionFind(len(oneCoordinates))
        xs = {}
        ys = {}
        # store the coordinates into list based on the key x or key y
        # at same time union the servers that are on same axis
        for coord in oneCoordinates:
            x, y = coord
            u = None
            if x not in xs:
                xs[x] = [y]
                
            else:
                xs[x].append(y)
                u = serverIndices[(x, xs[x][0])] # first coordinate
                v = serverIndices[(x, y)] # marginal coordinate
                uf.union(u, v)

            if y not in ys:
                ys[y] = [x]
            else:
                ys[y].append(x)
                u = serverIndices[(ys[y][0], y)] # first coordinate
                v = serverIndices[(x, y)] # marginal coordinate
                uf.union(u, v)


        parents = [uf.find(i) for i in range(len(oneCoordinates))]
        counter = Counter(parents)
        count = 0

        # print(f"counter {counter}, parents {parents}")

        
        for v in counter.values():
            if v > 1: # for each parent (or grouping), 
                      # if the count of server is more than 1, then
                      # add the server count (counter frequency)
                count += v
        return count


class UnionFind():
     
    def __init__(self, size): # size == number of nodes
        self.parent = list(range(size))
        self.rank = [0]*size

        # modified variable to hold last edge whose
        # vertices are already connected during the union process
        self.last_edge_already_connected = None


    def find(self, x: int) -> int:
        # base case
        if self.parent[x] == x:
            return x
        
        # recursive case with path compression
        self.parent[x] = self.find(self.parent[x])

        return self.parent[x]
    
    def union(self, u: int, v: int) -> None:
        
        uParent = self.find(u) 
        vParent = self.find(v) 

        if uParent == vParent: # nodes v and u are 
                               # already connected 
                               # even without adding edge
            self.last_edge_already_connected = (u, v)
            return

        # lines below execute if uParent != vParent
        uParentRank = self.rank[uParent]
        vParentRank = self.rank[vParent]

        if uParentRank < vParentRank:
            self.parent[uParent] = vParent
        elif uParentRank > vParentRank:
            self.parent[vParent] = uParent
        else:
            self.parent[uParent] = vParent
            self.rank[vParent] += 1

18600