BFS 啊哈算法-解救小哈 nextstep = [[0,1], [1,0], [0,-1], [-1,0] ] n,m = map(int,input().split()) queue = [[0 for i in range(3)] for j in range(500)] book = [[0 for i in range(m)] for j in range(n)] atlas = [input().split() for i in range(n)] startx,starty,finalx,finaly = map(int,input().split()) #队列初始化 head,tail=0,0 queue[tail][0]=startx-1 queue[tail][1]=starty-1 queue[tail][2]=0 tail+=1 book[startx-1][starty-1]=1 flag=0 while flag==0 and head < tail: for i in range(4): tx=queue[head][0]+nextstep[i][0] ty=queue[head][1]+nextstep[i][1] if 0 <= tx < n and 0 <= ty < m and atlas[tx][ty] == '0' and book[tx][ty] == 0: book[tx][ty]==1 queue[tail][0]=tx queue[tail][1]=ty queue[tail][2]=queue[head][2]+1 if tx==finalx-1 and ty==finaly-1: flag=1 break tail+=1 head+=1 print(queue[tail][2])