const INF: usize = 1usize << 60; fn main() { let mut nm = String::new(); std::io::stdin().read_line(&mut nm).ok(); let nm: Vec = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nm[0]; let m = nm[1]; let mut rooms = (0..n).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); temp }) .collect::>(); if n == 1 { println!("0"); return; } rooms.insert(0, vec![0usize; m]); let n = n+1; let mut dp = vec![vec![INF; m]; n]; dp[0] = vec![0usize; m]; for i in 1..n { let minval = *dp[i-1].iter().min().unwrap(); for j in 0..m { dp[i][j] = dp[i][j].min(dp[i-1][j] + rooms[i][j]); dp[i][j] = dp[i][j].min(minval + rooms[i][j] + rooms[i-1][j]) } } println!("{}", dp[n-1].iter().min().unwrap()); }