h,w=map(int,input().split()) p=[[0]+[0]*w+[0]] for i in range(h): p+=[[0]+list(map(int,input()))+[0]] p+=[[0]+[0]*w+[0]] a=10**10 for i in range(h+2): j=0 na=0 for ii in range(1,1+h): for jj in range(1,1+w): na+=(((ii-i)**2+(jj-j)**2)**0.5)*p[ii][jj] a=min(a,na) j=w+1 na=0 for ii in range(1,1+h): for jj in range(1,1+w): na+=(((ii-i)**2+(jj-j)**2)**0.5)*p[ii][jj] a=min(a,na) for j in range(w+2): i=0 na=0 for ii in range(1,1+h): for jj in range(1,1+w): na+=(((ii-i)**2+(jj-j)**2)**0.5)*p[ii][jj] a=min(a,na) i=h+1 na=0 for ii in range(1,1+h): for jj in range(1,1+w): na+=(((ii-i)**2+(jj-j)**2)**0.5)*p[ii][jj] a=min(a,na) print(a)