<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 collections import deque


class Solution:
    def bfs(self, grid: List[List[int]], r: int, c: int):
        current_id = len(self.component_sizes)
        current_size = 0
        q = deque([(r, c)])
        self.seen.add((r, c))
        while q:
            R, C = q.popleft()
            current_size += 1
            self.component_id[R][C] = current_id
            neighbours = [(R-1,C), (R+1, C), (R,C-1), (R,C+1)]
            for R1, C1 in neighbours:
                if (
                    0 <= R1 < self.n and 0 <= C1 < self.n and
                    (R1, C1) not in self.seen and
                    grid[R1][C1] == 1
                ):
                    q.append((R1, C1))
                    self.seen.add((R1, C1))
        self.component_sizes.append(current_size)
    
    def largestIsland(self, grid: List[List[int]]) -> int:
        self.n = len(grid)
        self.component_id = [[-1]*self.n for _ in range(self.n)]
        self.component_sizes = []
        self.seen = set()
        for r in range(self.n):
            for c in range(self.n):
                if (r, c) not in self.seen and grid[r][c] == 1:
                    self.bfs(grid, r, c)
        if len(self.component_sizes) == 1 and self.component_sizes[0] == self.n * self.n:
            return self.n * self.n
        # there is at least one 0 in the grid
        max_size = 0
        for r in range(self.n):
            for c in range(self.n):
                if grid[r][c] == 0:
                    neighbours = [(r-1,c), (r+1,c), (r,c-1), (r,c+1)]
                    adj_comps = {}
                    for r1, c1 in neighbours:
                        if 0 <= r1 < self.n and 0 <= c1 < self.n and grid[r1][c1] == 1:
                            comp_id = self.component_id[r1][c1]
                            adj_comps[comp_id] = self.component_sizes[comp_id]
                    max_size = max(max_size, sum(adj_comps.values()) + 1)
        return max_size
        
39643