#![allow(non_snake_case)] #[allow(unused_macros)] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut tokens = $s.split_whitespace(); input_inner! { tokens, $($r)* } }; ($($r:tt)*) => { let s = { use std::io::Read; let mut res = String::new(); ::std::io::stdin().read_to_string(&mut res).unwrap(); res }; let mut tokens = s.split_whitespace(); input_inner! { tokens, $($r)* } }; } #[allow(unused_macros)] macro_rules! input_inner { ($tokens:expr) => {}; ($tokens:expr,) => {}; ($tokens:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($tokens, $t); input_inner! { $tokens $($r)* } }; } #[allow(unused_macros)] macro_rules! read_value { ($tokens:expr, ( $($t:tt),* )) => { $(read_value!($tokens, $t)),* }; ($tokens:expr, [ $t:tt; $len:expr ]) => { (0..$len).map(|_| read_value!($tokens, $t)).collect::>() }; ($tokens:expr, chars) => { read_value!($tokens, String).chars().collect::>() }; ($tokens:expr, usize1) => { read_value!($tokens, usize) - 1 }; ($tokens:expr, $t:ty) => { $tokens.next().unwrap().parse::<$t>().expect("parse error") }; } use std::f64; fn chmin(xmin: &mut T, x: T) -> bool where T: PartialOrd + Copy { if x < *xmin { *xmin = x; true } else { false } } fn dist(x1: i32, y1: i32, x2: i32, y2: i32) -> f64 { let x1 = f64::from(x1); let y1 = f64::from(y1); let x2 = f64::from(x2); let y2 = f64::from(y2); let dx = x1 - x2; let dy = y1 - y2; (dx*dx + dy*dy).sqrt() } fn main() { input! { H: i32, W: i32, M: [chars; H], } let it1 = (0..W).map(|x| ( x,-1)); let it2 = (0..W).map(|x| ( x, H)); let it3 = (0..H).map(|y| (-1, y)); let it4 = (0..H).map(|y| ( W, y)); let it = it1.chain(it2).chain(it3).chain(it4); let mut ans = f64::MAX; for (px,py) in it { let mut cur = 0.0; for y in 0..H { for x in 0..W { if M[y as usize][x as usize] == '1' { cur += dist(px,py,x,y); } }} chmin(&mut ans, cur); } println!("{}", ans); }