from math import * def dist(x1, y1, x2, y2): return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) H, W = map(int, input().split()) P = [] for i in range(H): L = list(input()) L = list(map(int, L)) P.append(L) T = [] for i in range(W): T.append((-1, i)) T.append((H, i)) for i in range(H): T.append((i, -1)) T.append((i, W)) ans = 10 ** 18 for x, y in T: val = 0 for i in range(H): for j in range(W): if P[i][j]: val += dist(x, y, i, j) ans = min(ans, val) print(ans)