


익은 토마토 칸 1 vs 익지 않은 토마토 칸 0 vs 토마토 없는 칸 -1
익지 않은 토마토는 주변에 익은 토마토의 영향을 받아 익음 -> DFS BFS
N x M 행렬 (세로 x 가로)
알고싶은 것
이거 BFS 네 => graph에서 1인 것부터 풀스캔하면서 BFS수행하고
근데 BFS 맞는데 만약 첫 셋팅에서 1이 여러 개면(=전파시키는 토마토) "병렬"로 수행되어야 할 것 같은데?!
그리고 테케 2번 보면 토마토가 모두 익지 못하는 경우(=하나라도 안익은 경우) -1 출력 해야 함
''' 내가 푼 - 답아님 - 시작이 하나일 경우 (근데 애초에 답도 안나옴) '''
from collections import deque
def bfs(y, x, graph, n, m, cnt):
    cnt += 1
    graph[y][x] = 1
    que = deque([[y, x]])
    
    #     상 하 좌 우
    dy = [-1, 1, 0, 0]
    dx = [0, 0, -1, 1]
    
    while que:
        tmp = que.popleft()
        for i in range(4):
            ny = tmp[0] + dy[i]
            nx = tmp[1] + dx[i]
            
            if ny < 0 or n <= ny or nx < 0 or m <= nx: continue
            if graph[ny][nx] == -1: continue
                
            if graph[ny][nx] == 0:                
                graph[ny][nx] = 1
                que.append([ny, nx])
        cnt += 1
                
    return cnt
#m, n = map(int, input().split())  # n = 세로, m = 가로
#graph = [list(map(int, input().split())) for _ in range(n)]
m, n = 6, 4
# test case 1
graph = [[0, 0, 0, 0, 0, 0],         [0, 0, 0, 0, 0, 0],         [0, 0, 0, 0, 0, 0],         [0, 0, 0, 0, 0, 1]]
# test case 3
#graph = [[1, -1, 0, 0, 0, 0],         [0, -1, 0, 0, 0, 0],         [0, 0, 0, 0, -1, 0],         [0, 0, 0, 0, -1, 1]]
answer = 0
cnt = 0
for y in range(n):
    for x in range(m):
        if graph[y][x] == -1: continue
        if graph[y][x] == 0:            
            answer += bfs(y, x, graph, n, m, cnt)
            cnt = 0
for small in graph:
    for i in range(m):
        if small[i] == 0:   # 하나라도 있으면
            answer = -1   
print(answer)
''' 블로그 어떻게 했나 참고하고 다시 푼 '''
from collections import deque
import sys
input = sys.stdin.readline
m, n = map(int, input().split())  # n = 세로, m = 가로
graph = [list(map(int, input().split())) for _ in range(n)]
answer = 0
cnt = 0
que = deque()
# 토마토의 위치 전부 push
for y in range(n):
    for x in range(m):
        if graph[y][x] == 1:
            que.append([y, x])
#     상 하 좌 우
dy, dx = [-1, 1, 0, 0], [0, 0, -1, 1]
while que:
    ay, ax = que.popleft()
    for i in range(4):
        ny, nx = ay + dy[i], ax + dx[i]
        if 0 <= ny < n and 0 <= nx < m and graph[ny][nx] == 0:
            graph[ny][nx] = graph[ay][ax] + 1  # (핵중요) 최단거리 값 키워나가
            que.append([ny, nx])
flag = False
for small_g in graph:
    if 0 in small_g:  # 하나라도 있으면
        flag = True
        break
    answer = max(answer, max(small_g))
print(-1 if flag else answer - 1)
# => 처음 시작할 때 익은토마토는 1이고, 첫 익은 애의 +1 = 2로 시작했으니까 -1해줘야함

else: print(answer-1)
