import sys input=lambda: sys.stdin.readline().rstrip() h,w=map(int,input().split()) P=[input() for _ in range(h)] def dist(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5 def f(x,y): ret=0 for i in range(h): for j in range(w): if P[i][j]=="1": ret+=dist(x,y,i+1,j+1) return ret ans=float("inf") for i in range(h): ans=min(ans,f(i+1,0)) ans=min(ans,f(i+1,w+1)) for i in range(w): ans=min(ans,f(0,i+1)) ans=min(ans,f(h+1,i+1)) print(ans)