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

        rows = len(_grid)
        cols = len(_grid[0])

        def get_points(x, y):
            for dx, dy in ((-1, 0), (0, -1), (0, 1), (1, 0)):
                xx, yy = x + dx, y + dy
                if not (0 <= xx < rows) or not (0 <= yy < cols):
                    continue
                yield xx, yy

        def dfs(x, y, points, grid):
            assert grid[x][y] == 1
            grid[x][y] = 0
            points.add((x, y))
            for xx, yy in get_points(x, y):
                if grid[xx][yy] != 1:
                    continue
                dfs(xx, yy, points, grid)
        
        max_size = 0

        grid = deepcopy(_grid)
        zeroes = set()

        ii = iter(range(1000000))
        size_by_id = {}
        id_by_point = {}
        
        for i in range(rows):
            for j in range(cols):
                ch = grid[i][j]
                if ch == 1:
                    points = set()
                    dfs(i, j, points, grid)
                    next_id = next(ii)
                    for x, y in points:
                        id_by_point[(x, y)] = next_id
                    size_by_id[next_id] = len(points)
                    max_size = max(max_size, len(points))
                if _grid[i][j] == 0:
                    zeroes.add((i, j))
        
        for i, j in zeroes:
            island_ids = set()
            for xx, yy in get_points(i, j):
                if (is_id := id_by_point.get((xx, yy))) is not None:
                    island_ids.add(is_id)
            max_size = max(max_size, 1 + sum(size_by_id[isle] for isle in island_ids))
        
        return max_size
                

59593