H,W=map(int,input().split()) list1=[] list2=[] for i in range(W+2): list2.append(0) list1.append(list2) list2=[] for j in range(H): list3=[0] s=input() for k in range(W): list3.append(int(s[k])) list3.append(0) list1.append(list3) list3=[0] i=0 for i in range(W+2): list2.append(0) list1.append(list2) list2=[] #print(list1) listA=[] for a in range(H+2): for b in range(W+2): if a==0 or a==H+1: if b==0 or b==W+1: pass else: le=0 for c in range(1,H+1): for d in range(1,W+1): if list1[c][d]==1: le+=((a-c)**2+(b-d)**2)**0.5 listA.append(le) le=0 elif b==0 or b==W+1: le=0 for c in range(1,H+1): for d in range(1,W+1): if list1[c][d]==1: le+=((a-c)**2+(b-d)**2)**0.5 listA.append(le) le=0 print(min(listA))