import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		int W = sc.nextInt();
		int[][] P = new int[H][W];
		for (int i = 0; i < H; ++i) {
			char[] c = sc.next().toCharArray();
			for (int j = 0; j < W; ++j) {
				P[i][j] = c[j] - '0';
			}
		}
		double ans = Double.MAX_VALUE / 3;
		for (int i = 0; i <= H + 1; ++i) {
			for (int j = 0; j <= W + 1; ++j) {
				boolean f1 = i == 0 || i == H + 1;
				boolean f2 = j == 0 || j == W + 1;
				if (f1 && f2 || !f1 && !f2)
					continue;
				double sum = 0;
				for (int k = 1; k <= H; ++k) {
					for (int l = 1; l <= W; ++l) {
						if (P[k - 1][l - 1] == 1) {
							sum += Math.sqrt((i - k) * (i - k) + (j - l) * (j - l));
						}
					}
				}
				ans = Math.min(ans, sum);
			}
		}
		System.out.println(ans);
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}