#!/usr/bin/env python3 import math h, w = map(int, input().split()) p = [ list(map(int, input())) for _ in range(h) ] pos = [] pos += [ (y, -1) for y in range(h) ] pos += [ (y, w) for y in range(h) ] pos += [ (-1, x) for x in range(w) ] pos += [ (w, x) for x in range(w) ] answer = math.inf for y0, x0 in pos: acc = 0 for y1 in range(h): for x1 in range(w): if p[y1][x1]: acc += math.hypot(x1 - x0, y1 - y0) answer = min(answer, acc) print(answer)