#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using str = string;

using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)


void solve(){
    int n, m;
    cin >> n >> m;
    vvl v(n, vl(m));
    F0R(i, n) F0R(j, m) cin >> v[i][j];
    vvl levo(n, vl(m)), gore(n, vl(m)), dp(n, vl(m));
    F0R(i, n){
        ll sum = v[i][0];
        FOR(j, 1, m){
            levo[i][j] = levo[i][j - 1] + sum;
            sum += v[i][j];
        }
    }
    F0R(j, m){
        ll sum = v[0][j];
        FOR(i, 1, n){
            gore[i][j] = gore[i - 1][j] + sum;
            sum += v[i][j];
        }
    }
    //F0R(i, n)dp[i][0] = gore[i][0];
    //F0R(j, m)dp[0][j] = levo[0][j];
    FOR(i, 1, n){
        FOR(j, 1, m){
            dp[i][j] = min(dp[i - 1][j] + levo[i][j], dp[i][j - 1] + gore[i][j]);
        }
    }
    /*F0R(i, n){
        F0R(j, m){
            cout << dp[i][j] << " ";
        }
        cout << "\n";
    }*/
    cout << dp[n - 1][m - 1] << "\n";
}


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