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; } let n = n+1; rooms.insert(0, vec![0; m]); let mut dp = vec![vec![INF; m]; n]; for i in 0..m { dp[0][i] = 0; } 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-1][j] + rooms[i][j]); } } println!("{}", dp[n-1].iter().min().unwrap()); }