#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int h[777][777],sx,sy,n,m,ans;
int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,-1,1,-1,0,1};
struct Node{int x,y;};
bool visit[777][777],flag;
queue <Node> q;
void BFS(){
q.push({sx,sy});
visit[sx][sy] = true;
while(!q.empty()){
Node u = q.front();q.pop();
for(int i = 0 ; i < 8 ; ++i){
int vx = u.x + dx[i];
int vy = u.y + dy[i];
if( vx < 1 or vx > n or vy < 1 or vy > m) continue;
if(!visit[vx][vy] and h[vx][vy] <= h[u.x][u.y] ){
q.push({vx,vy});
visit[vx][vy] = true;
}
else if(h[vx][vy] > h[u.x][u.y] ) flag = false;
}
}
}
int main(){
cin >> n >> m;
for(int i = 1 ; i <= n ;i++)
for(int j = 1 ; j <= m ; j++)
cin >> h[i][j];
for(int i = 1 ; i <= n ; i++)
for(int j = 1; j <= m ; j++){
if( h[i][j] and !visit[i][j]){
flag = true;
sx = i ,sy = j;
BFS();
ans += flag ;
}
}
cout << ans << endl;
}