#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;
}
I2luY2x1ZGUgPGZzdHJlYW0+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHF1ZXVlPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiB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hpbGUgKCBEW1EuZnJvbnQoKS54XVtRLmZyb250KCkueV0gIT0gUS5mcm9udCgpLmMgKQogICAgICAgIHsKICAgICAgICAgICAgUS5wb3AoKTsKICAgICAgICAgICAgaWYgKCBRLmVtcHR5KCkgKSBicmVhazsKICAgICAgICB9CiAgICAgICAgaWYgKCBRLmVtcHR5KCkgKSBicmVhazsKCiAgICAgICAgbm9kZSBub3cgPSBRLmZyb250KCk7CiAgICAgICAgUS5wb3AoKTsKCiAgICAgICAgZm9yIChpbnQgZD0wO2Q8NDsrK2QpCiAgICAgICAgewogICAgICAgICAgICBpbnQgeCA9IG5vdy54K2R4W2RdOwogICAgICAgICAgICBpbnQgeSA9IG5vdy55K2R5W2RdOwogICAgICAgICAgICBpZiAoIGluc2lkZSh4LHkpICkKICAgICAgICAgICAgICAgIGlmICggRFt4XVt5XSA+IG1heCggRFtub3cueF1bbm93LnldICwgYVt4XVt5XSApICkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBEW3hdW3ldID0gbWF4KCBEW25vdy54XVtub3cueV0gLCBhW3hdW3ldICk7CiAgICAgICAgICAgICAgICAgICAgUS5wdXNoKCBtYWtlX25vZGUoIHggLCB5ICwgbWF4KCBEW25vdy54XVtub3cueV0gLCBhW3hdW3ldICkpICk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpCnsKICAgIEY+Pm4+Pm07CiAgICBmb3IgKGludCBpPTI7aTw9bisxOysraSkKICAgICAgICBmb3IgKGludCBqPTI7ajw9bSsxOysraikKICAgICAgICAgICAgRj4+YVtpXVtqXTsKICAgIG4gKz0gMjsKICAgIG0gKz0gMjsKCiAgICBmb3IgKGludCBpPTE7aTw9bjsrK2kpCiAgICAgICAgZm9yIChpbnQgaj0xO2o8PW07KytqKQogICAgICAgICAgICBEW2ldW2pdID0gaW5maTsKCiAgICBCZWxsbWFuKDEsMSk7CiAgICBmb3IgKGludCBpPTI7aTw9bi0xOysraSkKICAgICAgICBmb3IgKGludCBqPTI7ajw9bS0xOysraikKICAgICAgICAgICAgdm9sdW1lICs9IERbaV1bal0gLSBhW2ldW2pdOwoKICAgIEc8PHZvbHVtZTw8J1xuJzsKCiAgICBGLmNsb3NlKCk7CiAgICBHLmNsb3NlKCk7CiAgICByZXR1cm4gMDsKfQ==