#include<bits\stdc++.h>


using namespace std;


#define ll long long
#define ld long double
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f first
#define s second



int main()
{

    SYNC
    int n, m;
    cin >> n >> m;

    /*ll mat[n + 1][m + 1];
    ll pref[n + 2][m + 2];
    ll suff[n + 2][m + 2];
    ll dp[n + 2][m + 2];*/

    vector<vector<ll>> mat(n + 1, vector<ll>(m + 1));
    vector<vector<ll>> pref(n + 2, vector<ll>(m + 1, 0));
    vector<vector<ll>> suff(n + 2, vector<ll> (m + 1, 0 ));
    vector<vector<ll>> dp(n + 2, vector<ll>(m + 1, 0));

    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= m; j ++)
        {
            cin >> mat[i][j];
        }
    }


  /*  for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= m; j ++)
        {
            if(j == 1)pref[i][j] = 0;
            else
                pref[i][j] += (pref[i][j - 1] * 2 + mat[i][j - 1]);
        }

        for(int j = m; j >= 1; j --)
        {
            if(j == m)
                suff[i][j] = 0;
            else
                suff[i][j] += (suff[i][j + 1] * 2 + mat[i][j + 1]);
        }
    }*/

    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= m; j++)
        {

            ll tr = 0;
            for(int k = 1; k < j; k ++)
            {
                tr += ( (j - k) * mat[i][k] );
            }
            pref[i][j] = tr;

            tr = 0;

            for(int k = m; k > j; k --)
            {
                tr += ( (k - j) * mat[i][k] );
            }
            suff[i][j] = tr;

        }

    }





  /*  for(int i = 1; i <= n; i ++)
    {


        for(int j = 1; j <= m; j ++)
        {
            cout << suff[i][j] << " ";
        }
        cout << "\n";

        for(int j = 1; j <= m; j ++)cout << pref[i][j] << " ";
        cout << "\n\n";

    }*/

    dp[1][1] = suff[1][1] + pref[1][1];

    for(int i = 2; i <= m; i ++)
    {
        dp[1][i] = suff[1][i] + pref[1][i];
    }

    for(int i = 2; i <= n; i ++)
    {
        dp[i][1] = dp[i - 1][1] + suff[i][1];
    }

    for(int i = 2; i <= n; i++)
    {
        for(int j = 2; j <= m; j ++)
        {

            ll gore = dp[i - 1][j] + suff[i][j] + pref[i][j];
            ll levo = dp[i][j - 1] - mat[i][j];
            dp[i][j] = min(levo, gore);
        }
    }

  /*  for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= m; j ++)
        {
            cout << dp[i][j] << " ";
        }
        cout << "\n";
    }*/

    cout << dp[n][m];









    return 0;
}