<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>

from collections import deque
class Solution:
    def shortestBridge(self, grid: List[List[int]]) -> int:
        adjacent = [(1,0), (-1,0), (0,1), (0,-1)]
        n = len(grid)
        # find a point to do bfs from
        
        def find_one(grid, n):
            for i in range(n):
                for j in range(n):
                    if grid[j][i] == 1:
                        return i, j

        i, j = find_one(grid, n)

        
        def bfs_island(i, j, visited):
            q = deque()
            q.append((i, j))
            visited.add((i, j))

            while q:
                i, j = q.popleft()
                for adj in adjacent:
                    temp_j = j + adj[1]
                    temp_i = i + adj[0]
                    if 0 <= temp_j < n and 0 <= temp_i < n and (temp_i, temp_j) not in visited:
                        if grid[temp_j][temp_i] == 1:
                            visited.add((temp_i, temp_j))
                            q.append((temp_i,temp_j))

        def bfs_shortest(island):
            visited = set()
            flipped = 0
            new_q_size = len(island)
            q = deque()
            q_size = len(island)
            for coord in island:
                q.append(coord)
                visited.add(coord)
            
            while q:
                print(visited)
                flipped += 1
                q_size = new_q_size
                new_q_size = 0
                while q_size > 0:
                    i, j = q.popleft()
                    q_size -= 1
                    for adj in adjacent:
                        temp_j = j + adj[1]
                        temp_i = i + adj[0]
                        if 0 <= temp_j < n and 0 <= temp_i < n and (temp_i, temp_j) not in visited:
                            if grid[temp_j][temp_i] == 0:
                                visited.add((temp_i, temp_j))
                                q.append((temp_i,temp_j))
                                new_q_size += 1
                            if grid[temp_j][temp_i] == 1:
                                return flipped

        


        island = set()
        bfs_island(i, j, island)
        return bfs_shortest(island) - 1
                            
                    


18100