#include<cstdio>
#include<queue>
#include<memory.h>
#include<vector>
#include<algorithm>
using namespace std;
const int dr[] = { -1,-1,-1, 0, 0, 1,1,1 };
const int dc[] = { -1, 0, 1,-1, 1,-1,0,1 };
const int M = 111;
int row, col;
int im[M][M];
int chk[M][M];
struct pos {
int h;
int r, c;
pos() {}
pos(int ir, int ic) :r(ir), c(ic) {}
pos(int ir, int ic, int ih) :r(ir), c(ic), h(ih) {}
};
bool range_over(int r, int c) {
if (r < 0 || row <= r || c < 0 || col <= c) return true;
return false;
}
bool cmp(pos& a, pos& b) {
if (a.h > b.h) return true;
return false;
}
int main() {
scanf("%d%d", &row, &col);
vector<pos> p;
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++) {
scanf("%d", &im[r][c]);
p.push_back(pos(r, c, im[r][c]));
}
}// input
memset(chk, -1, sizeof(chk));
sort(p.begin(), p.end(), cmp);
for (auto it : p) {
if (chk[it.r][it.c] == -1) chk[it.r][it.c] = 1;
else continue;
queue<pos> Q = queue<pos>();
Q.push(pos(it.r, it.c, it.h));
int pivot = it.h;
while (!Q.empty()) {
pos here = Q.front();
Q.pop();
for (int i = 0; i < 8; i++) {
int tr = here.r + dr[i];
int tc = here.c + dc[i];
if (chk[tr][tc] != -1) continue;
if (range_over(tr, tc)) continue;
if ((pivot >= im[tr][tc]) &&
(im[tr][tc] <= im[here.r][here.c])) {
chk[tr][tc] = 0;
Q.push(pos(tr, tc));
}
}
}
}
int ans = 0;
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++) {
if (chk[r][c] == 1)ans++;
}
}
printf("%d\n", ans);
return 0;
}