human
I found an interesting problem on https://leetcode.com/problems/shortest-bridge/:
In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.)
Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.
Return the smallest number of 0s that must be flipped.  (It is guaranteed that the answer is at least 1.)
 
Example 1:
Input: A = [[0,1],[1,0]]
Output: 1
Example 2:
Input: A = [[0,1,0],[0,0,0],[0,0,1]]
Output: 2
Example 3:
Input: A = [[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]]
Output: 1

 
Constraints:

2 <= A.length == A[0].length <= 100
A[i][j] == 0 or A[i][j] == 1

I tried it in Python, but could not do it. Can you solve it?
assistant
#Here is the Python code you requested.

class Solution:
    def shortestBridge(self, A: List[List[int]]) -> int:
        
        count=0
        borders = collections.defaultdict(list)
        seen = set()
        x,y = [1,-1,0,0], [0,0,1,-1]
        m,n = len(A), len(A[0])
        for i in range(m):
            for j in range(n):
                if A[i][j] == 1 and (i,j) not in seen:
                    frontier = [(i,j)]
                    island = {(i,j)}
                    while frontier:
                        cur = frontier.pop()
                        bord = False
                        for k in range(len(x)):
                            r,c = cur[0]+x[k], cur[1]+y[k]
                            if 0<=r<m and 0<=c<n:
                                if A[r][c]==1 and (r,c) not in island:
                                    frontier.append((r,c))
                                    island.add((r,c))
                                elif A[r][c]==0: borders[count+1].append(cur)
                    
                    count+=1
                    seen.update(island)
                if count==2: break
            if count==2: break
        
        
        ans = float('inf')
        borders
        
        for i,j in borders[1]:
            for x,y in borders[2]:
                ans = min(ans, abs(i-x)+abs(j-y)-1)
      
        return ans