# -*- coding: utf-8 -*- import numpy as np X = [] Y = [] H, W = map(int, input().split()) for h in range(1, H+1): for w, v in enumerate(map(int, input()), start=1): if v==0: continue X.append(w) Y.append(h) gX = np.array(X, dtype=np.float64).reshape(1, -1) gY = np.array(Y, dtype=np.float64).reshape(1, -1) rX = np.array([0]*H + [W+1]*H + list(range(1, W+1)) + list(range(1, W+1)), dtype=np.float64).reshape(1, -1) rY = np.array(list(range(1, H+1)) + list(range(1, H+1)) + [0]*W + [H+1]*W, dtype=np.float64).reshape(1, -1) print(np.min(np.sum(np.sqrt(np.power(gX-rX.T, 2) + np.power(gY-rY.T, 2)), axis=1)))