h, w = map(int, input().split()) info = [["2"]*(h+2)] + [["2"] + list(input()) + ["2"] for i in range(h)] + [["2"]*(h+2)] def solve(i,j): ans = 0 for k in range(h+2): for l in range(w+2): if info[k][l] == "1": ans += ((i-k)**(2) + (j-l)**(2))**(0.5) return ans min_ans = 100000000000000 for i in range(h+2): for j in range(w+2): if info[i][j] == "2": ans = solve(i, j) min_ans = min(ans, min_ans) print(min_ans)