import numpy as np height, width = [int(x) for x in input().split(' ')] paint_place = [] length_lists = [] for y in range(1, height + 1): x_lists = [int(x) for x in input()] for x, i in enumerate(x_lists): if i == 1: paint_place.append([x + 1,y]) paint_place = np.array(paint_place) if paint_place.any(): for x in range(width + 2): for y in range(height + 2): if (((x == 0) or (x == width + 1)) and (1 <= y <= height)) or (((y == 0) or (y == height + 1)) and (1 <= x <= width)) : point = np. array([x, y]) length_lists.append(np.sum(np.sqrt(np.sum(((paint_place - point) ** 2), axis=1)))) print(min(length_lists)) else: print(0)