import std.stdio;
import std.algorithm;
import std.range;
import std.typecons;

void main(){
    int h, w;
    while(~scanf("%d%d", &h, &w)){
        int[1010][1010] to;
        int[1010][1010] g;
        alias Tuple!(int,int) idx;
        idx[] ds;
        foreach(i; 0..h){
            foreach(j; 0..w){
                scanf("%d", &g[i][j]);
                ds ~= idx(i, j);
            }
        }
        ds.sort!((a, b) => g[a[0]][a[1]] < g[b[0]][b[1]]);

        foreach(r; to) r[] = -1;

        int ans = 0;
        foreach(t; ds){
            int i = t[0], j = t[1];
            int[] s = [];
            foreach(d; [[0,1],[0,-1],[1,0],[-1,0]]){
                int ni = i + d[0];
                int nj = j + d[1];
                if(0 <= ni && ni < h && 0 <= nj && nj < w){
                    if(g[ni][nj] < g[i][j]){
                        s ~= to[ni][nj];
                    }
                }
            }
            s = uniq(sort(s)).array;
            if(s.length == 0){
                to[i][j] = g[i][j];
            } else if(s.canFind(-10)){
                to[i][j] = -10;
            } else {
                to[i][j] = s.length == 1 ? s[0] : -10;
            }
            ans += to[i][j] == -10;
        }
        writeln(ans);
    }
}