<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 Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        n = len(grid)
        us = UnionSet(n)
        islandCount = 0
        for y in range(n):
            for x in range(n):
                if grid[y][x] == 1:
                    islandCount += 1
                    if y>0 and grid[y-1][x] == 1:
                        us.join(y-1, x, y, x) 
                    if x>0 and grid[y][x-1] == 1:
                        us.join(y, x-1, y, x)
        if islandCount == n*n:
            return n*n
        

        # print(us.arr)
        # print(us.usSize)
        
        directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
        ans = 0
        for y in range(n):
            for x in range(n):
                if grid[y][x] == 1:
                    continue
                islandIds = set()
                tAns = 0
                for direction in directions:
                    ny, nx = y + direction[0], x + direction[1]
                    if ny >=0 and ny < n and nx >= 0 and nx < n:
                        if grid[ny][nx] == 0:
                            continue
                        id = us.getParent(ny, nx)
                        if id in islandIds:
                            continue
                        islandIds.add(id)
                        tAns += us.getUsSize(ny, nx)
                
                # print(f'y, x = {y},{x} with tans = {tAns}')
                ans = max(ans, tAns + 1)
        return ans




class UnionSet:
    def __init__(self, n: int):
        self.n = n
        self.arr = [i for i in range(n*n)]
        self.usSize = {}
        for i in range(n*n):
            self.usSize[i] = 1

    def join(self, y1, x1, y2, x2):
        p1 = self.getParent(y1, x1)
        p2 = self.getParent(y2, x2)
        s1 = self.getUsSize(y1, x1)
        s2 = self.getUsSize(y2, x2)
        if p1 != p2:
            self.arr[p2] = p1
            self.usSize[p1] = s1 + s2
            # print(f'join {y1},{x1} and {y2},{x2}')
            # print(f'stats: {p1} {p2} {s1} {s2}')

    def getParent(self, y, x) -> int:
        i = y * self.n + x
        while self.arr[i] != i:
            i = self.arr[i]
        return i

    def getUsSize(self, y, x) -> int:
        p = self.getParent(y, x)
        return self.usSize[p]

59593