#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--) #define fi first #define se second #define vi vector<int> #define pb push_back #define mp make_pair #define pcnt __builtin_popcount typedef long long ll; const double inf = 900900900100.0; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int main(){ // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); int h,w; cin >> h >> w; string s[55] = {}; double madist = inf; rep(y,h) cin >> s[y]; rep(y,h+2)rep(x,w+2){ if(y==h+1&&x==w+1) continue; if(y==0 &&x ==0) continue; if(y==h+1&&x ==0) continue; if(y==0 &&x ==w+1) continue; if(x==0||y==0||x==w+1||y==h+1){ double dist = 0.0; REP(yy,1,h+1)REP(xx,1,w+1){ if(y==yy&&x==xx) continue; if(s[yy-1][xx-1]=='1'){ dist += sqrtl((double)(yy-y)*(yy-y)+(double)(xx-x)*(xx-x)); // cout << dist << endl; } } // cout << dist << endl; madist = min<double>(madist,dist); } } cout <<setprecision(9)<< madist << endl; return 0; }