#include<bits/stdc++.h>
using namespace std;
struct slog
{
    int x;
    int y;
};
slog tren, putanja[2100];
int n,m;
int mat[1050][1050];
int dp[1050][1050], brput;
bool pos[1050][1050];
int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin >> mat[i][j];
    }

    for(int k=1; k<=n; k++)
    {
        for(int l=1; l<=m; l++)
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    if(pos[i][j] == 0)
                    {
                        //cout << mat[i][j] << " " << (abs(i-k) + abs(j-l)) << ", ";
                        dp[k][l] += mat[i][j] * (abs(i-k) + abs(j-l));
                    }
                }
                //cout << endl << dp[k][l] << endl;;
            }
            pos[k][l] = 1;
        }
    }
    for(int i = 0; i<=n+1; i++) /// red
    {
        dp[0][i] = 1e9;
        dp[n+1][i] = 1e9;
    }
    for(int i = 0; i<=m+1; i++) /// kolona
    {
        dp[i][0] = 1e9;
        dp[i][n+1] = 1e9;
    }


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

    putanja[1].x = 1;
    putanja[1].y = 1;
    tren.x = 1;
    tren.y = 1;
    brput = 1;
    while(true)
    {
        if(tren.x == n and tren.y == m)
            break;
        brput++;
        if(dp[tren.x+1][tren.y] <= dp[tren.x][tren.y+1])
        {
            tren.x++;
            putanja[brput].x = tren.x;
            putanja[brput].y = tren.y;
        }

        else
        {
            tren.y++;
            putanja[brput].x = tren.x;
            putanja[brput].y = tren.y;
        }
    }

    cout << "0" << endl;
    return 0;
}