use std::io::{Read, stdin}; fn main() { let mut buf = String::new(); stdin().read_to_string(&mut buf).unwrap(); let mut tok = buf.split_whitespace(); let mut get = || tok.next().unwrap(); macro_rules! get { ($t:ty) => (get().parse::<$t>().unwrap()); () => (get!(i64)); } let h = get!(); let w = get!(); let mut paper = vec![]; for _ in 0..h { paper.push(get().as_bytes()); } let mut ans = std::f64::MAX; for y0 in 0..h { let tmp = calc(h, w, 0, y0+1, &paper); if tmp < ans { ans = tmp; } let tmp = calc(h, w, w+1, y0+1, &paper); if tmp < ans { ans = tmp; } } for x0 in 0..w { let tmp = calc(h, w, x0+1, 0, &paper); if tmp < ans { ans = tmp; } let tmp = calc(h, w, x0+1, h+1, &paper); if tmp < ans { ans = tmp; } } println!("{}", ans); } fn calc(h: i64, w: i64, x0: i64, y0: i64, paper: &Vec<&[u8]>) -> f64 { let mut ret = 0.0; for y1 in 0..h { for x1 in 0..w { if paper[y1 as usize][x1 as usize] == b'1' { ret += dist(x0, y0, x1+1, y1+1); } } } ret } fn dist(x0: i64, y0: i64, x1: i64, y1: i64) -> f64 { let dx = (x1 - x0) as f64; let dy = (y1 - y0) as f64; (dx * dx + dy * dy).sqrt() }