#include <fstream>
#include <iostream>
#include <queue>
using namespace std;

typedef struct { int x,y,c,p; } node;

struct cmp_node
{
    bool operator () (node x,node y)
    {
        return x.c > y.c;
    }
};

node make_node(int x,int y,int c)
{
    node now;
    now.x = x;
    now.y = y;
    now.c = c;
    return now;
}

node make_node(int x,int y,int c,int p)
{
    node now;
    now.x = x;
    now.y = y;
    now.c = c;
    now.p = p;
    return now;
}

ifstream F("volum.in");
ofstream G("volum.out");

const int N = 760;
const int infi = 1<<30;

int n,m,a[N][N],D[N][N];
long long volume;

int dx[] = { 1 ,-1 , 0 , 0 , 0 };
int dy[] = { 0 , 0 ,-1 , 1 , 0 };

bool inside(int x,int y)
{
    if ( x == 0 || y == 0 || x == n+1 || y == m+1 ) return 0;
    return 1;
}

void Bellman(int sx,int sy)
{
    queue<node> Q;

    D[sx][sy] = 0;
    Q.push( make_node(sx,sy,D[sx][sy]) );

    while ( !Q.empty() )
    {
        while ( D[Q.front().x][Q.front().y] != Q.front().c )
        {
            Q.pop();
            if ( Q.empty() ) break;
        }
        if ( Q.empty() ) break;

        node now = Q.front();
        Q.pop();

        for (int d=0;d<4;++d)
        {
            int x = now.x+dx[d];
            int y = now.y+dy[d];
            if ( inside(x,y) )
                if ( D[x][y] > max( D[now.x][now.y] , a[x][y] ) )
                {
                    D[x][y] = max( D[now.x][now.y] , a[x][y] );
                    Q.push( make_node( x , y , max( D[now.x][now.y] , a[x][y] )) );
                }
        }
    }
}

int main()
{
    F>>n>>m;
    for (int i=2;i<=n+1;++i)
        for (int j=2;j<=m+1;++j)
            F>>a[i][j];
    n += 2;
    m += 2;

    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
            D[i][j] = infi;

    Bellman(1,1);
    for (int i=2;i<=n-1;++i)
        for (int j=2;j<=m-1;++j)
            volume += D[i][j] - a[i][j];

    G<<volume<<'\n';

    F.close();
    G.close();
    return 0;
}