#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) { cerr << to_string(h); if(sizeof...(t)) cerr << ", "; DBG(t...);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)

const int N = 1e6 + 3;
const ll inf = 1e18;
ll dp[2][N], a[2][N], levodesno[2][N], goredole[2][N], sumld[2][N], sumgd[2][N];



void solve() {
    int n, m; cin >> n >> m;
    int red = 1;
    for (int i = 1; i <= n; i++) {
        red ^= 1;
        for (int j = 1; j <= m; j++) {
            ll aij; cin >> aij;
            sumld[red][j] = sumld[red][j-1] + aij;
            sumgd[red][j] = sumgd[red^1][j] + aij;
            levodesno[red][j] = levodesno[red][j-1] + sumld[red][j-1];
            goredole[red][j] = goredole[red^1][j] + sumgd[red^1][j];
            if(i == 1 && j == 1) dp[red][j] = 0;
            else
                dp[red][j] = min((i-1>= 1 ? dp[red^1][j] + levodesno[red][j] : inf), (j-1 >= 1 ? dp[red][j-1] + goredole[red][j] : inf));
        }
    }


    cout << dp[red][m] << "\n";
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int t=1; //cin >> t;
    while (t--) {
        solve();
    }
}