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

import queue
class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        islandsize = {}
        island_pairs = set()
        cur_island_name = "a"
        m, n = len(grid), len(grid[0])

        def bfs(grid, i, j, name):
            que = queue.Queue()
            que.put((i, j, True))
            grid[i][j] = name
            size = 1
            while not que.empty():
                cur_i, cur_j, in_land = que.get()
                
                if in_land:
                    print(name, cur_i, cur_j, size)
                    for next_i, next_j in [[cur_i+1, cur_j], [cur_i-1, cur_j], [cur_i, cur_j+1], [cur_i, cur_j-1]]:
                        if next_i >= 0 and next_i < m and next_j >= 0 and next_j < n:
                            if grid[next_i][next_j] == 1:
                                que.put((next_i, next_j, True))
                                grid[next_i][next_j] = name
                                size += 1
                            else:
                                que.put((next_i, next_j, False))
                else:
                    connected_island = set()
                    for next_i, next_j in [[cur_i+1, cur_j], [cur_i-1, cur_j], [cur_i, cur_j+1], [cur_i, cur_j-1]]:
                        if next_i >= 0 and next_i < m and next_j >= 0 and next_j < n and grid[next_i][next_j] not in [0, 1]:
                            # meet a new island
                            connected_island.add(grid[next_i][next_j])
                    if len(connected_island) > 1:
                        island_pairs.add(tuple(connected_island))
            return size

        for i in range(m):
            for j in range(n):
                if grid[i][j] != 1:
                    continue
                size = bfs(grid, i, j, cur_island_name)
                islandsize[cur_island_name] = size
                cur_island_name = chr(ord(cur_island_name) + 1)
        
        if len(islandsize) == 0:
            return 1

        print(islandsize)
        print(island_pairs)
        
        max_island = min(max(islandsize.values())+1, m*n)
        for island_list in island_pairs:
            island_sum = 1
            for island in island_list:
                island_sum += islandsize[island]
            max_island = max(max_island, island_sum)
        return max_island
                

# [1,0,0,1,1]
# [1,0,0,1,0]
# [1,1,1,1,1]
# [1,1,1,0,1]
# [0,0,0,1,0]
32518