import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

H,W = map(int,readline().split())
P = np.frombuffer(read(),'S1').reshape(H,-1)[:,:W]

P = (P == b'1') * 1

def gen_position():
    for x in range(H):
        yield (x,-1)
        yield (x,W)
    for y in range(W):
        yield (-1,y)
        yield (H,y)

opt_value = 10 ** 18
for x,y in gen_position():
    dx = np.arange(H) - x
    dy = np.arange(W) - y
    D = np.sqrt((dx*dx)[:,None] + (dy*dy)[None,:])
    value = (D * P).sum()
    if opt_value > value:
        opt_value = value

print(opt_value)