#include using namespace std; signed main() { ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector P(H); for (int i = 0; i < H; ++i) cin >> P[i]; auto sqr = [](double x) { return x * x; }; double ans = 1e9; for (int i = 0; i < H; ++i) { double cost0 = 0, cost1 = 0; for (int r = 0; r < H; ++r) for (int c = 0; c < W; ++c) if (P[r][c] == '1') { cost0 += sqrt(sqr(i - r) + sqr(-1 - c)); cost1 += sqrt(sqr(i - r) + sqr(W + 1 - c)); } ans = min(ans, cost0); ans = min(ans, cost1); } for (int i = 0; i < W; ++i) { double cost0 = 0, cost1 = 0; for (int r = 0; r < H; ++r) for (int c = 0; c < W; ++c) if (P[r][c] == '1') { cost0 += sqrt(sqr(-1 - r) + sqr(i - c)); cost1 += sqrt(sqr(H + 1 - r) + sqr(i - c)); } ans = min(ans, cost0); ans = min(ans, cost1); } cout << fixed << setprecision(9) << ans << endl; return 0; }