<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:
    #define INF1 1e5+1
    #define INF2 1e5+2
    #define pr pair<int, int>
    int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    bool isValid(int i, int j, int n) {
        return i>=0 && j>=0 && i<n && j <n;
    }
    void dfs(vector<vector<int>>& grid, vector<vector<int>>& vis, vector<vector<pr>>& islands, int i, int j, int cnt) {
        int n = grid.size();
        vis[i][j] = 1;
        islands[cnt].push_back({i, j});
        for (int k=0; k<4; k++) {
            int x = i+dir[k][0];
            int y = j+dir[k][1];
            if (isValid(x, y, n) && !vis[x][y] && grid[x][y] == 1)
                dfs(grid, vis, islands, x, y, cnt);
        }
    }
    void bfs(vector<vector<int>>& grid, vector<vector<int>>& vis, vector<vector<pr>>& islands, int &ans) {
        int n = grid.size();
        queue<pr> q;
        // To avoid duplicates in queue
        set<pr> st;
        for (auto it : islands[0]) {
            q.push({it.first, it.second});
            st.insert({it.first, it.second});
        }
        // Multi-source BFS
        while (!q.empty()) {
            auto [i, j] = q.front(); q.pop();
            vis[i][j] = 1;
            for (int k=0; k<4; k++) {
                int x = i + dir[k][0];
                int y = j + dir[k][1];
                if (isValid(x, y, n) && !vis[x][y] && !st.count({x, y})) {
                    if (grid[x][y] == INF1) {
                        grid[x][y] = min(grid[x][y], grid[i][j] + 1);
                        q.push({x, y});
                        st.insert({x, y});
                    }
                    else if (grid[x][y] == INF2) {
                        grid[x][y] = grid[i][j] + 1;
                        ans = min(ans, grid[x][y]);
                    }
                }
            }
        }
    }
    int shortestBridge(vector<vector<int>>& grid) {
        int n = grid.size();
        // Finding islands
        vector<vector<pr>> islands(2);
        vector<vector<int>> vis(n, vector<int>(n, 0));
        int cnt = 0;
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (!vis[i][j]){
                    if (grid[i][j] == 1) {
                        dfs(grid, vis, islands, i, j, cnt);
                        cnt++;
                    }
                    else 
                        vis[i][j] = true;
                } 
            }
        }
        // All initial zeros = INF1
        for (int i=0; i<n; i++)
            for (int j=0; j<n; j++)
                if (grid[i][j] == 0)
                    grid[i][j] = INF1;
        // First island = 0, second island = INF2
        for (int i=0; i<2; i++) {
            for (auto it : islands[i]) {
                int x = it.first, y = it.second;
                grid[x][y] = i==0 ? 0 : INF2;
            }
        }
        // Resetting vis array
        for (int i=0; i<n; i++)
            for (int j=0; j<n; j++)
                vis[i][j] = 0;
        int ans = INT_MAX;
        bfs(grid, vis, islands, ans);
        return ans-1;
    }                                   // TC = O(n^2.logn) SC = O(n^2)
};  
43405