#include <bits/stdc++.h>

using namespace std;

using ll = long long;

double calc(int x, int y, int H, int W, vector<string>& p) {
    double res = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (p[i][j] == '0') continue;
            int x2 = j + 1, y2 = i + 1;
            res += hypot((x - x2), (y - y2));
        }
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W;
    cin >> H >> W;
    vector<string> p(H);
    for (int i = 0; i < H; i++) {
        cin >> p[i];
    }

    double ans = 1e15;
    for (int i = 0; i < H; i++) {
        int y = i + 1;
        int x = 0;
        ans = min(ans, calc(x, y, H, W, p));
        x = W + 1;
        ans = min(ans, calc(x, y, H, W, p));
    }

    for (int j = 0; j < W; j++) {
        int x = j + 1;
        int y = 0;
        ans = min(ans, calc(x, y, H, W, p));
        y = H + 1;
        ans = min(ans, calc(x, y, H, W, p));
    }
    cout << fixed << setprecision(15) << ans << endl;
    return 0;
}