#yuki707 import math h,w=map(int,raw_input().split()) p=[] q=[] for i in xrange(h): p.append(raw_input()) for i in xrange(h): for j in xrange(w): if p[i][j]=='1': q.append((i+1,j+1)) res=float('inf') for i in xrange(h+2): cols=range(1,w+1) if i==0 or i==h+1 else [0,w+1] for j in cols: temp=0 for x,y in q: temp+=math.sqrt((i-x)**2+(j-y)**2) res=min(res,temp) print "%.9f" % res