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

# find the islands first with dfs and dictionary
# then, check those zero cells for larger connections. 

# 1. first, DFS to search for the islands, and mark them with index, and computed this size, place the results in a dictionary; 
# 2. go through all empty cells, to see if it can conncet to larger island. 
# 3. if there is no chance to connect any island, return the largest island. 


class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        N = len(grid)

        # we go throught the neighourhood twice, so here we use a dedicate method
        def next_cell(x, y):
            for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                if 0 <= x + dx < N and 0 <= y + dy < N:
                    yield x + dx, y + dy

        def dfs(x, y, index): # the grid within an island filled with the area index
            res = 1
            grid[x][y] = index  # mark the cell with index for further query
            for i, j in next_cell(x, y):
                if grid[i][j] == 1:
                    res += dfs(i, j, index)
            return res

        # DFS every island and give it an index of island and its size through DFS
        index = 2 # index should be >= 2 to indicate it has been visited.  1 means it hasn't been visited yet
        areas = {0:0}  # island_index : island size,  padding, to make sure the result exist
        #areas = dict()  # why we padding 0:0, because during the neibhour exploration, we have zero cells 
        for x in range(N):
            for y in range(N):
                if grid[x][y] == 1:
                    areas[index] = dfs(x, y, index)
                    index += 1

        # traverse every 1 cell and count biggest island it can conntect
        res = max(areas.values()) if areas else 0 
        # if there is no chance to connect any two island, we will return the largest singular island
        
        for x in range(N):
            for y in range(N):
                if grid[x][y] == 0:
                    neighb_idx = set(grid[i][j] for i, j in next_cell(x, y) if grid[i][j]) # get all the neighbour island index
                    res = max(res, sum(areas[index] for index in neighb_idx) + 1) # include the 0 cell at this point
                    
        return res
24681