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


def compute_sum(grid, i, j, id_grid):
    n, m = len(grid), len(grid[0])
    visited = set()
    result = 1
    for x, y in get_neighbor(grid, i, j):
        if(x not in range(n) or y not in range(m)):
            continue
        if(id_grid[x][y] in visited):
            continue
        visited.add(id_grid[x][y])
        result += grid[x][y]
    return result

def get_neighbor(grid, x, y):
    return [
        [x+1, y],
        [x-1, y],
        [x, y+1],
        [x, y-1]
    ]

def dfs_count(grid, x, y, visited, id_grid, latest_id):
    n, m = len(grid), len(grid[0])
    if(x not in range(n) or y not in range(m)):
        return 0
    key = (x,y)
    if(key in visited):
        return 0
    if(grid[x][y] == 0):
        return 0
    id_grid[x][y] = latest_id
    count = 1
    visited.add(key)
    for i, j in get_neighbor(grid, x, y):
        count += dfs_count(grid, i, j, visited, id_grid, latest_id)
    return count

class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        """
        (2,1),0
        (2,1),0

        {
            (0,0): 2
        }
        
        """
        n, m = len(grid), len(grid[0])
        visited = set()
        count_id_map = {0: 0}
        latest_id = 1
        # count elems, and assign IDs to other matrix
        id_grid = [[0 for i in range(m)] for j in range(m)]
        for i in range(n):
            for j in range(m):
                if(grid[i][j] != 0):
                    result = dfs_count(grid, i, j, visited, id_grid, latest_id)
                    count_id_map[latest_id] = result
                    latest_id += 1
        # iterate over second matrix, and assign count to first matrix
        for i in range(n):
            for j in range(m):
                grid[i][j] = count_id_map[id_grid[i][j]]
        # for all zeroes, sum the 4 corners, and store max sum
        max_sum = 0
        for i in range(n):
            for j in range(m):
                if(grid[i][j] == 0):
                    result = compute_sum(grid, i, j, id_grid)
                    max_sum = max(max_sum, result)
                else:
                    max_sum = max(max_sum, grid[i][j])
        return max_sum
        
51043