<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 check if a cell is within bounds
    bool inBounds(int i, int j, int n) {
        return i >= 0 && j >= 0 && i < n && j < n;
    }

    // DFS to mark the island and calculate its size
    int dfs(int i, int j, vector<vector<int>>& grid, vector<vector<int>>& component, int id, int n) {
        if (!inBounds(i, j, n) || grid[i][j] == 0 || component[i][j] != -1) return 0;
        component[i][j] = id;  // Mark the component with the current id
        int size = 1;
        // Explore all 4 directions
        size += dfs(i + 1, j, grid, component, id, n);
        size += dfs(i - 1, j, grid, component, id, n);
        size += dfs(i, j + 1, grid, component, id, n);
        size += dfs(i, j - 1, grid, component, id, n);
        return size;
    }

    int largestIsland(vector<vector<int>>& grid) {
        int n = grid.size();
        vector<vector<int>> component(n, vector<int>(n, -1)); // To track components
        unordered_map<int, int> componentSize; // To store the size of each component
        int id = 0; // Component ID

        // Step 1: Use DFS to find all islands and store their sizes
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1 && component[i][j] == -1) {
                    int size = dfs(i, j, grid, component, id, n);
                    componentSize[id++] = size;
                }
            }
        }

        // Step 2: Find the maximum size of any island
        int maxIslandSize = 0;
        for (auto& entry : componentSize) {
            maxIslandSize = max(maxIslandSize, entry.second);
        }

        // Step 3: Try flipping each 0 and check the size of the resulting island
        vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    unordered_set<int> neighboringComponents;
                    int newSize = 1; // Starting size after flipping this 0 to 1

                    // Check the 4 neighbors of this 0
                    for (auto& dir : directions) {
                        int ni = i + dir.first, nj = j + dir.second;
                        if (inBounds(ni, nj, n) && grid[ni][nj] == 1) {
                            int neighborComponent = component[ni][nj];
                            if (neighboringComponents.count(neighborComponent) == 0) {
                                neighboringComponents.insert(neighborComponent);
                                newSize += componentSize[neighborComponent]; // Add the size of the neighbor component
                            }
                        }
                    }
                    maxIslandSize = max(maxIslandSize, newSize);
                }
            }
        }

        // Step 4: If no 0 was flipped, return the size of the largest original island
        return maxIslandSize == 0 ? n * n : maxIslandSize;
    }
};

160340