import sys
from collections import deque
M, N, H = map(int, input().split())
graph = [[list(map(int, sys.stdin.readline().split())) for _ in range(N)] for _ in range(H)]
queue = deque()
dx = [1,-1,0,0,0,0]
dy = [0,0,1,-1,0,0]
dz = [0,0,0,0,1,-1]
for i in range(H):
for j in range(N):
for k in range(M):
if graph[i][j][k] == 1:
queue.append((i,j,k))
def BFS():
while queue:
x, y, z = queue.popleft()
for i in range(6):
nx = x+dx[i]
ny = y+dy[i]
nz = z+dz[i]
if 0<=nx<H and 0<=ny<N and 0<= nz<M and graph[nx][ny][nz] == 0:
graph[nx][ny][nz] = graph[x][y][z] + 1
queue.append((nx,ny,nz))
BFS()
ans = 0
for i in range(H):
for j in range(N):
for k in range(M):
if graph[i][j][k] == 0:
print(-1)
exit()
else:
ans = max(ans, graph[i][j][k])
print(ans-1)
기존 토마토 문제에서 높이를 추가로 고려하여 BFS를 수행하면 되는 문제이다.