<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:
        self.m = len(grid)
        self.n = len(grid[0])
        self.grid = grid
        self.island = 0
        self.ans = 0
        def flood(r, c):
            island = [(r, c)]
            to_visit = [(r, c)]
            while to_visit:
                r, c = to_visit.pop()
                for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
                    if r + x >= 0 and r + x < self.m and c + y >= 0 and c + y < self.n and self.grid[r+x][c+y] == 1:
                        island.append((r+x, c+y))
                        to_visit.append((r+x, c+y))
                        self.grid[r+x][c+y] = -1
            size = len(island)
            self.ans = max(self.ans, size)
            for r, c in island:
                self.grid[r][c] = (size, self.island)
        
        for i in range(self.m):
            for j in range(self.n):
                if grid[i][j] == 1:
                    self.island += 1
                    grid[i][j] = -1
                    flood(i, j)

        for i in range(self.m):
            for j in range(self.n):
                if grid[i][j] == 0:
                    max_size = 1
                    seen = set()
                    if i - 1 >= 0 and grid[i-1][j] != 0:
                        max_size += grid[i-1][j][0]
                        seen.add(grid[i-1][j][1])
                    if i + 1 < self.m and grid[i+1][j] != 0 and grid[i+1][j][1] not in seen:
                        max_size += grid[i+1][j][0]
                        seen.add(grid[i+1][j][1])
                    if j - 1 >= 0 and grid[i][j-1] != 0 and grid[i][j-1][1] not in seen:
                        max_size += grid[i][j-1][0]
                        seen.add(grid[i][j-1][1])
                    if j + 1 < self.n and grid[i][j+1] != 0 and grid[i][j+1][1] not in seen:
                        max_size += grid[i][j+1][0]
                    self.ans = max(self.ans, max_size)
        return self.ans
31806