<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.parent = [i for i in range(n)]
    self.rank = [0] * n
  
  def findParent(self, node):
    if node == self.parent[node]:
      return node
    self.parent[node] = self.findParent(self.parent[node])
    return self.parent[node]
  
  def unionbyRank(self, u, v):
    ulp_u = self.findParent(u)
    ulp_v = self.findParent(v)
    if ulp_u == ulp_v:
      return 
    if self.rank[ulp_u] > self.rank[ulp_v]:
      self.parent[ulp_v] = ulp_u
    elif self.rank[ulp_v] > self.rank[ulp_u]:
      self.parent[ulp_u] = ulp_v
    else:
      self.rank[ulp_u] += 1
      self.parent[ulp_v] = ulp_u

class Solution:
  def largestIsland(self, grid: List[List[int]]) -> int:
    res = 0
    count = 0
    visited = set()
    ds = DisjointSet(len(grid) * len(grid))
    queue = collections.deque()
    d = [[0,1],[1,0],[0,-1],[-1,0]]
    parents = {}

    def dfs(i,j, grid, visited):
      if i >= 0 and j >= 0 and i < len(grid) and j < len(grid[i]) and (i,j) not in visited and grid[i][j] == 1:
        visited.add((i,j))
        res = 1 + dfs(i,j + 1, grid, visited) + dfs(i + 1, j, grid, visited) + dfs(i, j - 1, grid, visited) + dfs(i - 1, j, grid, visited)
        return res
      return 0
    

    for r in range(len(grid)):
      for c in range(len(grid[r])):
        if grid[r][c] == 1:
          queue.append([r,c])
        while queue:
          i,j = queue.popleft()
          grid[i][j] = -1
          for x,y in d:
            newr = i + x
            newc = j + y

            if newr >= 0 and newc >= 0 and newr < len(grid) and newc < len(grid[0]) and grid[newr][newc] == 1:
              u = i * len(grid) + j
              v = newr * len(grid) + newc
              if ds.findParent(u) != ds.findParent(v):
                grid[newr][newc] = -1
                ds.unionbyRank(u,v)
                queue.append([newr,newc])
                #print(u,v, ds.parent)
    
    for i in range(len(grid)):
      for j in range(len(grid[i])):
        if grid[i][j] == -1:
          grid[i][j] = 1

    for i in range(len(grid)):
      for j in range(len(grid[i])):
        if (i,j) not in visited and grid[i][j] == 1:
          count = dfs(i,j,grid,visited)
          res = max(res, count)
          node = i * len(grid) + j
          p = ds.findParent(node)
          #print(ds.parent)
          parents[p] = count
    
    #print(ds.parent)
    #print(parents)
    for i in range(len(grid)):
      for j in range(len(grid[i])):
        visited = set()
        count = 1
        if grid[i][j] == 0:
          for x,y in d:
            newr = i + x
            newc = j + y

            if newr >= 0 and newc >= 0 and newr < len(grid) and newc < len(grid[0]) and grid[newr][newc] == 1:
              u = newr * len(grid) + newc
              node = ds.findParent(u)
              if node not in visited:
                count += parents[node]
                visited.add(node)
        res = max(res, count)
    
    #print(res,count)
    return res
48906