<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 DSU {
private:
    vector<int> parent;
    vector<int> size;
    
public:
    DSU(int n) : parent(n), size(n, 1) {
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
    }
    
    int find(int u) {
        if (parent[u] != u) {
            parent[u] = find(parent[u]);  // Path compression
        }
        return parent[u];
    }
    
    bool unionSets(int u, int v) {
        int rootU = find(u);
        int rootV = find(v);
        
        if (rootU != rootV) {
            if (size[rootU] < size[rootV]) swap(rootU, rootV);
            parent[rootV] = rootU;
            size[rootU] += size[rootV];
            return true;
        }
        return false;
    }
    
    int getSize(int u) {
        return size[find(u)];
    }
};

class Solution {
public:
    int largestIsland(vector<vector<int>>& grid) {
        int n = grid.size();
        DSU dsu(n * n);
        vector<vector<int>> componentId(n, vector<int>(n, -1));
        unordered_map<int, int> componentSize;
        int componentIndex = 0;
        
        // Step 1: Initialize DSU for each cell with 1 and calculate component sizes
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == 1) {
                    int current = i * n + j;
                    if (i > 0 && grid[i-1][j] == 1) {
                        dsu.unionSets(current, (i-1) * n + j);
                    }
                    if (j > 0 && grid[i][j-1] == 1) {
                        dsu.unionSets(current, i * n + (j-1));
                    }
                }
            }
        }
        
        // Assign component IDs and calculate sizes
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == 1) {
                    int id = dsu.find(i * n + j);
                    componentId[i][j] = id;
                    componentSize[id] = dsu.getSize(id);
                }
            }
        }
        
        // Step 2: Find the maximum island size with one `0` flipped
        int maxIslandSize = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == 0) {
                    unordered_set<int> seenComponents;
                    int potentialSize = 1;  // Start with size 1 for the new cell
                    
                    // Check all 4 directions
                    for (int k = 0; k < 4; ++k) {
                        int newX = i + directions[k][0];
                        int newY = j + directions[k][1];
                        
                        if (newX >= 0 && newX < n && newY >= 0 && newY < n && grid[newX][newY] == 1) {
                            int compId = dsu.find(newX * n + newY);
                            if (seenComponents.find(compId) == seenComponents.end()) {
                                potentialSize += componentSize[compId];
                                seenComponents.insert(compId);
                            }
                        }
                    }
                    
                    // Update the maximum possible island size
                    maxIslandSize = max(maxIslandSize, potentialSize);
                }
            }
        }
        
        // If all cells are 1s, return the size of the grid
        if (maxIslandSize == 0) {
            return n * n;
        }
        
        return maxIslandSize;
    }
    
private:
    int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
};


170971