<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 shortestBridge(vector<vector<int>>& grid) {

        int N = grid.size();
        int M = grid[0].size();

        vector<vector<int>> visited(N, vector<int>(M, 0));

        queue<pair<int, pair<int, int>>> mQ;

        bool stop = false;
        for (int i = 0; i < N; i++) {
            if (stop) break;
            for (int j = 0; j < M; j++) {
                if (grid[i][j] == 1) {
                    dfs(grid, i, j, mQ, visited);
                    stop = true;
                    break;
                }
            }
        }

        vector<int> mvr = {-1, 1, 0, 0};
        vector<int> mvc = {0, 0, -1, 1};
        int ans = numeric_limits<int>::max();

        while (!mQ.empty()) {
            pair<int, pair<int, int>> cur = mQ.front();
            mQ.pop();
            int curR = cur.second.first;
            int curC = cur.second.second;
            int curDis = cur.first;
            for (int i = 0; i < 4; i++) {
                int nr = curR + mvr[i];
                int nc = curC + mvc[i];
                if (nr >= 0 && nr < N && nc >= 0 && nc < M &&
                    visited[nr][nc] == 0) {
                    if (grid[nr][nc] == 0) {
                        mQ.push({curDis + 1, {nr, nc}});
                        visited[nr][nc] = 1;
                    } else {
                        ans = min(ans, curDis);
                    }
                }
            }
        }

        return ans;
    }

    	void dfs(vector<vector<int>>& arr, int r, int c,
		queue<pair<int, pair<int, int>>>& mQ, vector<vector<int>>& visited) {
		
		int N = arr.size();
		int M = arr[0].size();

		if (r < 0 || r >= N || c < 0 || c >= M || visited[r][c] == 1 || arr[r][c] == 0) 
			return;

		mQ.push({ 0, {r, c} });
		visited[r][c] = 1;

		vector<int> mvr = { -1, 1, 0, 0 };
		vector<int> mvc = { 0, 0, -1, 1 };
		for (int i = 0; i < 4; i++) {
			int nr = r + mvr[i];
			int nc = c + mvc[i];
			dfs(arr, nr, nc, mQ, visited);
		}

		return;
	}
};
45445