import sys input = sys.stdin.readline H,W=map(int,input().split()) S=[input().strip() for i in range(H)] ANS=1<<60 for i in range(H): x=-1 y=i score=0 for j in range(H): for k in range(W): if S[j][k]=='1': score+=((x-j)**2+(y-k)**2)**0.5 ANS=min(ANS,score) x=W y=i score=0 for j in range(H): for k in range(W): if S[j][k]=='1': score+=((x-j)**2+(y-k)**2)**0.5 ANS=min(ANS,score) for i in range(W): x=i y=-1 score=0 for j in range(H): for k in range(W): if S[j][k]=='1': score+=((x-j)**2+(y-k)**2)**0.5 ANS=min(ANS,score) x=i y=H score=0 for j in range(H): for k in range(W): if S[j][k]=='1': score+=((x-j)**2+(y-k)**2)**0.5 ANS=min(ANS,score) print(ANS)