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