<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 {
    int n;
    vector<vector<int>> grid;
    int dir[5] = {-1, 0, 1, 0, -1};

    int dfs(int r, int c, int index) {
        int ans = 1;
        grid[r][c] = index;
        for (pair<int, int> move: neighbors(r, c)) {
            if (grid[move.first][move.second] == 1) {
                grid[move.first][move.second] = index;
                ans += dfs(move.first, move.second, index);
            }
        }

        return ans;
    }

    vector<pair<int, int>> neighbors(int r, int c) {
        vector<pair<int, int>> ans;

        for (int i=0; i<4; i++) {
            int nr = r + dir[i];
            int nc = c + dir[i+1];

            if (nr >=0 && nr < n && nc >= 0 && nc < n) {
                ans.push_back({nr, nc});
            }
        }

        return ans;
    }
public:
    int largestIsland(vector<vector<int>>& g) {
        n = g.size();
        grid = g;
        vector<int> area(n*n + 2);
        int index = 2;

        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (grid[i][j] == 1) {
                    area[index] = dfs(i, j, index);
                    index++;
                }
            }
        }

        int ans = 0;
        for (int x: area) ans = max(ans, x);

        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (grid[i][j] == 0) {
                    set<int> seen;
                    for (pair<int, int> nei: neighbors(i, j)) {
                        if (grid[nei.first][nei.second] > 1) {
                            seen.insert(grid[nei.first][nei.second]);
                        }
                    }

                    int bns = 1;
                    for (int i : seen) bns += area[i];
                    ans = max(ans, bns);
                }
            }
        }

        return ans;
    }
};
309178