#include<bits/stdc++.h>

using namespace std;

#define int long long

void solve(){
    int n, m;cin>>n>>m;
    vector<vector<int>> v(n, vector<int>(m)), d(n, vector<int>(m)), px(n+1, vector<int>(m+1)), py(n+1, vector<int>(m+1)), pxultra(n+1, vector<int>(m+1)), pyultra(n+1, vector<int>(m+1));
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            cin>>v[i][j];
        }
    }
    for(int i = 1;i<=n;i++){
        px[i][0] = 0;
        for(int j = 0;j<m;j++){
            px[i][j+1] = px[i][j] + v[i-1][j];
        }
    }
    for(int j = 1;j<=m;j++){
        py[0][j] = 0;
        for(int i = 0;i<n;i++){
            py[i+1][j] = py[i][j] + v[i][j-1];
        }
    }
    for(int i = 1;i<=n;i++){
        pxultra[i][0] = 0;
        for(int j = 0;j<m;j++){
            pxultra[i][j+1] = pxultra[i][j] + px[i][j] + v[i-1][j];
        }
    }
    for(int j = 1;j<=m;j++){
        pyultra[0][j] = 0;
        for(int i = 0;i<n;i++){
            pyultra[i+1][j] = pyultra[i][j] + py[i][j] + v[i][j-1];
        }
    }
    for(int j = 0;j<m;j++) d[0][j] = 0;
    for(int i = 0;i<n;i++) d[i][0] = 0;
    for(int i = 1;i<n;i++){
        for(int j = 1;j<m;j++){
            d[i][j] = min(d[i-1][j]+pxultra[i+1][j], d[i][j-1]+pyultra[i][j+1]);
        }
    }
    cout<<d[n-1][m-1]<<endl;
    return;
}

int32_t main(){
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0;
}