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


        size_grid=[[0]*len(grid) for _ in range(len(grid))]
        group_grid=[[0]*len(grid) for _ in range(len(grid))]
        #print(size_grid)
        visited=set()
        moves=[(1,0),(-1,0),(0,1),(0,-1)]
        group_id=0
        def bfs(row,col):
            nonlocal group_id
            queue=[]
            queue=deque()
            area=0
            new_visited=set()
            if (row,col) not in visited:
                queue.append((row,col))
            while queue:
                row,col=queue.popleft()
                
                if (row,col) in visited:
                    continue
                area=area+1
                visited.add((row,col))
                new_visited.add((row,col))
                for move in moves:
                    dr,dc=move
                    new_row=dr+row
                    new_col=dc+col
                    if 0<=new_row<len(grid) and 0<=new_col<len(grid) and grid[new_row][new_col]==1 and (new_row,new_col) not in visited:
                        queue.append((new_row,new_col))
            
            for position in new_visited:
                row,col=position
                size_grid[row][col]=area
            group_id=group_id+1
            for position in new_visited:
                row,col=position
                group_grid[row][col]=group_id
            
            
            return area
        max_area=0
        for row in range(len(grid)):
            for col in range(len(grid)):
                if grid[row][col]==1:
                    new_area=bfs(row,col)
                    #print(new_area)
                    max_area=max(new_area,max_area)
        
        for row in range(len(grid)):
            for col in range(len(grid)):
                if grid[row][col]==0:
                    group_considered=set()
                    total_new_area=0
                    for move in moves:
                        dr,dc=move
                        new_row=dr+row
                        new_col=dc+col
                        if 0<=new_row<len(grid) and 0<=new_col<len(grid) and group_grid[new_row][new_col] not in group_considered :
                            new_area=size_grid[new_row][new_col]
                            
                            total_new_area=total_new_area+new_area
                            group_considered.add(group_grid[new_row][new_col])
                    #print(total_new_area)
                    max_area=max(total_new_area+1,max_area)



        return max_area


        
41781