<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:
    map<pair<int,int>,int> rank;
    map<pair<int,int>,pair<int,int>> par;

    pair<int,int> find(pair<int,int> node) {
        if (node == par[node])
            return node;
        return par[node] = find(par[node]);
    }
    void unionf(pair<int,int> u,pair<int,int> v){
        pair<int,int> paru=find(u);
        pair<int,int> parv=find(v);
        if(paru==parv){return;}
        if(rank[paru]>rank[parv]){
            rank[paru]+=rank[parv];
            par[parv]=paru;
        }
        else{
            rank[parv]+=rank[paru];
            par[paru]=parv;
        }
    }

    int largestIsland(vector<vector<int>>& grid) {
        int n=grid.size();
        set<pair<int,int>> graph;

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                    
                par[{i,j}]={i,j};
                if(grid[i][j]){
                    graph.insert({i,j});
                    rank[{i,j}]=1;
                }
                else{
                    rank[{i,j}]=0;
                }
            }
        }
        if(graph.size()==0){return 1;}

        for(auto itr:graph){
            if(graph.count({itr.first+1,itr.second})){
                unionf({itr.first,itr.second},{itr.first+1,itr.second});
            }
            if(graph.count({itr.first-1,itr.second})){
                unionf({itr.first,itr.second},{itr.first-1,itr.second});
            }
            if(graph.count({itr.first,itr.second+1})){
                unionf({itr.first,itr.second},{itr.first,itr.second+1});
            }
            if(graph.count({itr.first,itr.second-1})){
                unionf({itr.first,itr.second},{itr.first,itr.second-1});
            }
        }
        // if(graph.size()==n){return n;}

        int ans=1;

        vector<vector<int>> dir={{0,1},{1,0},{-1,0},{0,-1}};

        for(auto itr:rank){
            ans=max(ans,itr.second);
        }

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                int curr=1;
                set<pair<int,int>> visited;
                if(grid[i][j]==0){
                    for(int d=0;d<4;d++){
                        int u=i+dir[d][0],v=j+dir[d][1];
                        pair<int,int> p={u,v};
                        int ranke=rank[find(p)];
                        if(u>=0 && u<n && v>=0 && v<n && grid[u][v]==1 && !visited.count(find(p))){
                              visited.insert(find(p));
                              curr+=ranke;
                        }
                    }
                }
                ans=max(ans,curr);
            }
        }
        return ans;
    }
};
333984