from collections import defaultdict h, w = map(int, input().split()) p = [input() for i in range(h)] black = [] for i in range(h): for g in range(w): if p[i][g] == "1": black.append((i+1, g+1)) red = defaultdict(float) for i in range(1, h+1): for x, y in black: red[(i, 0)] += ((x-i)**2 + (y)**2)**0.5 red[(i, w+1)] += ((x-i)**2 + (y-w-1)**2)**0.5 for i in range(1, w+1): for x, y in black: red[(0, i)] += ((x)**2 + (y-i)**2)**0.5 red[(h+1, i)] += ((x-h-1)**2 + (y-i)**2)**0.5 print(min(red.values()))