H,W = map(int, input().split()) P = [input() for _ in range(H)] def dist(x, y, s, t): return ((x-s)**2+(y-t)**2)**0.5 ans = 10**9 for i in range(-1, H+1, H+1): for j in range(W): num = 0 for k in range(H): for l in range(W): if P[k][l] == "0": continue num += dist(i, j, k, l) ans = min(ans, num) for j in range(-1, W+1, W+1): for i in range(H): num = 0 for k in range(H): for l in range(W): if P[k][l] == "0": continue num += dist(i, j, k, l) ans = min(ans, num) print(ans)