def main(): H, W = map(int, input().split()) P = list(input() for _ in range(H)) ans = float('inf') for h1 in range(1,H+1): for w1 in [0,W+1]: tmp = 0 for h in range(H): for w in range(W): if P[h][w] == '1': tmp += ((h + 1 - h1) ** 2 + (w + 1 - w1) ** 2) ** 0.5 ans = min(ans, tmp) for w1 in range(1,W+1): for h1 in [0,H+1]: tmp = 0 for h in range(H): for w in range(W): if P[h][w] == '1': tmp += ((h + 1 - h1) ** 2 + (w + 1 - w1) ** 2) ** 0.5 ans = min(ans, tmp) print(ans) if __name__ == "__main__": main()