from math import sqrt def f(p,X): a,b=p S=0 for x,y in X: S+=sqrt((x-a)**2+(y-b)**2) return S H,W=map(int,input().split()) X=[] for i in range(1,H+1): P=input() for j in range(1,W+1): if P[j-1]=="1": X.append((i,j)) Ans=float("inf") for j in range(1,W+1): Ans=min(Ans,f((0,j),X),f((H+1,j),X)) for i in range(1,H+1): Ans=min(Ans,f((i,0),X),f((i,W+1),X)) print(Ans)