import java.awt.Point; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int H = scan.nextInt(); int W = scan.nextInt(); int[][]P = new int[H][W]; List list = new ArrayList(); for(int i = 0; i < H; i++) { String s = scan.next(); for(int j = 0; j < W; j++) { P[i][j] = (int)s.charAt(j) - '0'; if(P[i][j] == 1) { int x = j + 1; int y = i + 1; Point p = new Point(x, y); list.add(p); } } } scan.close(); double l1 = 0; double l2 = 0; double ans = Double.MAX_VALUE; for(int i = 1; i <= W; i++) { l1 = 0; l2 = 0; for(Point p : list) { int x = p.x; int y = p.y; int dx1 = i - x; int dy1 = 0 - y; l1 += Math.sqrt(dx1 * dx1 + dy1 * dy1); int dy2 = H + 1 - y; l2 += Math.sqrt(dx1 * dx1 + dy2 * dy2); } ans = Math.min(ans, l1); ans = Math.min(ans, l2); } for(int i = 1; i <= H; i++) { l1 = 0; l2 = 0; for(Point p : list) { int x = p.x; int y = p.y; int dx1 = 0 - x; int dy1 = i - y; l1 += Math.sqrt(dx1 * dx1 + dy1 * dy1); int dx2 = W + 1 - x; l2 += Math.sqrt(dx2 * dx2 + dy1 * dy1); } ans = Math.min(ans, l1); ans = Math.min(ans, l2); } System.out.println(ans); } }