#include <iostream>
#include<queue>
using namespace std;

int main()
{
int n,m;
cin>>n>>m;
int mat[n][m];
int s=2e9;
int si,sj;
int roboti=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
cin>>mat[i][j];
}
}
queue<int>q;

bool visited[n][m];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
visited[i][j]=false;
}
}
int di[]= {-1,1,0,0};
int dj[]= {0,0,-1,1};
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
si=-1;
sj=-1;
s=2e9;
for(int x=0; x<n; x++)
{
for(int y=0; y<m; y++)
{
if(mat[x][y]<s && !visited[x][y])
{
s=mat[x][y];
si=x;
sj=y;
}
}
}
if(si!=-1 && sj!=-1)
{
        q.push(si);
        q.push(sj);
roboti+=1;
visited[si][sj]=true;
while(!q.empty())
{
int ci=q.front();
q.pop();
int cj=q.front();
q.pop();
for(int k=0; k<4; k++)
{
int ti=ci+di[k];
int tj=cj+dj[k];
if(ti>=0 && tj>=0 && ti<n && tj<m && !visited[ti][tj] && mat[ti][tj]>=mat[ci][cj])
{
visited[ti][tj]= true;
q.push(ti);
q.push(tj);
}
}

}
}
}
}
cout<<roboti;

return 0;
}
