<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:
        n, m = len(grid), len(grid[0])
        max_area = 0
        max_areas = [[0] * m for _ in range(n)]
        distincts = [[-1] * m for _ in range(n)]
        dist_counter = 0
        visited = [[0] * m for _ in range(n)]  # use for in outer to avoid using same refs.
        def valid_moves(i, j) -> tuple[int, int]:
            for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                x, y = i + di, j + dj
                if 0 <= x < n and 0 <= y < m and grid[x][y] and not visited[x][y]:
                    yield x, y

        all_coords = list(product(range(n), range(m)))
        for r, c in all_coords:
            if not grid[r][c]:  # not island
                continue
            if visited[r][c]:  # already done
                continue
            # Starting a new Island.
            stack = [(r, c)]
            curr_area = 0
            max_area = 0
            local_visited = set()
            while stack:
                i, j = stack.pop()
                if visited[i][j]: continue
                curr_area += 1
                visited[i][j] = 1
                local_visited.add((i, j))
                stack.extend(valid_moves(i, j))
                max_area = max(max_area, curr_area)
            for x, y in local_visited:
                max_areas[x][y] = max_area
                distincts[x][y] = dist_counter
                visited[x][y] = 1
            dist_counter += 1
        
        for r, c in all_coords:
            if not grid[r][c]:
                islands_seen = set()
                area = 0
                for dr, dc in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                    x, y = r + dr, c + dc
                    if 0 <= x < n and 0 <= y < m:
                        dist = distincts[x][y]
                        if dist not in islands_seen:
                            print('hi')
                            area += max_areas[x][y]
                            islands_seen.add(dist)
                max_area = max(area + 1, max_area)
        return max_area

            
44631