from collections import deque 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)) def BFS(V): Q=deque([V[0]]) S={v:False for v in V} S[V[0]]=True T=[V[0]] U=set(V) while Q: v=Q.popleft() N=[] if v+1 in U and not S[v+1]: Q.append(v+1) T.append(v+1) S[v+1]=True if v-1 in U and not S[v-1]: Q.append(v-1) T.append(v-1) S[v-1]=True if v+(W+2) in U and not S[v+(W+2)]: Q.append(v+(W+2)) T.append(v+(W+2)) S[v+(W+2)]=True if v-(W+2) in U and not S[v-(W+2)]: Q.append(v-(W+2)) T.append(v-(W+2)) S[v-(W+2)]=True return T V=set() for y in range(H): A=list(map(int,input().split())) for x in range(W): if A[x]==1: V.add(f(x+1,y+1)) U=set() X=set(V) k=0 while U!=X: k+=1 U|=set(BFS(list(X-U))) print(k)