import math H, W = map(int, input().split()) l = [] for i in range(H): ll = ["0"] + list(input()) + ["0"] l.append(ll) l = [["0" for i in range(W + 2)]] + l + [["0" for i in range(W + 2)]] print(l) best_ans = 10000000000 for i in range(H + 2): for j in range(W + 2): if 0 < i < H + 1 and 0 < j < W + 1: continue if i == j == 0: continue if i == 0 and j == W + 1: continue if j == 0 and i == H + 1: continue if i == H + 1 and j == W + 1: continue ans = 0 for cki in range(H + 2): for ckj in range(W + 2): if l[cki][ckj] == "1": ans += math.sqrt(pow(i - cki,2) + pow(j - ckj,2)) best_ans = min(best_ans,ans) print(best_ans)