h,w = map(int,raw_input().split()) a = [-1]*w+range(h)+range(h)+[h]*w b = range(w)+[-1]*h+[w]*h+range(w) mn = 1000000; d = list(); for y in xrange(h): s = raw_input() d += [[y,x] for x in range(w) if s[x] == '1'] for y1,x1 in zip(a,b): mn = min(mn,sum(((y1-y2)**2+(x1-x2)**2)**.5 for y2,x2 in d)) print mn