H,W = map(int, input().split()) def dist(a,b,x,y): return ((a-x)**2+(b-y)**2)**0.5 pos = {} for i in range(1,H+1): pos[(i,0)] = 0 pos[(i,W+1)] = 0 for j in range(1,W+1): pos[(0,j)] = 0 pos[(H+1,j)] = 0 for i in range(H): s = input() for j in range(W): if s[j]=="1": for y,x in pos: pos[(y,x)] += dist(i+1,j+1,y,x) ans = 10**10 for y,x in pos: ans = min(ans,pos[(y,x)]) print(ans)