from math import sqrt def sum_dist(H: int, W: int, st: tuple) -> int: global P dist = 0 for w in range(1,W+1): for h in range(1,H+1): if P[h-1][w-1] == 1: dist += sqrt((st[0]-w)**2 + (st[1]-h)**2) return dist H,W = map(int, input().split()) P = [] for i in range(H): P.append([int(p) for p in list(input().rstrip())]) INF = float("inf") ans = INF for w in range(1,W+1): ans_temp = sum_dist(H,W,(w,0)) ans = min(ans, ans_temp) ans_temp = sum_dist(H,W,(w,H+1)) ans = min(ans, ans_temp) for h in range(1,H+1): ans_temp = sum_dist(H,W,(0,h)) ans = min(ans, ans_temp) ans_temp = sum_dist(H,W,(W+1,h)) ans = min(ans, ans_temp) print(ans)