object Main extends App {
  import scala.io.StdIn._


  val Array(height, width) = readLine.trim.split(' ').map(_.toInt)
  val state = Array.tabulate(height){_ ⇒ readLine.trim.split(' ').map(_.toInt)}
  val sortedPosition = (0 until height).flatMap(y ⇒ (0 until width).map(x ⇒ Coordinate(x, y))).sortBy(c ⇒ state(c.y)(c.x))
  val isRidge: Array[Array[Option[Int]]] = Array.tabulate(height){_ ⇒ Array.tabulate(width){_ ⇒ None:Option[Int]}}
  var res = 0
  for (c ← sortedPosition){
    val lower = c.neighbors.filter(n ⇒ (0 until height).contains(n.y) && (0 until width).contains(n.x) && state(n.y)(n.x) < state(c.y)(c.x)).map(n ⇒ isRidge(n.y)(n.x))
    if (lower.exists(_.isEmpty) || lower.flatten.toList.distinct.length >= 2){
      res += 1
    }else if (lower.flatten.toList.distinct.length == 1) {
      isRidge(c.y)(c.x) = Some(lower.flatten.head)
    }else {
      isRidge(c.y)(c.x) = Some(state(c.y)(c.x))
    }
  }
  println(res)

  case class Coordinate(x: Int, y: Int) {
    def neighbors: Iterable[Coordinate] = List(Coordinate(0, 1), Coordinate(0, -1), Coordinate(1, 0), Coordinate(-1, 0)).map(this + _)
    def +(that: Coordinate): Coordinate = Coordinate(x + that.x, y + that.y)
  }
  implicit class ScopeFunc[T](val value: T){
    def let[A](func: T ⇒ A): A = func(value)
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}
