#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+3), a(n+3);
    for (int i = 0; i < n+3; i++) {
        dp[i].resize(m+3);
        a[i].resize(m+3);
    }
    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];
    }
    dp[0][0] = inf;
    dp[1][1] = 0;

    auto levodesno = [&](int i, int j) {
        int fact = 1;
        int s = 0;
        for (int k = j; k >= 1; k--) {
            s += a[i][k] * fact;
            fact++;
        }
        return s;
    };
    auto goredole = [&](int i, int j) {
        int fact = 1;
        int s = 0;
        for (int k = i; k >= 1; k--) {
            s += a[k][j] * fact;
            fact++;
        }
        return s;
    };

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

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

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