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>
template <class T>
inline void read(T &res) {
  char ch;
  while (ch = getchar(), !isdigit(ch))
    ;
  res = ch ^ 48;
  while (ch = getchar(), isdigit(ch)) res = res * 10 + ch - 48;
}
using std::set;
using std::vector;
const int N = 2e5 + 5;
const int M = 8e5 + 5;
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
int que[N], val[N], px[N], py[N], tag[M], lc[N], rc[N], fa[N], rev[N];
int tot, now_e, qr, n, m, l, r;
long long ans;
vector<int> a[N];
namespace LinkCutTree {
inline bool whichSide(int x) { return lc[fa[x]] == x; }
inline bool isRoot(int x) { return lc[fa[x]] != x && rc[fa[x]] != x; }
inline void addRev(int x) {
  if (!x) return;
  rev[x] ^= 1;
  std::swap(lc[x], rc[x]);
}
inline void pushDown(int x) {
  if (rev[x]) {
    addRev(lc[x]);
    addRev(rc[x]);
    rev[x] = 0;
  }
}
inline void Rotate(int x) {
  int y = fa[x], z = fa[y], b = lc[y] == x ? rc[x] : lc[x];
  if (!isRoot(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
  fa[y] = x, fa[x] = z;
  b ? fa[b] = y : 0;
  lc[y] == x ? (rc[x] = y, lc[y] = b) : (lc[x] = y, rc[y] = b);
}
inline void Splay(int x) {
  que[qr = 1] = x;
  for (int y = x; !isRoot(y); y = fa[y]) que[++qr] = fa[y];
  for (int i = qr; i >= 1; --i) pushDown(que[i]);
  while (!isRoot(x)) {
    if (!isRoot(fa[x])) Rotate(whichSide(fa[x]) == whichSide(x) ? fa[x] : x);
    Rotate(x);
  }
}
inline void Access(int x) {
  for (int y = 0; x; y = x, x = fa[x]) Splay(x), rc[x] = y;
}
inline int findRoot(int x) {
  Access(x);
  Splay(x);
  int w = x;
  while (pushDown(w), lc[w]) w = lc[w];
  Splay(w);
  return w;
}
inline void makeRoot(int x) {
  Access(x);
  Splay(x);
  addRev(x);
}
inline void Link(int x, int y) {
  makeRoot(x);
  fa[x] = y;
}
inline void Cut(int x, int y) {
  makeRoot(x);
  Access(y);
  Splay(y);
  lc[y] = fa[x] = 0;
}
}  // namespace LinkCutTree
template <class T>
inline T Min(T x, T y) {
  return x < y ? x : y;
}
struct node {
  int mx, cnt;
  node() {}
  node(int Mx, int Cnt) : mx(Mx), cnt(Cnt) {}
  friend inline node operator+(const node &a, const node &b) {
    node c;
    c.mx = Min(a.mx, b.mx);
    c.cnt = 0;
    c.mx == a.mx ? c.cnt += a.cnt : 0;
    c.mx == b.mx ? c.cnt += b.cnt : 0;
    return c;
  }
} tr[M];
namespace SegmentTree {
inline void Update(int s) { tr[s] = tr[s << 1] + tr[s << 1 | 1]; }
inline void addTag(int s, int v) {
  tag[s] += v;
  tr[s].mx += v;
}
inline void pushDown(int s) {
  if (tag[s]) {
    addTag(s << 1, tag[s]);
    addTag(s << 1 | 1, tag[s]);
    tag[s] = 0;
  }
}
inline void Build(int s, int l, int r) {
  if (l == r) return (void)(tr[s] = node(val[l], 1));
  int mid = l + r >> 1;
  Build(s << 1, l, mid);
  Build(s << 1 | 1, mid + 1, r);
  Update(s);
}
inline void Modify(int s, int l, int r, int x, int v) {
  if (l == r) return addTag(s, v);
  pushDown(s);
  int mid = l + r >> 1;
  x <= mid ? (addTag(s << 1 | 1, v), Modify(s << 1, l, mid, x, v))
           : Modify(s << 1 | 1, mid + 1, r, x, v);
  Update(s);
}
inline node Query(int s, int l, int r, int x, int y) {
  if (l == x && r == y) return tr[s];
  pushDown(s);
  int mid = l + r >> 1;
  if (y <= mid)
    return Query(s << 1, l, mid, x, y);
  else if (x > mid)
    return Query(s << 1 | 1, mid + 1, r, x, y);
  else
    return Query(s << 1, l, mid, x, mid) +
           Query(s << 1 | 1, mid + 1, r, mid + 1, y);
}
}  // namespace SegmentTree
inline bool check() {
  int x = px[r + 1], y = py[r + 1], cnt = 0;
  set<int> s;
  s.clear();
  for (int k = 0; k < 4; ++k) {
    int tx = x + dx[k], ty = y + dy[k];
    if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty] >= l &&
        a[tx][ty] <= r)
      s.insert(LinkCutTree::findRoot(a[tx][ty])), ++cnt;
  }
  return s.size() == cnt;
}
inline void Insert() {
  ++r;
  int x = px[r], y = py[r];
  for (int k = 0; k < 4; ++k) {
    int tx = x + dx[k], ty = y + dy[k];
    if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty] >= l &&
        a[tx][ty] <= r)
      LinkCutTree::Link(r, a[tx][ty]);
  }
}
inline void Delete() {
  int x = px[l], y = py[l];
  for (int k = 0; k < 4; ++k) {
    int tx = x + dx[k], ty = y + dy[k];
    if (tx >= 1 && tx <= n && ty >= 1 && ty <= m) {
      if (a[tx][ty] >= l && a[tx][ty] <= r) LinkCutTree::Cut(l, a[tx][ty]);
      if (a[tx][ty] >= l) SegmentTree::Modify(1, 1, tot, a[tx][ty], 1);
    }
  }
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; ++i) a[i].resize(m + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      read(a[i][j]);
      px[a[i][j]] = i;
      py[a[i][j]] = j;
    }
  tot = n * m;
  for (int i = 1; i <= tot; ++i) {
    int x = px[i], y = py[i];
    for (int k = 0; k < 4; ++k) {
      int tx = x + dx[k], ty = y + dy[k];
      if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty] < i) ++now_e;
    }
    val[i] = i - now_e;
  }
  SegmentTree::Build(1, 1, tot);
  r = 1;
  for (l = 1; l <= tot; ++l) {
    while (r < tot && check()) Insert();
    ans += SegmentTree::Query(1, 1, tot, l, r).cnt;
    SegmentTree::Modify(1, 1, tot, l, -1);
    Delete();
  }
  std::cout << ans << std::endl;
  return 0;
}