const maxn = 700;
var n,m:word;
    a: array[0..maxn+1,0..maxn+1] of word;
    visit: array[0..maxn+1,0..maxn+1] of boolean;
procedure enter;
var i,j:word;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(a[i,j]);
      visit[i,j]:=false;
    end;
    readln;
  end;
end;
function dfs(x,y:word):boolean;
var p,pp:boolean;
begin
  p:=true;
  visit[x,y]:=true;
  if (x>1) and (a[x-1,y]>a[x,y]) then p:=false;
  if p and (x<n) and (a[x+1,y]>a[x,y]) then p:=false;
  if p and (y>1) and (a[x,y-1]>a[x,y]) then p:=false;
  if p and (y<m) and (a[x,y+1]>a[x,y]) then p:=false;
  if p and (x>1) and (y>1) and (a[x-1,y-1]>a[x,y]) then p:=false;
  if p and (x>1) and (y<m) and (a[x-1,y+1]>a[x,y]) then p:=false;
  if p and (x<n) and (y>1) and (a[x+1,y-1]>a[x,y]) then p:=false;
  if p and (x<n) and (y<m) and (a[x+1,y+1]>a[x,y]) then p:=false;
  if not p then
  begin
    dfs:=p;
    if (x>1) and (a[x-1,y]=a[x,y]) and (not visit[x-1,y]) then dfs(x-1,y);
    if (x<n) and (a[x+1,y]=a[x,y]) and (not visit[x+1,y]) then dfs(x+1,y);
    if (y>1) and (a[x,y-1]=a[x,y]) and (not visit[x,y-1]) then dfs(x,y-1);
    if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then dfs(x,y+1);
    if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
    if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
    if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
    if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  end;
  if (x>1) and (a[x-1,y]=a[x,y]) and (not visit[x-1,y]) then p:=p and dfs(x-1,y);
  if not p then
  begin
    dfs:=p;
    if (x<n) and (a[x+1,y]=a[x,y]) and (not visit[x+1,y]) then dfs(x+1,y);
    if (y>1) and (a[x,y-1]=a[x,y]) and (not visit[x,y-1]) then dfs(x,y-1);
    if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then dfs(x,y+1);
    if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
    if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
    if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
    if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  end;
  if (x<n) and (a[x+1,y]=a[x,y]) and (not visit[x+1,y]) then p:=p and dfs(x+1,y);
  if not p then
  begin
    dfs:=p;
    if (y>1) and (a[x,y-1]=a[x,y]) and (not visit[x,y-1]) then dfs(x,y-1);
    if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then dfs(x,y+1);
    if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
    if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
    if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
    if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  end;
  if (y>1) and (a[x,y-1]=a[x,y]) and (not visit[x,y-1]) then p:=p and dfs(x,y-1);
  if not p then
  begin
    dfs:=p;
    if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then dfs(x,y+1);
    if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
    if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
    if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
    if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  end;
  if (y<m) and (a[x,y+1]=a[x,y]) and (not visit[x,y+1]) then p:=p and dfs(x,y+1);
  if not p then
  begin
    dfs:=p;
    if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then dfs(x-1,y-1);
    if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
    if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
    if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  end;
  if (x>1) and (y>1) and (a[x-1,y-1]=a[x,y]) and (not visit[x-1,y-1]) then p:=p and dfs(x-1,y-1);
  if not p then
  begin
    dfs:=p;
    if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then dfs(x-1,y+1);
    if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
    if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  end;
  if (x>1) and (y<m) and (a[x-1,y+1]=a[x,y]) and (not visit[x-1,y+1]) then p:=p and dfs(x-1,y+1);
  if not p then
  begin
    dfs:=p;
    if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then dfs(x+1,y-1);
    if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  end;
  if (x<n) and (y>1) and (a[x+1,y-1]=a[x,y]) and (not visit[x+1,y-1]) then p:=p and dfs(x+1,y-1);
  if not p then
  begin
    dfs:=p;
    if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then dfs(x+1,y+1);
  end;
  if (x<n) and (y<m) and (a[x+1,y+1]=a[x,y]) and (not visit[x+1,y+1]) then p:=p and dfs(x+1,y+1);
  dfs:=p;
end;
procedure initvisit;
var i,j:word;
begin
  for i:=1 to n do
  for j:=1 to m do
  visit[i,j]:=false;
end;
procedure printvisit;
var i,j:word;
begin
  for i:=1 to n do
  begin
    for j:=1 to m do
    write(visit[i,j]:5,' ');
    writeln;
  end;
end;
procedure solve;
var i,j,res:word;
    p:boolean;
begin
  res:=0;
  for i:=1 to n do
  for j:=1 to m do
  if not visit[i,j] then
  if dfs(i,j) then 
  begin
    inc(res);
  end;
  writeln(res);
end;
begin
  enter;
  solve;
end.

  
  