<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>

import heapq
class Solution:
    def find(self,parent,x):
        if x!=parent[x]:
            parent[x]=self.find(parent,parent[x])
        return parent[x]
    def largestIsland(self, grid: List[List[int]]) -> int:
        n=len(grid)
        m=1
        r=[-1,0,0,1]
        c=[0,-1,1,0]
        parent=[-1]*(n*n)
        pq=[]
        for i in range(n):
            for j in range(n):
                if grid[i][j]==1:
                    if parent[i*n+j]==-1:
                        parent[i*n+j]=i*n+j
                    for k in range(4):
                        nr=i+r[k]
                        nc=j+c[k]
                        if 0<=nr<n and 0<=nc<n and grid[nr][nc]==1:
                            heapq.heappush(pq,(i*n+j,nr*n+nc))
        size=[1]*(n*n)
        while pq:
            u,v=heapq.heappop(pq)
            ulp_u=self.find(parent,u)
            ulp_v=self.find(parent,v)
            if parent[ulp_u]!=parent[ulp_v]:
                if size[ulp_v]>size[ulp_u]:
                    parent[ulp_u]=parent[ulp_v]
                    size[ulp_v]+=size[ulp_u]
                else:
                    parent[ulp_v]=parent[ulp_u]
                    size[ulp_u]+=size[ulp_v]
        
        island_map = {}
        for i in range(n * n):
            if parent[i] != -1:
                root = self.find(parent, i)
                island_map[root] = size[root]
        max_island = max(island_map.values(), default=0)
        print(max_island)
        for i in range(n):
            for j in range(n):
                if grid[i][j]==0:
                    s=set()
                    curr_size=1
                    for k in range(4):
                        nr=i+r[k]
                        nc=j+c[k]
                        if 0<=nr<n and 0<=nc<n and grid[nr][nc]==1:
                            root=self.find(parent,nr*n+nc)
                            if root not in s:
                                s.add(root)
                                curr_size+=island_map[root]
                    max_island=max(max_island,curr_size)
        return max_island if max_island > 0 else n * n
        
63156