<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 dataclasses import dataclass

@dataclass
class Island:
    id: int
    area: int

directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]

class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        n = len(grid)
        islands = {}
        cur_id = 0
        for x in range(n):
            for y in range(n):
                if grid[x][y] == 1 and f"{x}_{y}" not in islands:
                    self.visitIsland(grid, x, y, islands, cur_id)
                    cur_id += 1
        
        max_area = 0
        for island in islands.values():
            max_area = max(max_area, island.area)
        for x in range(n):
            for y in range(n):
                if grid[x][y] == 1:
                    continue
                cur_area = 1
                seen_ids = set()
                # try to connect to adjcent islands
                for d in directions:
                    nx = x + d[0]
                    ny = y + d[1]
                    if nx < 0 or nx >= n or ny < 0 or ny >= n:
                        continue
                    if f"{nx}_{ny}" not in islands:
                        continue
                    island = islands[f"{nx}_{ny}"]
                    if island.id in seen_ids:
                        continue
                    cur_area += island.area
                    seen_ids.add(island.id)

                if cur_area > max_area:
                    max_area = cur_area
        return max_area
    
    # visit the island from the start point (x, y) and cal the area
    def visitIsland(self, grid: List[List[int]], x: int, y: int, islands: dict, id: int):
        n = len(grid)
        island = Island(id, 0)
        queue = [(x, y), ]
        islands[f"{x}_{y}"] = island
        while queue:
            next_level = []
            for x, y in queue:
                island.area += 1
                for d in directions:
                    nx = x + d[0]
                    ny = y + d[1]
                    if nx < 0 or nx >= n or ny < 0 or ny >= n:
                        continue
                    if grid[nx][ny] != 1 or f"{nx}_{ny}" in islands:
                        continue
                    next_level.append((nx, ny))
                    islands[f"{nx}_{ny}"] = island
            queue = next_level

        
39643