<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 collections
from typing import List


class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        rows = len(grid)
        cols = len(grid[0])

        max_area = 0

        # test max_area if converted to land
        water = []

        # (row, col): island_index
        cell_map = {}

        # island_index: area
        area_map = {}

        def bfs(island_index, row, col):
            queue = collections.deque()
            queue.append((row, col))

            cell_map[(row, col)] = island_index

            area = 1

            while queue:
                r, c = queue.popleft()

                directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
                for dr, dc in directions:
                    new_row = r + dr
                    new_col = c + dc

                    if (
                        0 <= new_row < rows
                        and 0 <= new_col < cols
                        and grid[new_row][new_col] == 1
                        and (new_row, new_col) not in cell_map
                    ):
                        queue.append((new_row, new_col))
                        cell_map[(new_row, new_col)] = island_index
                        area += 1

            area_map[island_index] = area

            return area

        island_index = 0

        for i in range(rows):
            for j in range(cols):
                if grid[i][j] == 0:
                    water.append((i, j))
                else:
                    if (i, j) not in cell_map:
                        max_area = max(max_area, bfs(island_index, i, j))

                        island_index += 1

        for row, col in water:
            islands_connected_to_cell = set()

            directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
            for dr, dc in directions:
                new_row = row + dr
                new_col = col + dc

                if (new_row, new_col) in cell_map:
                    islands_connected_to_cell.add(cell_map[(new_row, new_col)])

            # water turned into land
            area = 1

            for island_connected_to_cell in islands_connected_to_cell:
                area += area_map[island_connected_to_cell]

            max_area = max(max_area, area)

        return max_area
53181