#include #include #include #include #include #include #include #include #include #include using namespace std; #define int long long int MOD = 1000000007; double solve(vector > &vp, int x, int y) { double res = 0; for (int i = 0; i < vp.size(); i++) { res += sqrt((vp[i].first - x)*(vp[i].first - x) + (vp[i].second - y)*(vp[i].second - y)); } return res; } signed main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector S(H); double res = 1e18; vector > vp; for (int i = 0; i < H; i++) { cin >> S[i]; for (int j = 0; j < W; j++) { if (S[i][j] == '1') { vp.emplace_back(i + 1, j + 1); } } } for (int i = 0; i < H; i++) { res = min(res, solve(vp, i, 0)); res = min(res, solve(vp, i, W + 1)); } for (int i = 0; i < W; i++) { res = min(res, solve(vp, H + 1, i)); res = min(res, solve(vp, 0, i)); } cout << fixed << setprecision(15) << res << endl; }