from collections import deque def BFS(V,E): Q=deque([V[0]]) S={v:False for v in V} S[V[0]]=True T=[V[0]] while Q: v=Q.popleft() N=[w for w in V if ((v,w) in E) and (not S[w])] for w in N: Q.append(w) T.append(w) S[w]=True return T H,W=map(int,input().split()) def f(x,y): return y*(W+2)+x def g(n): return (n%(W+2),n//(W+2)) A=[[0]*(W+2)] for i in range(H): A.append([0]+list(map(int,input().split()))+[0]) A+=[[0]*(W+2)] V=[] for y in range(H+2): for x in range(W+2): if A[y][x]==1: V.append(f(x,y)) E=set() for n in V: (x,y)=g(n) if A[y][x]==1: if A[y][x+1]==1: E|={(f(x,y),f(x+1,y)),(f(x+1,y),f(x,y))} if A[y+1][x]==1: E|={(f(x,y),f(x,y+1)),(f(x,y+1),f(x,y))} U=set() W=set(V) k=0 while U!=W: k+=1 U|=set(BFS(list(W-U),E)) print(k)