from collections import deque
# 입력
m,n,h=map(int,input().split())
tomato = []
for i in range(h):
tomato.append([list(map(int,input().split())) for _ in range(n)])
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 tomato[i][j][k] == 1:
queue.append([i,j,k])
def ripe():
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 tomato[nx][ny][nz] == 0:
tomato[nx][ny][nz] = tomato[x][y][z] + 1
queue.append([nx,ny,nz])
ripe()
ans = 0
for t1 in tomato:
for t2 in t1:
for t3 in t2:
if t3 == 0:
print(-1)
exit(0)
ans = max(ans ,max(t2))
print(ans-1)