#include <bits/stdc++.h>

#define int long long

#define pb push_back

const int N = 2e5 + 10;

const int inf = 2e9;

using namespace std;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,m;
    cin >> n >> m;
    vector<vector<int>>a(n + 1,vector<int>(m + 1));
    vector<vector<int>>rows(n + 1,vector<int>(m + 1));
    vector<vector<int>>cols(m + 1,vector<int>(n + 1));
    vector<vector<int>>dp(n + 1,vector<int>(m + 1));
    vector<vector<int>>pref1(n + 1,vector<int>(m + 1));
    vector<vector<int>>pref2(m + 1,vector<int>(n + 1));
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            cin >> a[i][j];
            rows[i][j] = rows[i][j - 1] + a[i][j];
            pref1[i][j] = pref1[i][j - 1] + rows[i][j];
        }
    }
    for(int j = 1; j <= m; j++) {
        for(int i = 1; i <= n; i++) {
            cols[j][i] = cols[j][i - 1] + a[i][j];
            pref2[j][i] = pref2[j][i - 1] + cols[j][i];
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            dp[i][j] = min(dp[i - 1][j] + pref1[i][j - 1],dp[i][j - 1] + pref2[j][i - 1]);
        }
    }
    cout << dp[n][m];
    return 0;
}