#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cerr.tie(nullptr);
    int n, m;
    cin >> n >> m;
    int a[n][m];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        cin >> a[i][j];
    unsigned long long levi[n][m];
    unsigned long long gornji[n][m];
    unsigned long long dp[n][m];
    for (int i = 0; i < n; i++) {
        levi[i][0] = 0;
        dp[i][0] = 0;
    }
    for (int j = 0; j < m; j++) {
        gornji[0][j] = 0;
        dp[0][j] = 0;
    }
    for (int i = 0; i < n; i++) {
        unsigned long long zbir = 0;
        for (int j = 1; j < m; j++) {
            zbir += a[i][j - 1];
            levi[i][j] = levi[i][j - 1] + zbir;
        }
    }
    for (int j = 0; j < m; j++) {
        unsigned long long zbir = 0;
        for (int i = 1; i < n; i++) {
            zbir += a[i - 1][j];
            gornji[i][j] = gornji[i - 1][j] + zbir;
        }
    }
    for (int i = 1; i < n; i++)
        for (int j = 1; j < m; j++)
        dp[i][j] = min(dp[i - 1][j] + levi[i][j], dp[i][j - 1] + gornji[i][j]);
    cout << dp[n - 1][m - 1] << '\n';
    return 0;
}