Problem: One fine day Sasha went to the park for a walk. In the park, he saw that his favorite bench is occupied, and he had to sit down on the neighboring one. He sat down and began to listen to the silence. Suddenly, he got a question: what if in different parts of the park, the silence sounds in different ways? So it was. Let's divide the park into 1 × 1 meter squares and call them cells, and numerate rows from 1 to n from up to down, and columns from 1 to m from left to right. And now, every cell can be described with a pair of two integers (x, y), where x — the number of the row, and y — the number of the column. Sasha knows that the level of silence in the cell (i, j) equals to f_{i,j}, and all f_{i,j} form a permutation of numbers from 1 to n ⋅ m. Sasha decided to count, how many are there pleasant segments of silence?

Let's take some segment [l … r]. Denote S as the set of cells (i, j) that l ≤ f_{i,j} ≤ r. Then, the segment of silence [l … r] is pleasant if there is only one simple path between every pair of cells from S (path can't contain cells, which are not in S). In other words, set S should look like a tree on a plain. Sasha has done this task pretty quickly, and called the algorithm — "algorithm of silence's sounds".

Time passed, and the only thing left from the algorithm is a legend. To prove the truthfulness of this story, you have to help Sasha and to find the number of different pleasant segments of silence. Two segments [l_1 … r_1], [l_2 … r_2] are different, if l_1 ≠ l_2 or r_1 ≠ r_2 or both at the same time.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 1000, 1 ≤ n ⋅ m ≤ 2 ⋅ 10^5) — the size of the park.

Each from next n lines contains m integers f_{i,j} (1 ≤ f_{i,j} ≤ n ⋅ m) — the level of silence in the cell with number (i, j).

It is guaranteed, that all f_{i,j} are different.

Output

Print one integer — the number of pleasant segments of silence.

Examples

Input

1 5
1 2 3 4 5


Output

15

Input

2 3
1 2 3
4 5 6


Output

15

Input

4 4
4 3 2 16
1 13 14 15
5 7 8 12
6 11 9 10


Output

50

Note

In the first example, all segments of silence are pleasant.

In the second example, pleasant segments of silence are the following:

<image>

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
auto& errStream = cerr;
class CerrDummy {
} cerrDummy;
template <class T>
CerrDummy& operator<<(CerrDummy& cd, const T&) {
  return cd;
}
using charTDummy = char;
using traitsDummy = char_traits<charTDummy>;
CerrDummy& operator<<(CerrDummy& cd,
                      basic_ostream<charTDummy, traitsDummy>&(
                          basic_ostream<charTDummy, traitsDummy>&)) {
  return cd;
}
using pi = pair<ll, ll>;
using vi = vector<ll>;
using ld = long double;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (ll i = ll(0); i < ll((ll)v.size()); i++) {
    if (i) os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
template <ll i, class T>
void print_tuple(ostream&, const T&) {}
template <ll i, class T, class H, class... Args>
void print_tuple(ostream& os, const T& t) {
  if (i) os << ",";
  os << get<i>(t);
  print_tuple<i + 1, T, Args...>(os, t);
}
template <class... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
  os << "(";
  print_tuple<0, tuple<Args...>, Args...>(os, t);
  return os << ")";
}
ll read() {
  ll i;
  scanf("%" SCNd64, &i);
  return i;
}
void printSpace() { printf(" "); }
void printEoln() { printf("\n"); }
void print(ll x, ll suc = 1) {
  printf("%" PRId64, x);
  if (suc == 1) printEoln();
  if (suc == 2) printSpace();
}
template <class T>
void print(const vector<T>& v) {
  for (ll i = ll(0); i < ll(v.size()); i++)
    print(v[i], i == ll(v.size()) - 1 ? 1 : 2);
}
string readString() {
  static char buf[3341000];
  scanf("%s", buf);
  return string(buf);
}
char* readCharArray() {
  static char buf[3341000];
  static ll bufUsed = 0;
  char* ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}
template <class T, class U>
void chmax(T& a, U b) {
  if (a < b) a = b;
}
template <class T, class U>
void chmin(T& a, U b) {
  if (b < a) a = b;
}
template <class T>
T Sq(const T& t) {
  return t * t;
}
void Yes(bool ex = true) {
  cout << "YES" << endl;
  if (ex) exit(0);
}
void No(bool ex = true) {
  cout << "NO" << endl;
  if (ex) exit(0);
}
const ll infLL = LLONG_MAX / 3;
const ll inf = infLL;
constexpr ll TEN(ll n) { return n == 0 ? 1 : TEN(n - 1) * 10; }
template <class T>
vector<T> Uniqued(const vector<T>& vv) {
  auto v(vv);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  return v;
}
template <class T>
void MakeUniqued(vector<T>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
struct Node {
  typedef Node* NP;
  NP l, r, p;
  bool rev;
  Node() : l(NULL), r(NULL), p(NULL), rev(false) {}
  void Propagate() {
    if (rev) {
      swap(l, r);
      if (l) l->rev ^= true;
      if (r) r->rev ^= true;
      rev = false;
    }
  }
  void Update() {}
  ll Pos() {
    if (p && p->l == this) return -1;
    if (p && p->r == this) return 1;
    return 0;
  }
  void Prepare() {
    if (Pos()) p->Prepare();
    Propagate();
  }
  void Rotate() {
    NP q = p, c;
    if (Pos() == 1) {
      c = l;
      l = p;
      p->r = c;
    } else {
      c = r;
      r = p;
      p->l = c;
    }
    if (c) c->p = p;
    p = p->p;
    q->p = this;
    if (p && p->l == q) p->l = this;
    if (p && p->r == q) p->r = this;
    q->Update();
  }
  void Splay() {
    Prepare();
    while (Pos()) {
      ll a = Pos(), b = p->Pos();
      if (b && a == b) p->Rotate();
      if (b && a != b) Rotate();
      Rotate();
    }
    Update();
  }
  void Expose() {
    for (NP x = this; x; x = x->p) x->Splay();
    for (NP x = this; x->p; x = x->p) {
      x->p->r = x;
      x->p->Update();
    }
    Splay();
  }
  void Evert() {
    Expose();
    if (l) {
      l->rev ^= true;
      l = NULL;
      Update();
    }
  }
  void Link(NP x) {
    Evert();
    p = x;
  }
  void Cut() {
    Expose();
    assert(l);
    l->p = NULL;
    l = NULL;
    Update();
  }
};
bool Same(Node* a, Node* b) {
  a->Expose();
  b->Expose();
  if (!a->p) return false;
  return true;
}
const ll Nmax = 200010;
Node buf[Nmax];
Node* NewNode() {
  static ll bufUsed = 0;
  return buf + bufUsed++;
}
struct SegTree {
  ll s;
  vi lz, mn, cnt;
  SegTree(ll n) {
    s = 1;
    while (s < n) s *= 2;
    lz.resize(s * 2, 0);
    mn.resize(s * 2, 0);
    cnt.resize(s * 2, 1);
    for (ll i = s - 1; i >= 1; i--) cnt[i] = cnt[i * 2] + cnt[i * 2 + 1];
  }
  ll GetMin(ll i) { return mn[i] + lz[i]; }
  void Propagate(ll i) {
    lz[i * 2] += lz[i];
    lz[i * 2 + 1] += lz[i];
    mn[i] += lz[i];
    lz[i] = 0;
  }
  void Update(ll i) {
    assert(lz[i] == 0);
    ll x = GetMin(i * 2);
    ll y = GetMin(i * 2 + 1);
    mn[i] = min(x, y);
    cnt[i] = 0;
    if (mn[i] == x) cnt[i] += cnt[i * 2];
    if (mn[i] == y) cnt[i] += cnt[i * 2 + 1];
  }
  void Add(ll b, ll e, ll v, ll l, ll r, ll i) {
    if (e <= l || r <= b) return;
    if (b <= l && r <= e) {
      lz[i] += v;
      return;
    }
    Propagate(i);
    Add(b, e, v, l, (l + r) / 2, i * 2);
    Add(b, e, v, (l + r) / 2, r, i * 2 + 1);
    Update(i);
  }
  void Add(ll b, ll e, ll v) {
    if (e <= b) return;
    cerrDummy << b << " " << e << " " << v << endl;
    Add(b, e, v, 0, s, 1);
  }
  ll Calc(ll b, ll e, const ll v, ll l, ll r, ll i) {
    if (e <= l || r <= b) return 0;
    if (b <= l && r <= e) {
      if (GetMin(i) == v)
        return cnt[i];
      else
        return 0;
    }
    Propagate(i);
    ll ans = 0;
    ans += Calc(b, e, v, l, (l + r) / 2, i * 2);
    ans += Calc(b, e, v, (l + r) / 2, r, i * 2 + 1);
    return ans;
  }
  ll Calc(ll b, ll e, const ll v) { return Calc(b, e, v, 0, s, 1); }
};
signed main() {
  ll n = read(), m = read();
  ll s = n * m;
  vector<vi> f(n, vi(m));
  vector<pi> pos(s);
  for (ll i = ll(0); i < ll(n); i++)
    for (ll j = ll(0); j < ll(m); j++) {
      f[i][j] = read() - 1;
      pos[f[i][j]] = pi(i, j);
    }
  const auto get = [&](ll x, ll y) {
    if (0 <= x && x < n && 0 <= y && y < m) return f[x][y];
    return s;
  };
  SegTree seg(s);
  ll ans = 0, lim = s;
  vector<vi> es(s);
  for (ll i = s - 1; i >= 0; i--) {
    ll x = pos[i].first, y = pos[i].second;
    seg.Add(i, s, 1);
    const ll dxy[] = {-1, 0, 1, 0, -1};
    for (ll k = ll(0); k < ll(4); k++) {
      ll nx = x + dxy[k], ny = y + dxy[k + 1];
      ll w = get(nx, ny);
      if (i < w && w < s) {
        seg.Add(w, s, -1);
        while (w < lim) {
          if (!Same(buf + i, buf + w)) break;
          ll z = lim - 1;
          buf[z].Evert();
          for (auto j : es[z]) buf[j].Cut();
          lim--;
        }
        if (w < lim) {
          es[w].push_back(i);
          buf[i].Evert();
          buf[i].Link(buf + w);
        }
      }
    }
    cerrDummy << lim << endl;
    ll r = seg.Calc(i, lim, 1);
    cerrDummy << i << " " << r << endl;
    ans += r;
  }
  print(ans);
}