#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;
//int dp[3][N], a[3][N], levodesno[3][N], goredole[3][N], goredole[3][N], sumld[3][N], sumgd[3][N];



void solve() {
    int n, m; cin >> n >> m;
    vector<vector<ll>> dp(n+3), a(n+3), levodesno(n+3), goredole(n+3), sumld(n+3), sumgd(n+3);
    for (int i = 0; i < n+3; i++) {
        dp[i].resize(m+3, 0); levodesno[i].resize(m+3, 0); sumld[i].resize(m+3, 0);
        a[i].resize(m+3, 0); goredole[i].resize(m+3, 0); sumgd[i].resize(m+3, 0);
    }
    for (int i = 1; i <= n; i++) {
        dp[i][0] = inf;
        dp[0][i] = inf;
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            sumld[i][j] = sumld[i][j-1] + a[i][j];
            sumgd[i][j] = sumgd[i-1][j] + a[i][j];
        }
    }
    dp[0][0] = inf;
    dp[1][1] = 0;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            levodesno[i][j] = levodesno[i][j-1] + sumld[i][j-1];
            goredole[i][j] = goredole[i-1][j] + sumgd[i-1][j];
            if(i == 1 && j == 1) continue;
            dp[i][j] = min(dp[i-1][j] + levodesno[i][j], dp[i][j-1] + goredole[i][j]);
            //dbg(levodesno[i][j], i, j);
        }
    }

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

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