H,W=map(int,input().split()) R=[list(map(int,list(input()))) for h in range(H)] ans=1e20 HW=[] for h in range(1,H+1): HW.append((h,0)) HW.append((h,W+1)) for w in range(1,W+1): HW.append((0,w)) HW.append((H+1,w)) for h,w in HW: d=0 for x in range(H): for y in range(W): if R[x][y]: d+=((x+1-h)**2+(y+1-w)**2)**.5 ans=min(ans,d) print(ans)