from collections import deque
import sys
M, N = map(int, sys.stdin.readline().split())
arr = []
my_deq = deque()
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
cnt = 0
for i in range(N):
arr.append(list(map(int, sys.stdin.readline().split())))
for j in range(M):
if arr[i][j] == 1:
cnt += 1
my_deq.append((i, j))
elif arr[i][j] == -1:
cnt += 1
answer = -1
while my_deq:
my_len = len(my_deq)
for _ in range(my_len):
x, y = my_deq.popleft()
for i in range(4):
new_x, new_y = x + dx[i], y + dy[i]
if 0 <= new_x < N and 0 <= new_y < M and arr[new_x][new_y] == 0:
cnt += 1
arr[new_x][new_y] = 1
my_deq.append((new_x, new_y))
answer += 1
if cnt == M * N:
print(answer)
else:
print(-1)
'파이썬 코테 준비' 카테고리의 다른 글
[백준 1300] K번째 수 - Python(파이썬) (0) | 2021.03.09 |
---|---|
[백준 2805] 나무 자르기 - Python(파이썬) (0) | 2021.03.09 |
[백준 1012] 유기농 배추 - Python(파이썬) (0) | 2021.03.09 |
[백준 2606] 바이러스 - Python(파이썬) (0) | 2021.03.09 |
[백준 9251] LCS - Python(파이썬) (0) | 2021.03.09 |
댓글