#include <bits/stdc++.h>
using namespace std;
int N, M;

void solve() {
    cin>>N>>M;
    int sum = 0;
    int dp[N][M];
    int a;
    for(int i=0;i<N;++i) {
        for(int j=0;j<M;++j) {
            cin>>a;
            if (i == 0 || j == 0)
                dp[i][j] = 0;
            else dp[i][j] = a + max(dp[i-1][j], dp[i][j-1]);
            sum += a;
        }
    }

    cout<<sum - dp[N-1][M-1]<<"\n";
}

int main(void){
    ios::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}