fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { read::().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } fn read_vec2(n: usize) -> Vec> { (0..n).map(|_| read_vec()).collect() } fn main() { let nm: Vec = read_vec(); let n = nm[0]; let m = nm[1]; let mut a: Vec> = read_vec2(n); let mut ans = 0; for _i in 0..m { let mut smallest = 9999999; let mut pos: (usize, usize, usize) = (999, 999, 999); for j in 0..n { let mut x = 0; let mut y = 0; for k in 0..n { x += a[j][k]; y += a[k][j]; } if smallest > x && x != 0 { smallest = x; pos = (j, 999, 999); } if smallest > y && y != 0 { smallest = y; pos = (999, j, 999); } } let mut z1 = 0; let mut z2 = 0; for k in 0..n { z1 += a[k][k]; z2 += a[k][n - k - 1]; } if smallest > z1 && z1 != 0 { smallest = z1; pos = (999, 999, 1); } if smallest > z2 && z2 != 0 { smallest = z2; pos = (999, 999, 2); } ans += smallest; if pos.0 != 999 { for k in 0..n { a[pos.0][k] = 0; } } else if pos.1 != 999 { for k in 0..n { a[k][pos.1] = 0; } } else if pos.2 == 1 { for k in 0..n { a[k][k] = 0; } } else { for k in 0..n { a[k][n - k - 1] = 0; } } } println!("{}", ans); }