<p>You are given an <code>n x n</code> binary matrix <code>grid</code>. You are allowed to change <strong>at most one</strong> <code>0</code> to be <code>1</code>.</p>

<p>Return <em>the size of the largest <strong>island</strong> in</em> <code>grid</code> <em>after applying this operation</em>.</p>

<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>

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

<pre>
<strong>Input:</strong> grid = [[1,0],[0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
</pre>

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

<pre>
<strong>Input:</strong> grid = [[1,1],[1,0]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>Change the 0 to 1 and make the island bigger, only one island with area = 4.</pre>

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

<pre>
<strong>Input:</strong> grid = [[1,1],[1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Can&#39;t change any 0 to 1, only one island with area = 4.
</pre>

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

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

class DisjointSet:
    def __init__(self, n):
        self.rank = [0] * n
        self.size = [1] * n
        self.parent = [i for i in range(n)]

    def find_parent(self, node):
        if self.parent[node] == node:
            return node
        self.parent[node] = self.find_parent(self.parent[node])
        return self.parent[node]

    def is_connected(self, u, v):
        return self.find_parent(u) == self.find_parent(v)

    def union_by_rank(self, u, v):
        parent_u = self.find_parent(u)
        parent_v = self.find_parent(v)
        if parent_u == parent_v:
            return
        if self.rank[parent_u] == self.rank[parent_v]:
            self.rank[parent_u] += 1
            self.parent[parent_v] = parent_u
        elif self.rank[parent_u] > self.rank[parent_v]:
            self.parent[parent_v] = parent_u
        else:
            self.parent[parent_u] = parent_v

    def union_by_size(self, u, v):
        parent_u = self.find_parent(u)
        parent_v = self.find_parent(v)
        if parent_u == parent_v:
            return
        if self.size[parent_u] >= self.size[parent_v]:
            self.parent[parent_v] = parent_u
            self.size[parent_u] += self.size[parent_v]
        else:
            self.parent[parent_u] = parent_v
            self.size[parent_v] += self.size[parent_u]


class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        n = len(grid)
        ds = DisjointSet(n * n)
        drs = [[0, 1], [1, 0],[-1, 0], [0, -1]]
        for i in range(n):
            for j in range(n):
                if grid[i][j] == 0:
                    continue
                node = i * n + j
                for dr in drs:
                    new_r = i + dr[0]
                    new_c = j + dr[1]
                    if 0 <= new_r < n and 0 <= new_c < n and grid[new_r][new_c] == 1:
                        ds.union_by_rank(node, new_r * n + new_c)
        parent_count = collections.defaultdict(lambda : 0)
        maxi = 0
        for i in range(n * n):
            parent = ds.find_parent(i)
            parent_count[parent] += 1
            maxi = max(maxi, parent_count[parent])
        for i in range(n):
            for j in range(n):
                if grid[i][j] == 0:
                    count = 1
                    neighbours = set()
                    for dr in drs:
                        new_r = i + dr[0]
                        new_c = j + dr[1]
                        new_node = new_r * n + new_c
                        if 0 <= new_r < n and 0 <= new_c < n and grid[new_r][new_c] == 1:
                            parent = ds.find_parent(new_node)
                            if parent not in neighbours:
                                count += parent_count[parent]
                            neighbours.add(parent)
                    maxi = max(maxi, count)
        return maxi
        
63868