#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; if(h == 1){ cout << 0 << '\n'; return 0; } vector> A(h, vector(w)), dp(h, vector(w)); for(int y = 0; y < h; y++){ for(int x = 0; x < w; x++){ cin >> A[y][x]; } } dp[0] = A[0]; for(int y = 0; y + 1 < h; y++){ ll v = *min_element(dp[y].begin(), dp[y].end()); for(int x = 0; x < w; x++){ dp[y + 1][x] = min(dp[y][x] + A[y + 1][x], A[y][x] + A[y + 1][x] + v); } } cout << *min_element(dp[h - 1].begin(), dp[h - 1].end()) << '\n'; }