<p>You are given an <code>n x n</code> binary matrix <code>grid</code> where <code>1</code> represents land and <code>0</code> represents water.</p>

<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>&#39;s not connected to any other <code>1</code>&#39;s. There are <strong>exactly two islands</strong> in <code>grid</code>.</p>

<p>You may change <code>0</code>&#39;s to <code>1</code>&#39;s to connect the two islands to form <strong>one island</strong>.</p>

<p>Return <em>the smallest number of </em><code>0</code><em>&#39;s you must flip to connect the two islands</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,1],[1,0]]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,1,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length == grid[i].length</code></li>
	<li><code>2 &lt;= n &lt;= 100</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
	<li>There are exactly two islands in <code>grid</code>.</li>
</ul>

class Solution {
public:

    int vis[101][101];
    
    void dfs(vector<vector<int>>& grid,vector<vector<int>>& cgrid,int color,int r,int c)
    {
        int n=grid.size();
        if(r<0 || r>=n || c<0 || c>=n || vis[r][c]==1 || grid[r][c]==0 || cgrid[r][c]!=0)
        {
            return;
        }
        vis[r][c]=1;
        cgrid[r][c]=color;
        int dx[]={-1,0,1,0};
        int dy[]={0,1,0,-1};
        vector<pair<int,int>> neigh;
        for(int i=0;i<4;i++)
        {
            int x=dx[i]+r;
            int y=dy[i]+c;
            if(x>=0 && x<n && y>=0 && y<n && vis[x][y]!=1 && grid[x][y]!=0 && cgrid[x][y]==0)
            {
                neigh.push_back({x,y});
            }
        }
        for(auto i:neigh)
        {
            dfs(grid,cgrid,color,i.first,i.second);
        }
        return;
    }

    int bfs(vector<vector<int>>& cgrid,queue<pair<int,int>> q,vector<vector<int>>& dis)
    {
        int n=cgrid.size();
        int ans=INT_MAX;
        while(!q.empty())
        {
            pair<int,int> node=q.front();
            q.pop();
            if(vis[node.first][node.second]==1)
            {
                continue;
            }
            vis[node.first][node.second]=1;
            int dx[]={-1,0,1,0};
            int dy[]={0,1,0,-1};
            vector<pair<int,int>> neigh;
            for(int i=0;i<4;i++)
            {
                int x=dx[i]+node.first;
                int y=dy[i]+node.second;
                if(x>=0 && x<n && y>=0 && y<n && cgrid[x][y]!=1 && vis[x][y]!=1)
                {
                    neigh.push_back({x,y});
                }
            }
            for(auto i:neigh)
            {
                if(dis[i.first][i.second]>dis[node.first][node.second]+1)
                {
                    dis[i.first][i.second]=dis[node.first][node.second]+1;
                    q.push(i);
                    if(cgrid[i.first][i.second]==2)
                    {
                        ans=min(ans,dis[i.first][i.second]);
                    }
                }
            }
        }
        return ans;
    }



    int shortestBridge(vector<vector<int>>& grid) {
        int n=grid.size();
        memset(vis,-1,sizeof(vis));
        vector<vector<int>> cgrid(n,vector<int>(n,0));
        int col=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(vis[i][j]!=1 && grid[i][j]==1)
                {
                    dfs(grid,cgrid,col,i,j);
                    col++;
                }
            }
        }
        queue<pair<int,int>> q;
        memset(vis,-1,sizeof(vis));
        vector<vector<int>> dis(n,vector<int>(n,1e9));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(cgrid[i][j]==1)
                {
                    q.push({i,j});
                    dis[i][j]=0;
                }
            }
        }
        cout<<q.size()<<endl;
        int ans=bfs(cgrid,q,dis);
        return ans-1;
    }
};
50545