import sys
from collections import deque

# input = sys.stdin.readline

M, N, H = map(int, input().split())

floors = []

for _ in range(H):
    floor = []
    for _ in range(N):
        floor.append(list(map(int, input().split())))
        
    floors.append(floor)
    
queue = deque()

for i in range(H):
    for j in range(N):
        for k in range(M):
            if(floors[i][j][k] == 1):
                queue.append([i, j, k])

# 이전 문제랑 다른 점은 높이가 추가됨
dx = [1, -1, 0, 0, 0, 0]
dy = [0, 0, 1, -1, 0, 0]
dh = [0, 0, 0, 0, 1, -1]

while queue:
    height, row, col = queue.popleft()
    
    for k in range(6):
        _height = height + dh[k]
        _row = row + dy[k]
        _col = col + dx[k]
        
        if 0 <= _height < H and 0 <= _row < N and 0 <= _col < M and floors[_height][_row][_col] == 0:
            queue.append([_height, _row, _col])
            floors[_height][_row][_col] = floors[height][row][col] + 1
            
check_tot = False
result = -2

for i in floors:
    for j in i:
        for k in j:
            if(k == 0):
                check_tot = True
            result = max(result, k)
            
if check_tot:
    print(-1)
elif(result == -1):
    print(0)
else:
    print(result - 1)

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

[HackerRank-python] Apple and Orange  (0) 2022.06.29
[HackerRank-python] Grading Students  (0) 2022.06.29
백준 7576번(python)  (0) 2020.08.02
백준 2606번(python)  (0) 2020.08.01
백준 2805번(python)  (0) 2020.07.27