import java.util.Scanner object Main extends App { val sc = new Scanner(System.in) val h, w = sc.nextInt val poses = (0 until h).map({ _ => val s = sc.next (0 until w).filter(s(_) == '1') }) Console.err.println(poses(0).mkString(" ")) val cands = (1 to w).map((0, _)) ++ (1 to w).map((h + 1, _)) ++ (1 to h).map((_, 0)) ++ (1 to h).map((_, w + 1)) val ans = cands.map({ case (sy, sx) => (0 until h).map({ cy => poses(cy).map({ cx => math.sqrt(((cx + 1 - sx) * (cx + 1 - sx) + (cy + 1 - sy) * (cy + 1 - sy)).toFloat) }).sum }).sum }).min println(ans) }