<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:
        uf = UnionFind()
        m,n = len(grid),len(grid[0])
        steps = [(-1,0),(1,0),(0,1),(0,-1)]
        for i in range(m):
            for j in range(n):
                if grid[i][j]:
                    uf.add((i,j))
                    for step in steps:
                        uf.union((i,j), (i+step[0],j+step[1]))
        #res = 1 if not uf.weight.values() else max(uf.weight.values())
        weight = {}
        res = 1
        for i in range(m):
            for j in range(n):
                if not grid[i][j]:
                    continue
                root = uf.find((i,j))
                if root not in weight.keys():
                    weight[root] = 0
                weight[root] = weight.get(root,0)+1
                res = max(res, weight[root])
        #print(weight)
        #res = max(res, list(weight.values()))
        for i in range(m):
            for j in range(n):
                if not grid[i][j]:
                    candidates = set()
                    for step in steps:
                        root = uf.find((i+step[0],j+step[1]))
                        if not root:
                            continue
                        candidates.add(root)
                    cur = 1
                    for c in candidates:
                        cur+=weight[c]
                    res = max(res, cur)
        #print('weight:',weight)
        #print('uf:', uf.uf)
        return res
class UnionFind:
    def __init__(self):
        self.uf = {}
        #self.weight = {}
    def print(self):
        print('uf:', self.uf,'weight:',self.weight)
    def add(self, num):
        if num in self.uf.keys():
            return
        self.uf[num] = num
    def find(self, num):
        if num not in self.uf.keys():
            return None
        root = num
        while root!=self.uf[root]:
            root = self.uf[root]
        self.uf[num] = root
        return root
    def union(self, num1, num2):
        def calc(num):
            return num[0]**2+num[1]**2
        root1,root2 = self.find(num1), self.find(num2)
        if not root1 or not root2 or root1==root2:
            return False
        if calc(root1)<calc(root2):
            self.uf[root2] = root1
        else:
            self.uf[root1] = root2
        return True
45343