#include <bits/stdc++.h>
using namespace std;
#define int long long
//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 = 23;
const int inf = 1e18;



void solve() {
    int n, m; cin >> n >> m;
    vector<vector<int>> dp(n+23), a(n+23), levodesno(n+23), goredole(n+23), sumld(n+23), sumgd(n+23);
    for (int i = 0; i < n+23; i++) {
        dp[i].resize(m+23); levodesno[i].resize(m+23); sumld[i].resize(m+23);
        a[i].resize(m+23); goredole[i].resize(m+23); sumgd[i].resize(m+23);
    }
    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] = (j-1 >= 0 ? levodesno[i][j-1] : 0) + sumld[i][j-1];
            goredole[i][j] = (i-1 >= 0 ? goredole[i-1][j] : 0) + 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();
    }
}