<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 find(int i, int j,vector<int> &parent, int n){
        int k = i*n+j;
        int x = parent[k];
        if(x==k)return k;
        return parent[k] = find(x/n,x%n,parent,n);
    }
    void solve(vector<vector<int>>& grid,vector<vector<int>> &visited,vector<int> &parent,int i,int j,int par){
        int n = grid.size();
        if(i<0||j<0||i>=n||j>=n)return;
        if(visited[i][j]==1||grid[i][j]==0) return;
        visited[i][j]=1;
        parent[i*n+j] = par;
        solve(grid,visited,parent,i+1,j,par);
        solve(grid,visited,parent,i-1,j,par);
        solve(grid,visited,parent,i,j+1,par);
        solve(grid,visited,parent,i,j-1,par);
        return;  
    }
public:
    int largestIsland(vector<vector<int>>& grid) {
        int n = grid.size();
        vector<int> parent(n*n);
        int t = n*n;
        for(int i=0; i<t;i++){
            parent[i]=i;
        }
        vector<vector<int>> visited(n,vector<int>(n,0));
        for(int i=0; i<n;i++){
            for(int j=0; j<n; j++){
                if(visited[i][j]==0 &&grid[i][j]==1){
                    //visited[i][j]=1;
                    solve(grid,visited,parent,i,j,parent[i*n+j]);
                }
            }
        }
        unordered_map<int,int> hash;
        for(int i=0; i<n;i++){
            for(int j=0; j<n; j++){
                if(grid[i][j]==1){
                    int par = parent[i*n+j];
                    hash[par]++;
                }
            }
        }
        int ans =0;
        for(int i=0; i<n;i++){
            for(int j=0; j<n; j++){
                if(grid[i][j]==0){
                  int ca=0,cb=0,cc=0,cd=0;
                  int pa=-1,pb=-1,pc=-1,pd=-1;
                  if(i-1>=0){
                    if(grid[i-1][j]==1){
                        pa = parent[(i-1)*n+j];
                        ca = hash[pa];
                    }
                  }
                  if(j-1>=0){
                    if(grid[i][j-1]==1){
                        pb = parent[i*n +j-1];
                        cb = hash[pb];
                    }
                  }
                  if(i+1<n){
                    if(grid[i+1][j]==1){
                        pc = parent[(i+1)*n+j];
                        cc = hash[pc];
                    }
                  }
                  if(j+1<n){
                    if(grid[i][j+1]==1){
                        pd = parent[i*n +j+1];
                        cd = hash[pd];
                    }
                  }
                  unordered_map<int,int> h;
                  h[pa]= ca;
                  h[pb]= cb;
                  h[pc]= cc;
                  h[pd]= cd;
                    int sol=1;
                    for(auto it: h)sol+=it.second;
                    ans = max(ans,sol);
                }
            }
        }
if(ans==0)return n*n;
return ans;

    }
};














// class Solution {
//     int solve(vector<vector<int>>& grid, int i, int j,vector<vector<bool>> &visited){
//         if(i<0||j<0 ||i>=grid.size()||j>=grid[0].size())return 0;
//         if(grid[i][j]==0||visited[i][j]==1)return 0;
//         int a,b,c,d;
//         visited[i][j]=1;
//         a = 1+solve(grid,i+1,j,visited);
//         a += solve(grid,i,j+1,visited);
//         a += solve(grid,i-1,j,visited);
//         a += solve(grid,i,j-1,visited);
//         return a;
        
//     }
    
// public:
//     int largestIsland(vector<vector<int>>& grid) {
//         int n = grid.size();
//         int m = grid[0].size();
//         int ans=0,a=0;
//         for(int i=0; i<n;i++){
//             for(int j=0; j<m;j++){
//                 if(grid[i][j]==0){
//                     vector<vector<bool>> visited(n,vector<bool>(m,0));
//                     grid[i][j]=1;
//                     a   = solve(grid,i,j,visited);
//                     ans = max(ans,a);
//                     grid[i][j]=0;
//                 }
//             }
//         }
//         if(ans==0)return m*n;
//         return ans;
//     }
// };
330440