def getTotalX(a, b):
# Write your code here
def gcd(x, y):
if x > y:
s = y
y = x
x = s
# x < y
while(y > 0):
x, y = y, x % y
return x
def lcm(x, y):
return x * y // gcd(x, y)
_lcm = a[0]
for i in range(1, len(a)):
_lcm = lcm(_lcm, a[i])
if _lcm > 100:
return 0
_gcd = b[0]
for i in range(1, len(b)):
_gcd = gcd(_gcd, b[i])
if (_gcd == 0) or (_lcm > _gcd):
return 0
cnt = 0
for i in range(_lcm, _gcd + 1):
# b의 약수이고, a의 배수인 경우
if (i % _lcm == 0) and (_gcd % i == 0):
cnt += 1
return cnt
def kangaroo(x1, v1, x2, v2):
# Write your code here
# x1 < x2이므로 속도가 같거나 크면 따라잡을 수 없음
if v1 <= v2:
return 'NO'
else:
# v1 > v2여서 따라잡을 수 있는 경우
# 거리 차를 속도 차가 잡을 수 있고, 같은 위치에 있을 수 있어야함
r = (x2 - x1) % (v2 - v1)
if r == 0:
return 'YES'
else:
return 'NO'
def countApplesAndOranges(s, t, a, b, apples, oranges):
# Write your code here
print(sum([1 if ((apple + a) >= s and (apple + a) <= t) else 0 for apple in apples]))
print(sum([1 if ((orange + b) >= s and (orange + b) <= t) else 0 for orange in oranges]))
def gradingStudents(grades):
# Write your code here
result = []
for grade in grades:
if grade < 38:
result.append(grade)
else:
r = grade % 5
grade = grade + (5 - r) if r > 2 else grade
result.append(grade)
return result
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)