import math H,W=map(int,input().split()) papers=[input() for _ in range(H)] blacks = [] for x in range(H): for y in range(W): if papers[x][y] == '1': blacks.append([x+1,y+1]) result = pow(10,18) for c in [0,W+1]: for a in range(1,H+1): tmp = 0 for i in range(len(blacks)): tmp += math.sqrt(pow(blacks[i][0]-a,2)+pow(blacks[i][1]-c,2)) result = min(result,tmp) for r in [0,H+1]: for b in range(1,W+1): tmp = 0 for i in range(len(blacks)): tmp += math.sqrt(pow(blacks[i][0]-r,2)+pow(blacks[i][1]-b,2)) result = min(result,tmp) print(result)