from collections import deque
if __name__ == "__main__":
N,M = map(int,input().split())
graph=[]
for i in range(N):
graph.append(list(map(int,input())))
visited = [ [0 for _ in range(M)] for _ in range(N) ]
visited[0][0] = 1
queue = deque([[0,0]])
dists = [[-1,0],[1,0],[0,1],[0,-1]]
def bfs():
while queue:
[i,j] = queue.popleft()
for dist in dists:
nextI = i + dist[0]
nextJ = j + dist[1]
if nextI < 0 or nextJ < 0 or nextI >= N or nextJ >= M:
continue
if graph[nextI][nextJ] == 0 or visited[nextI][nextJ] != 0:
continue
visited[nextI][nextJ] = visited[i][j] + 1
queue.append([nextI,nextJ])
bfs()
print(visited[N-1][M-1])
for in range
로 만들었다.visited = [[0]*N]]*M
visited[0][0] = 1
// 의도한 값[[1,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
// 이상하게 동작[[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0]]
또는
방문한 경우에는 제외해야한는데 or연산자가 아닌 and연산자로 해서 헤매었다.if nextI < 0 or nextJ < 0 or nextI >= N or nextJ >= M:
continue // 좌표를 벗어난 경우
if graph[nextI][nextJ] == 0 or visited[nextI][nextJ] != 0:
continue // 미로가 막힌경우 또는 방문한 경우
오랜만에 bfs문제를 풀어보니 헷갈리는 부분이 많았다. 그래프 문제를 좀 더 집중해서 풀고 연습해야겠다