import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author silviase */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Caligraphy solver = new Caligraphy(); solver.solve(1, in, out); out.close(); } static class Caligraphy { public void solve(int testNumber, Scanner in, PrintWriter out) { // long 恧chmin int h = in.nextInt(); int w = in.nextInt(); char[][] c = new char[h + 2][w + 2]; Pair p = new Pair(0, 0); for (int i = 1; i <= h; i++) { String s = in.next(); Arrays.fill(c[i], '0'); for (int j = 0; j < w; j++) { c[i][j + 1] = s.charAt(j); } } double res = 1e11; for (int i = 1; i <= h; i++) { double cmp = 0; for (int j = 1; j <= h; j++) { for (int k = 1; k <= w; k++) { if (c[j][k] == '1') { cmp += Math.sqrt((i - j) * (i - j) + (0 - k) * (0 - k)); } } } res = Math.min(res, cmp); cmp = 0; for (int j = 1; j <= h; j++) { for (int k = 1; k <= w; k++) { if (c[j][k] == '1') { cmp += Math.sqrt((i - j) * (i - j) + (w + 1 - k) * (w + 1 - k)); } } } res = Math.min(res, cmp); } for (int i = 1; i <= w; i++) { double cmp = 0; for (int j = 1; j <= h; j++) { for (int k = 1; k <= w; k++) { if (c[j][k] == '1') { cmp += Math.sqrt((0 - j) * (0 - j) + (i - k) * (i - k)); } } } res = Math.min(res, cmp); cmp = 0; for (int j = 1; j <= h; j++) { for (int k = 1; k <= w; k++) { if (c[j][k] == '1') { cmp += Math.sqrt((h + 1 - j) * (h + 1 - j) + (i - k) * (i - k)); } } } res = Math.min(res, cmp); } out.println(res); } } static class Pair { public long x; public long y; public Pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return "Pair{" + "x=" + x + ", y=" + y + '}'; } } }