/* -*- coding: utf-8 -*- * * 707.cc: No.707 書道 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_H = 50; const int MAX_W = 50; const int MAX_N = MAX_H * MAX_W; const double DINF = 1.0e60; /* typedef */ /* global variables */ char s[MAX_W + 2]; int xs[MAX_N], ys[MAX_N]; /* subroutines */ double dsum(int n, int x0, int y0) { double sum = 0.0; for (int i = 0; i < n; i++) { int dx = x0 - xs[i], dy = y0 - ys[i]; sum += sqrt(dx * dx + dy * dy); } return sum; } inline void setmin(double &a, double b) { if (a > b) a = b; } /* main */ int main() { int h, w; scanf("%d%d", &h, &w); int n = 0; for (int y = 1; y <= h; y++) { scanf("%s", s); for (int x = 1; x <= w; x++) if (s[x - 1] == '1') xs[n] = x, ys[n] = y, n++; } //printf("n=%d\n", n); double mind = DINF; for (int x = 1; x <= w; x++) { setmin(mind, dsum(n, x, 0)); setmin(mind, dsum(n, x, h + 1)); } for (int y = 1; y <= h; y++) { setmin(mind, dsum(n, 0, y)); setmin(mind, dsum(n, w + 1, y)); } printf("%.8lf\n", mind); return 0; }