import java.io.InputStream; import java.io.PrintWriter; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; import static java.util.Comparator.*; public class Main { public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); Solver solver = new Solver(System.in, out); solver.solve(); out.close(); } } class Solver { Scanner sc; PrintWriter out; public Solver(InputStream in, PrintWriter out) { sc = new Scanner(in); this.out = out; } // ================================================================== public void solve() { int H = Integer.parseInt(sc.next()); int W = Integer.parseInt(sc.next()); char[][] C = new char[H][]; for (int i = 0; i < H; i++) { C[i] = sc.next().toCharArray(); } double ans = 999999999, wk; for (int i = 0; i <= H + 1; i += (H+1)) { for (int j = 1; j <= W; j++) { wk = 0; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { if(C[y][x] == '1') { wk += Math.sqrt((x+1 - j)*(x+1 - j) + (y+1 - i)*(y+1 - i)); } } } ans = Math.min(ans, wk); } } for (int i = 1; i <= H; i++) { for (int j = 0; j <= W + 1; j += (W+1)) { wk = 0; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { if(C[y][x] == '1') { wk += Math.sqrt((x+1 - j)*(x+1 - j) + (y+1 - i)*(y+1 - i)); } } } ans = Math.min(ans, wk); } } out.println(ans); } }