이런 류(bfs)의 문제는 대게 deque를 사용하나 단원이 스택인만큼 리스트를 활용하여 풀었다.
move=[(-1,0),(1,0),(0,1),(0,-1)]#위 아래 오른 왼
for tc in range(1,int(input())+1):
N = int(input())
arr=[list(map(int,input())) for _ in range(N)]
for i in range(N):
for j in range(N):
if arr[i][j]==2:
start,end=i,j
chk=[]
chk.append((start,end))
res=0
while chk:
x,y=chk.pop(0)
if arr[x][y]==3:
res=1
break
arr[x][y]=1
for i in range(4):
mx=x+move[i][0]
my=y+move[i][1]
if 0<=mx<N and 0<=my<N and arr[mx][my]!=1:
chk.append((mx,my))
print(f'#{tc} {res}')