#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define all(x) (x).begin(), (x).end() #define MOD 1000000007 #define int long long using ll = long long; using namespace std; const int INF = 1LL << 50; using P = pair; template bool chmin(T &a, T b) { if (a > b) { a = b; return true; } else { return false; } } template bool chmax(T &a, T b) { if (a < b) { a = b; return true; } else { return false; } } struct Setup { Setup() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); } } SETUP; int solve(vector &as, int h, int w, int x, int y) { double res = 0.0; rep(i, 0, h) rep(j, 0, w) { int xx = j + 1, yy = i + 1; if (as[i][j] == '1') { res += sqrt(pow(abs(xx - x), 2) + pow(abs(yy - y), 2)); } } return res; } signed main() { int h, w; cin >> h >> w; vector as(h); for (auto &s : as) { cin >> s; } double ans = INF + 0.123456; rep(i, 1, h + 1) { int x = 0, y = i; chmin(ans, solve(as, h, w, x, y)); } rep(i, 1, h + 1) { int x = w + 1, y = i; chmin(ans, solve(as, h, w, x, y)); } rep(i, 1, w + 1) { int x = i, y = 0; chmin(ans, solve(as, h, w, x, y)); } rep(i, 1, w + 1) { int x = i, y = h + 1; chmin(ans, solve(as, h, w, x, y)); } cout << ans << endl; }