import sys input = sys.stdin.readline def calc(x, y): res = 0 for i in range(H): for j in range(W): if P[i][j]=='1': res += ((x-i)**2+(y-j)**2)**0.5 return res H, W = map(int, input().split()) P = [input()[:-1] for _ in range(H)] ans = 10**18 for i in range(H): ans = min(ans, calc(i, -1), calc(i, W)) for i in range(W): ans = min(ans, calc(-1, i), calc(H, i)) print(ans)