H, W = map(int, input().split()) A = [[-1] * (W + 2) for _ in range(H + 2)] for i in range(1, H + 1): A[i] = [-1] + list(map(int, input().rstrip())) + [-1] A[0][0], A[0][W + 1], A[i + 1][0], A[i + 1][W + 1] = -2, -2, -2, -2 ans = 1e9 for i in range(0, H + 2): for j in range(0, W + 2): if A[i][j] == -1: sum = 0 for x in range(1, H + 1): for y in range(1, W + 1): if A[x][y] == 1: d = ((i - x) * (i - x) + (j - y) * (j - y)) ** 0.5 sum += d ans = min(ans, sum) print(ans)