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

from queue import Queue

class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        paint = 3
        n = len(grid)
        island_sizes = []
        connections = dict()

        # 0: ocean
        # 1: uncolored land
        # 2k: coast of land colored 2k + 1


        for y in range(n):
            for x in range(n):
                if grid[y][x] != 1:
                    continue

                q = Queue()
                q.put((y,x))
                island_size = 0
                while not q.empty():
                    row, col = q.get()
                    cell_color = grid[row][col]
                    if cell_color == paint or cell_color == paint - 1:
                        continue

                    if cell_color == 0:
                        grid[row][col] = paint - 1
                        continue
                    
                    if cell_color != paint - 1 and cell_color % 2 == 0:
                        if (row, col) in connections:
                            connections[(row, col)].add(paint)
                        else:
                            connections[(row, col)] = {paint, cell_color + 1}
                        continue

                    grid[row][col] = paint
                    island_size += 1

                    for offset_y, offset_x in ((-1, 0), (0, -1), (1, 0), (0, 1)):
                        new_y, new_x = row + offset_y, col + offset_x
                        if 0 <= new_y < n and 0 <= new_x < n:
                            if grid[new_y][new_x] != paint:
                                q.put((new_y, new_x))

                island_sizes.append(island_size)
                paint += 2          

        island_groups = {tuple(value) for value in connections.values()}
        island_groups.update({(x,) for x in range(3, paint, 2)})

        if len(island_sizes) == 1 and island_sizes[0] == n * n:
            return island_sizes[0]

        if len(island_groups) == 0:
            return 1

        return max(sum(island_sizes[(island - 3) // 2]for island in island_group) for island_group in island_groups) + 1
                    



                

55318