from collections import deque N, M = map(int, input().split()) paper = [] cheese = 0 for i in range(N) : paper.append(list(map(int, input().split()))) for n in paper[i] : if n == 1 : cheese += 1 dx = [1,-1,0,0] dy = [0,0,1,-1] q = deque() time = 0 while cheese != 0 : visited = [[0]*M for _ in range(N)] q.append((0,0)) visited[0][0] = 1 while q : cx, cy = q.popleft() for i in range(4) : nx, ny = cx + dx[i], cy + dy[i] if 0<=nx= 2 : paper[i][j] = 0 cheese -= 1 time += 1 print(time)