<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:
    def shortestBridge(self, grid: List[List[int]]) -> int:
        COL = len(grid)
        ROW = len(grid[0])

        island1 = None
        for i in range(COL):
            for j in range(ROW):
                if grid[i][j]==1:
                    island1 = (i,j)
                    break
            if island1:
                break

        print("island1", island1)
        queue = []
        queue.append(island1)
        visit = set()
        res = 0

        island1_queue = []
        island1_queue.append(island1)

        while island1_queue:
            x, y = island1_queue.pop()
            for cur_x, cur_y in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
                if 0 <= cur_x < COL and 0 <= cur_y < ROW and grid[cur_x][cur_y] == 1:
                    grid[cur_x][cur_y]=2
                    island1_queue.append((cur_x, cur_y))
                    queue.append((cur_x, cur_y))

        print(queue)
        while queue:
            res += 1
            size = len(queue)
            print("queue", queue)
            for s in range(size):
                (i, j) = queue.pop(0)
                if (i, j) in visit:
                    continue
                visit.add((i,j))

                for ni, nj in [(i+1, j), (i-1, j), (i,j+1), (i,j-1)]:
                    if ni not in range(COL) or nj not in range(ROW) or (ni,nj) in visit or grid[ni][nj]==2:
                        continue
                    if grid[ni][nj]==1:
                        return res-1
                    queue.append((ni,nj))

        return -1





'''
    def shortestBridge(self, grid: List[List[int]]) -> int:
        N = len(grid)

        def _getNeighbors(i, j):
            for ni, nj in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
                if ni in range(N) and nj in range(N):
                    yield ni, nj

        # Find a piece of an island
        start = None
        for i in range(N):
            for j in range(N):
                if grid[i][j] == 1:
                    start = (i, j)
                    break
            if start:
                break

        # BFS to find perimeter around one island
        queue = deque([start])

        visit = set()
        water = set()
        while queue:
            i, j = queue.popleft()
            #visit or not
            if (i, j) in visit:
                continue
            visit.add((i, j))

            # check neighbors
            for ni, nj in _getNeighbors(i, j):
                if grid[ni][nj] == 0:
                    water.add((ni, nj))
                else:
                    queue.append((ni, nj))

        # BFS from the perimeter out, until the other island is reached
        queue = deque(water)
        res = 0
        while queue:
            #size of queue -> same level
            for _ in range(len(queue)):
                i, j = queue.popleft()
                if grid[i][j] == 1:
                    return res
                for ni, nj in _getNeighbors(i, j):
                    #already visit skips
                    if (ni, nj) in visit:
                        continue
                    visit.add((ni, nj))
                    #put next level 
                    queue.append((ni, nj))
            res += 1

        return res
'''
17800