<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+1)
        self.parent = list(range(n+1))
    def findUPar(self,node):
        if node == self.parent[node]:
            return node
        self.parent[node] = self.findUPar(self.parent[node])  # Path compression
        return self.parent[node]
    def unionByRank(self,u,v):
        upu = self.findUPar(u)
        upv = self.findUPar(v)
        if upu == upv: return
        if self.rank[upu] < self.rank[upv]:
            self.parent[upu] = upv
        elif self.rank[upv] < self.rank[upu]:
            self.parent[upv] = upu
        else:
            self.parent[upv] = upu
            self.rank[upu] += 1
class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        dir = [(0,1),(1,0),(-1,0),(0,-1)]
        r,c = len(grid), len(grid[0])
        maxArea = 0
        ds = DisjointSet(r*c)
        parComp = defaultdict(int)
        #connect existing islands
        for i in range(r):
            for j in range(c):
                if grid[i][j] == 1:
                    for dr,dc in dir:
                        nr,nc = i+dr, j+dc
                        if r>nr>=0 and r>nc>=0 and grid[nr][nc] == 1:
                            node = (i*c)+j
                            adj = (nr*c)+nc
                            ds.unionByRank(node, adj)
        for node in range(r*c):
            parComp[ds.findUPar(node)] += 1
            maxArea = max(maxArea, parComp[ds.findUPar(node)])


        for i in range(r):
            for j in range(c):
                if grid[i][j] == 0:
                    par = set()
                    noOfComp = 1
                    for dr,dc in dir:
                        nr,nc = i+dr, j+dc
                        if r>nr>=0 and r>nc>=0 and grid[nr][nc] == 1:
                            adjP = ds.findUPar((nr*r)+nc)
                            if adjP not in par:
                                par.add(adjP)
                                noOfComp += parComp[adjP]
                    maxArea = max(maxArea, noOfComp)
        return maxArea

        
61731