#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; int H, W; char P[55][55]; void solve() { cin >> H >> W; double ans = numeric_limits::max(); rep(y, H)cin >> (P[y + 1] + 1); rep(sy, H + 2)rep(sx, W + 2) { int z = (sy == 0) + (sy == H + 1) + (sx == 0) + (sx == W + 1); if (z != 1)continue; double sm = 0; FOR(y, 1, H + 1)FOR(x, 1, W + 1)if (P[y][x] == '1') { sm += hypot(sy - y, sx - x); } smin(ans, sm); } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }