from collections import deque
m,n = map(int,input().split())
graph = []
queue = deque([])
for i in range(n):
graph.append(list(map(int,input().split())))
for j in range(m):
if graph[i][j]==1:
queue.append([i,j])
dx = [0,0,-1,1]
dy = [-1,1,0,0]
def bfs():
while queue:
x,y = queue.popleft()
for i in range(4):
nx = x+dx[i]
ny = y+dy[i]
if 0 <= nx < n and 0 <= ny < m and graph[nx][ny]==0:
queue.append([nx,ny])
graph[nx][ny] = graph[x][y]+1
bfs()
result = 0
for i in graph:
for j in i:
if j==0:
print(-1)
exit(0)
result = max(result,max(i))
print(result-1)
'문제풀이 > 백준(Boj) 문제풀이' 카테고리의 다른 글
[백준][스택] 9012. 스택 (파이썬/Python) (0) | 2021.11.11 |
---|---|
[백준][BFS] 1753. 최단경로 (파이썬/Python) (0) | 2021.11.09 |
[백준][큐 & 덱] 18258. 큐 2 (파이썬/Python) (0) | 2021.11.08 |
[백준][구현] 2504.괄호의 값 (파이썬/Python) (0) | 2021.11.02 |
[백준][구현] 10820. 문자열 분석 (파이썬/Python) (0) | 2021.11.02 |