# -*- coding:utf-8 -*-
from collections import deque

def bfs() -> list:
    while queue:
        x, y = queue.popleft()

        for i in range(4):
            a = x + dx[i]
            b = y + dy[i]

            if 0<=a<N and 0<=b<M and graph[a][b] == 0:
                queue.append([a, b]) # 방문하지 않은 다음 노드를 큐에 삽입
                graph[a][b] = graph[x][y] + 1

    return graph

if __name__ == "__main__":

    # 초기 변수 선언 & 그래프 생성
    M, N = list(map(int, input().split()))
    graph = []
    queue = deque()

    dx = [-1, 0, 1, 0] # 익은 토마토 좌표 기준으로 사방향 드로잉을 위한 x좌표
    dy = [0, -1, 0, 1] # 익은 토마토 좌표 기준으로 사방향 드로잉을 위한 y좌표

    for i in range(N):
        graph.append(list(map(int,input().split())))

    # 익은 토마토 좌표 추출 -> 큐 삽입

    for i in range(N):
        for j in range(M):
            if graph[i][j] == 1:
                queue.append([i, j])
    
    # 토마토 숙성
    graph = bfs()

    # 결과 출력
    day = 0
    for i in graph:
        for j in i:
            if j == 0:
                print (-1)
                exit(0)
        day = max(day, max(i))
    
    print (day-1)
# -*- coding:utf-8 -*-
import sys
from collections import deque

def breadth_first_seach() -> list:
    while queue:
        z, x, y = queue.popleft()

        for i in range(6):
            a = z + dz[i]
            b = x  +dx[i]
            c = y + dy[i]

            if 0<=a<H and 0<=b<N and 0<=c<M and graph[a][b][c] == 0:
                queue.append([a, b, c])
                graph[a][b][c] = graph[z][x][y] + 1

    return graph


if __name__ == "__main__":

    # N = 행, M = 열, H = 높이
    M, N, H = list(map(int, sys.stdin.readline().split()))
    queue = deque()
    graph = [[[0 for i in range(M)] for j in range(N)] for k in range(H)]

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

    for z in range(H):
        for x in range(N): # 행
            graph[z][x] = list(map(int, sys.stdin.readline().split()))
            for y in range(M): # 열
                if graph[z][x][y] == 1:
                    queue.append([z, x, y])

    graph = breadth_first_seach()    
    
    day = 0
    for i in graph:
        for j in i:
            for k in j:
                if k == 0:
                    print (-1)
                    exit(0)
            day = max(day, max(j))
    print (day-1)

+ Recent posts