<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:
        #m= len(grid)
        n= len(grid[0])
        #first do a dfs on all 1s and note down the island number, island size of each
        island_size = defaultdict(int)
        island_num = [[0 for i in range(n)] for i in range(n)]

        neigh = [(0,1),(1,0),(0,-1),(-1,0)]

        def dfs(i,j, i_n):
            nonlocal island_num,island_size, neigh
            
            for x,y in neigh:
                if 0<=i+x<n and 0<=j+y<n and grid[i+x][j+y]==1:
                    grid[i+x][j+y] = i_n
                    seen.add((i+x,j+y))
                    #island_num[i+x][j+y] = i_n
                    #print(island_num[i+x][j+y],i_n)
                    dfs(i+x,j+y,i_n)
        
        i_n=2
        for r in range(n):
            for c in range(n):
                if grid[r][c]==1:
                    
                    i_n+=1
                    grid[r][c]=i_n
                    seen = {(r,c)}
                    #print(r,c,i_n)
                    dfs(r,c,i_n)
                    island_size[i_n]=len(seen)

        # print(grid)
        # print(island_size)
        max_size = -float(inf)
        one_zero = False
        for i in range(n):
            for j in range(n):
                if grid[i][j]==0:
                    one_zero = True
                    size = 0
                    uniq_isl = set()

                    neigh = [(0,1),(1,0),(0,-1),(-1,0)]
                    for x,y in neigh:
                        if 0<=i+x<n and 0<=j+y<n and grid[i+x][j+y]!=0 and grid[i+x][j+y] not in uniq_isl:
                            uniq_isl.add(grid[i+x][j+y])
                            size+=island_size[grid[i+x][j+y]]
                    #if size:
                    size+=1
                    max_size = max(size,max_size)
        if not one_zero:
            return n*n

        if max_size == -float(inf):
            return 0
        return max_size






        #then iterate on all 0s, see if neighbors are island of different type, if yes add their area
26106