import java.io.*; import java.util.*; public class Main_yukicoder707 { private static Scanner sc; private static Printer pr; private static void solve() { int h = sc.nextInt(); int w = sc.nextInt(); char[][] p = new char[h][]; for (int i = 0; i < h; i++) { p[i] = sc.next().toCharArray(); } double min = Double.MAX_VALUE; for (int i = 1; i <= h; i++) { min = Math.min(min, d(p, i, 0)); min = Math.min(min, d(p, i, w + 1)); } for (int j = 1; j <= w; j++) { min = Math.min(min, d(p, 0, j)); min = Math.min(min, d(p, h + 1, j)); } pr.printf("%.7f\n", min); } private static double d(char[][] p, int y, int x) { int h = p.length; int w = p[0].length; double d = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (p[i][j] == '1') { d += Math.sqrt((i + 1 - y) * (i + 1 - y) + (j + 1 - x) * (j + 1 - x)); } } } // pr.printf("%d %d %f\n", y, x, d); return d; } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(INPUT == null ? System.in : new ByteArrayInputStream(INPUT.getBytes())); pr = new Printer(System.out); solve(); // pr.close(); pr.flush(); // sc.close(); } static String INPUT = null; private static class Printer extends PrintWriter { Printer(OutputStream out) { super(out); } } }