#include <stdio.h>
#include <string.h>
#include <iostream.h>
#define N 1010
int vis[N][N],height[N][N];
int m,n,cnt;
int move[][2] = {0,1, 0,-1, 1,0, -1,0};
bool ok(int x,int y,int c)
{
return x > 0 && x <= m && y > 0 && y <= n && height[x][y] >= c;
}
bool boundary(int x,int y)
{
return x == 1 || y == 1 || x == m || y == n;
}
void dfs(int x,int y,int c)
{
height[x][y] = 0;
if(boundary(x,y))
++cnt;
int s,t;
for(int i = 0; i < 4; ++i)
{
s = x + move[i][0];
t = y + move[i][1];
if(ok(s,t,c))
dfs(s,t,height[s