<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 {
public:
    // Function to perform DFS and calculate the area of an island
    int dfs(vector<vector<int>>& grid, pair<int, int> p, int idx, vector<vector<bool>>& visited) {
        stack<pair<int, int>> s;
        s.push(p);
        int area = 0;
        int n = grid.size();
        
        while (!s.empty()) {
            auto [i, j] = s.top();
            s.pop();
            
            if (visited[i][j]) continue;

            visited[i][j] = true;
            ++area;
            grid[i][j] = idx;

            if (i > 0 && grid[i - 1][j] == 1 && !visited[i - 1][j]) s.push({i - 1, j});
            if (j > 0 && grid[i][j - 1] == 1 && !visited[i][j - 1]) s.push({i, j - 1});
            if (i < n - 1 && grid[i + 1][j] == 1 && !visited[i + 1][j]) s.push({i + 1, j});
            if (j < n - 1 && grid[i][j + 1] == 1 && !visited[i][j + 1]) s.push({i, j + 1});
        }
        return area;
    }

    int largestIsland(vector<vector<int>>& grid) {
        int n = grid.size();
        unordered_map<int, int> area;
        area[0] = 0; // For cells that are water

        vector<vector<bool>> visited(n, vector<bool>(n, false));
        int idx = 2; // Start index for labeling islands

        // First pass: label each island with a unique index and calculate their areas
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == 1 && !visited[i][j]) {
                    area[idx] = dfs(grid, {i, j}, idx, visited);
                    ++idx;
                }
            }
        }

        int maxIsland = 0;
        // Second pass: find the largest island we can create by changing one 0 to 1
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == 0) {
                    set<int> uniqueIslands;
                    int potentialArea = 1; // Change current 0 to 1

                    if (i > 0) uniqueIslands.insert(grid[i - 1][j]);
                    if (j > 0) uniqueIslands.insert(grid[i][j - 1]);
                    if (i < n - 1) uniqueIslands.insert(grid[i + 1][j]);
                    if (j < n - 1) uniqueIslands.insert(grid[i][j + 1]);

                    for (int islandIdx : uniqueIslands) {
                        potentialArea += area[islandIdx];
                    }
                    maxIsland = max(maxIsland, potentialArea);
                } else {
                    // If it's already land, check if it's the largest island so far
                    maxIsland = max(maxIsland, area[grid[i][j]]);
                }
            }
        }
        return maxIsland;
    }
};

217040