#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int inf = 2147483647;


int n, m;
vector<vector<int> > a;
vector<vector<long long> > h, v;

void fv(){
    for(int j = 0; j < m; ++j){
        for(int i = 0; ++i < n;){
            v[i][j] = v[i-1][j] + a[i-1][j];
        }
    }
    for(int i = 0; i < n-1; ++i){
        for(int j = 0; j < m; ++j){
            v[i+1][j] += v[i][j];
        }
    }
}
void fh(){
    for(int i = 0; i < n; ++i){
        for(int j = 0; ++j < m;){
            h[i][j] = h[i][j-1] + a[i][j-1];
        }
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m-1; ++j){
            h[i][j+1] += h[i][j];
        }
    }
}

long long f(){
    vector<vector<long long> > dp(n, vector<long long>(m, 0ll));
    /*for(int i = 0; ++i < n;){
        dp[i][0] = v[i][0];
    }
    for(int i = 0; ++i < m;){
        dp[0][i] = h[0][i];
    }*/
    for(int i = 0; ++i < n;){
        for(int j = 0; ++j < m;){
            dp[i][j] = min(dp[i-1][j] + h[i][j],
                           dp[i][j-1] + v[i][j]);
        }
    }
    //cout << "dp \n"; for(int i = 0; i < n; ++i){for(int j = 0; j < m; ++j) cout << dp[i][j] << ' '; cout << '\n';}
    return dp[n-1][m-1];
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    a = vector<vector<int> >(n, vector<int>(m));
    h = vector<vector<long long> >(n, vector<long long>(m,0ll));
    v = vector<vector<long long> >(n, vector<long long>(m,0ll));
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            cin >> a[i][j];
        }
    }
    fh(); fv();
    cout << f();

    //cout << "a \n"; for(int i = 0; i < n; ++i){for(int j = 0; j < m; ++j) cout << a[i][j] << ' '; cout << '\n';}
    //cout << "h \n"; for(int i = 0; i < n; ++i){for(int j = 0; j < m; ++j) cout << h[i][j] << ' '; cout << '\n';}
    //cout << "v \n"; for(int i = 0; i < n; ++i){for(int j = 0; j < m; ++j) cout << v[i][j] << ' '; cout << '\n';}
    return 0;
}