#include <bits/stdc++.h>
using namespace std;


int m[60][60];
vector<double> tmp;

void search(int x, int y, int h, int w) {
  double t = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (m[i][j] == 1) {
        t += sqrt((i-x)*(i-x)+(j-y)*(j-y));
      }
    }
  }
  tmp.push_back(t);
}

int main(){
  int h, w; cin >> h >> w;
  for (int i = 0; i < h; i++) {
    string s; cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '1') m[i+1][j+1]++;
    }
  }
  for (int i = 1; i <= h; i++) {
    search(i, 0, h, w); search(i, w+1, h, w);
  }
  for (int i = 1; i <= w; i++) {
    search(0, i, h, w); search(h+1, i, h, w);
  }
  sort(tmp.begin(), tmp.end());
  printf("%.9f\n", tmp[0]);
  return 0;
}