<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:
        
        # get all islands
        # create set of "boundaries" (adjacent blocks that could be added)
        # for each pair of islands with overlapping boundary, find greatest
        # problem: 1 swap can make multiple islands join together
        # for each "boundary" track the total size of adjacent islands

        island_lookup = {}
        adjacents_lookup = {}
        islands = []

        for i, row in enumerate(grid):
            for j, v in enumerate(row):
                if (i, j) in island_lookup:
                    continue
                
                if v != 1:
                    continue

                q = deque()

                island = set()
                adjacents = set()

                q.append((i, j))

                while len(q) > 0:
                    x, y = q.popleft()

                    if (x, y) in island:
                        continue

                    if x + 1 < len(grid):
                        if grid[x + 1][y] == 1:
                            q.append((x + 1, y))
                        else:
                            adjacents.add((x + 1, y))

                    if x - 1 >= 0:
                        if grid[x - 1][y] == 1:
                            q.append((x - 1, y))
                        else:
                            adjacents.add((x - 1, y))

                    if y + 1 < len(grid[0]):
                        if grid[x][y + 1] == 1:
                            q.append((x, y + 1))
                        else:
                            adjacents.add((x, y + 1))

                    if y - 1 >= 0:
                        if grid[x][y - 1] == 1:
                            q.append((x, y - 1))
                        else:
                            adjacents.add((x, y - 1))

                    island_lookup[x, y] = island
                    island.add((x, y))
                
                for (x, y) in adjacents:
                    if (x, y) not in adjacents_lookup:
                        adjacents_lookup[x, y] = 1

                    adjacents_lookup[x, y] += len(island)

                islands.append(island)
        
        if len(islands) == 0:
            return 1

        return max(list(adjacents_lookup.values()) + [len(i) for i in islands])
        # print(adjacents_lookup)



67431