#include "bits/stdc++.h" #define REP(i, n, N) for(ll i=(n); i<(N); i++) #define RREP(i, n, N) for(ll i=(N-1); i>=n; i--) #define CK(n, a, b) ((a)<=(n)&&(n)<(b)) #define ALL(v) (v).begin(),(v).end() #define p(s) cout<<(s)<> typedef long long int ll; using namespace std; const ll mod = 1e9 + 7; const ll inf = 1e18; ll H,W; int main() { while(cin>>H>>W){ string P[52]; REP(i,1,H+1){ cin>>P[i]; } double ans=1e18; REP(i,0,H+2){ REP(j,0,W+2){ if((i==0||i==H+1||j==0||j==W+1)){ double tmp=0; REP(y,1,H+1){ REP(x,0,W){ if(P[y][x]=='1'){ tmp+=sqrt((y-i)*(y-i)+(x+1-j)*(x+1-j)); } } } ans=min(ans,tmp); } } } printf("%.7lf\n",ans); } return 0; }