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

        def dfs(i, j, island_index):
            # This method paints the current island starting at cell (i,j) and all its connected neighbors and returns the
            # size of the painted island
            if not 0 <= i < n or not 0 <= j < n or not grid[i][j] or (i, j) in visited:
                return 0
            grid[i][j] = island_index
            visited.add((i, j))
            return 1 + dfs(i - 1, j, island_index) + dfs(i + 1, j, island_index) + dfs(i, j - 1, island_index) + \
                   dfs(i, j + 1, island_index)

        n = len(grid)
        res, visited = 0, set()
        island_area = defaultdict(int)  # {color: area}: Area of island painted of that color
        island_index = 2  # 0 and 1 are already used in grid, hence we start color index from 2
        for i in range(n):
            for j in range(n):
                if grid[i][j]:
                    island_area[island_index] = dfs(i, j, island_index)
                    island_index += 1
        for i in range(n):
            for j in range(n):
                if not grid[i][j]:
                    # We get unique color indices to avoid calculating the size of 2 connected components twice (it can
                    # happen that for example left neighbor and upper neighbor are the same island)
                    indices = set()
                    for x, y in (i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1):
                        if 0 <= x < n and 0 <= y < n:
                            indices.add(grid[x][y])
                    sum_areas = sum(island_area[index] for index in indices)
                    res = max(res, 1 + sum_areas)  # +1 to account for the flipped 0
        return res if res != 0 else n * n
        
        # def dfs(i, j, island_index):
        #     if not 0 <= i < n or not 0 <= j < n or not grid[i][j] or (i, j) in visited:
        #         return 0
        #     visited.add((i, j))
        #     island_map[(i, j)] = island_index
        #     area = 1
        #     for x, y in (i-1, j), (i+1, j), (i, j-1), (i, j+1):
        #         area += dfs(x, y, island_index)
        #     return area 
        
        # n = len(grid)
        # island_map = dict()
        # island_area = dict()
        # island_index = 1
        # visited = set()
        # for i in range(n):
        #     for j in range(n):
        #         if grid[i][j] == 1:
        #             area = dfs(i, j, island_index)
        #             island_area[island_index] = area
        #             island_index += 1
        # print(island_map)
        # print(island_area)
        # res = 0
        # for i in range(n):
        #     for j in range(n):
        #         if grid[i][j] == 0:
        #             surrounding_islands_indices = set()
        #             for x, y in (i-1, j) , (i+1, j), (i, j-1), (i, j+1):
        #                 if 0 <= x < n and 0 <= y < n and grid[x][y]:
        #                     surrounding_islands_indices.add(island_map[(x, y)])
        #             surrounding_islands_areas = 0
        #             for index in surrounding_islands_indices:
        #                 surrounding_islands_areas += island_area[index]
        #             res = max(res, 1 + surrounding_islands_areas)
        # return res if res > 0 else n * n
        
78831