예제 1은 맞는데 예제 2에서 틀림

어디가 틀렸는지 알려줄 고수 구합니다

from collections import deque

def bfs():

    while queue:

        x,y=queue.popleft()

        for i in range(6):

            nx=dx[i]+x

            ny=dy[i]+y

            if nx<=-1 or nx>=h*c or ny<=-1 or ny>=w or graph[nx][ny]==-1:

                continue

            if graph[nx][ny]==0:

                graph[nx][ny]=graph[x][y]+1

                queue.append((nx,ny))

w,h,c=map(int,input().split())

queue=deque()

dx=[-1,1,0,0,-h,h]

dy=[0,0,-1,1,0,0]

graph=[]

for _ in range(h*c):

    graph.append(list(map(int,input().split())))

for k in range(h*c):

    for j in range(w):

        if graph[k][j]==1:

            queue.append((k,j))

bfs()

ans=0

for m in range(h*c):

    for n in range(w):

        if graph[m][n]==0:

            print(-1)

            exit(0)

        else:

            ans=max(ans,graph[m][n])

print(ans-1)