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>
const int M = 200005;
const int N = 2005;
const int INF = 1e9;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0, w = 1;
  while (c > '9' || c < '0') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * w;
}
int n, m, e, val[N][N];
long long ans;
vector<int> eb[M], ec[M];
struct Node {
  int tmin, cnt;
};
inline Node chkmin(Node A, Node B) {
  Node c;
  c.cnt = 0;
  c.tmin = min(A.tmin, B.tmin);
  if (c.tmin == A.tmin) c.cnt += A.cnt;
  if (c.tmin == B.tmin) c.cnt += B.cnt;
  return c;
}
struct Link_Cut_Tree {
  int root, tot;
  int fa[M], son[M][2], rev[M], st[M];
  inline bool Nrt(int now) {
    return (son[fa[now]][0] == now || son[fa[now]][1] == now);
  }
  inline void Flip(int now) {
    swap((son[now][0]), (son[now][1]));
    rev[now] ^= 1;
  }
  inline void pushdown(int now) {
    if (!rev[now]) return;
    if ((son[now][0])) Flip((son[now][0]));
    if ((son[now][1])) Flip((son[now][1]));
    rev[now] = 0;
  }
  inline void rotate(int now) {
    int father = fa[now], fafa = fa[father], k = (son[father][1] == now),
        w = son[now][k ^ 1];
    if (Nrt(father)) son[fafa][son[fafa][1] == father] = now;
    son[now][k ^ 1] = father;
    son[father][k] = w;
    if (w) fa[w] = father;
    fa[father] = now;
    fa[now] = fafa;
  }
  inline void splay(int now) {
    int top = 0, father = now, fafa;
    st[++top] = father;
    while (Nrt(father)) father = fa[father], st[++top] = father;
    while (top) pushdown(st[top--]);
    while (Nrt(now)) {
      father = fa[now], fafa = fa[father];
      if (Nrt(father))
        rotate(((son[father][0] == now) ^ (son[fafa][0] == father)) ? now
                                                                    : father);
      rotate(now);
    }
  }
  inline void access(int now) {
    for (int ch = 0; now; ch = now, now = fa[now])
      splay(now), (son[now][1]) = ch;
  }
  inline void makeroot(int now) {
    access(now);
    splay(now);
    Flip(now);
  }
  inline int findroot(int now) {
    access(now);
    splay(now);
    while ((son[now][0])) pushdown(now), now = (son[now][0]);
    return now;
  }
  inline void Split(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
  }
  inline void Link(int x, int y) {
    makeroot(x);
    if (findroot(y) != x) fa[x] = y;
  }
  inline void Cut(int x, int y) {
    makeroot(x);
    if (findroot(y) == x && !son[x][1] && fa[x] == y) fa[x] = son[y][0] = 0;
  }
} lct;
struct Segment_Tree {
  int tmin[M << 2], cnt[M << 2], Tag[M << 2];
  inline void pushup(int now) {
    cnt[now] = 0;
    tmin[now] = min(tmin[(now << 1)], tmin[(now << 1 | 1)]);
    if (tmin[(now << 1)] == tmin[now]) cnt[now] += cnt[(now << 1)];
    if (tmin[(now << 1 | 1)] == tmin[now]) cnt[now] += cnt[(now << 1 | 1)];
  }
  void build(int l, int r, int now) {
    tmin[now] = 0;
    cnt[now] = r - l + 1;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, (now << 1));
    build(mid + 1, r, (now << 1 | 1));
  }
  inline void pushdown(int now) {
    if (!Tag[now]) return;
    Tag[(now << 1)] += Tag[now];
    Tag[(now << 1 | 1)] += Tag[now];
    tmin[(now << 1)] += Tag[now];
    tmin[(now << 1 | 1)] += Tag[now];
    Tag[now] = 0;
  }
  void Change(int L, int R, int k, int l, int r, int now) {
    if (l >= L && r <= R) {
      tmin[now] += k;
      Tag[now] += k;
      return;
    }
    pushdown(now);
    int mid = (l + r) >> 1;
    if (mid >= R)
      Change(L, R, k, l, mid, (now << 1));
    else if (mid < L)
      Change(L, R, k, mid + 1, r, (now << 1 | 1));
    else
      Change(L, mid, k, l, mid, (now << 1)),
          Change(mid + 1, R, k, mid + 1, r, (now << 1 | 1));
    pushup(now);
  }
  Node query(int L, int R, int l, int r, int now) {
    if (l > R || r < L) return ((Node){INF, 0});
    if (l >= L && r <= R) return ((Node){tmin[now], cnt[now]});
    pushdown(now);
    int mid = (l + r) >> 1;
    if (mid >= R)
      return query(L, R, l, mid, (now << 1));
    else if (mid < L)
      return query(L, R, mid + 1, r, (now << 1 | 1));
    else
      return chkmin(query(L, mid, l, mid, (now << 1)),
                    query(mid + 1, R, mid + 1, r, (now << 1 | 1)));
  }
} seg;
int main() {
  n = read();
  m = read();
  e = n * m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) val[i][j] = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1, x, y; j <= m; j++)
      for (int k = 0; k < 4; k++) {
        x = i + dx[k], y = j + dy[k];
        if (x < 1 || y < 1 || x > n || y > m) continue;
        if (val[x][y] > val[i][j])
          eb[val[i][j]].push_back(val[x][y]);
        else
          ec[val[i][j]].push_back(val[x][y]);
      }
  seg.build(1, e, 1);
  int r = 0;
  vector<int> vec;
  for (int l = 1; l <= e; l++) {
    bool iscir = false;
    for (int ri = r + 1; ri <= e; ri++) {
      vec.clear();
      for (int i = 0, v; i < ec[ri].size(); i++) {
        v = ec[ri][i];
        if (v < l) continue;
        if (lct.findroot(ri) == lct.findroot(v)) {
          iscir = true;
          break;
        }
        lct.Link(ri, v);
        vec.push_back(v);
      }
      for (int i = 0; i < vec.size(); i++) lct.Cut(ri, vec[i]);
      if (iscir) break;
      ++r;
      int tot = 0;
      for (int i = 0, v; i < ec[ri].size(); i++) {
        v = ec[ri][i];
        if (v < l) continue;
        lct.Link(ri, v);
        ++tot;
      }
      seg.Change(ri, e, -tot, 1, e, 1);
      seg.Change(ri, ri, r - l + 1, 1, e, 1);
    }
    Node temp = seg.query(l, r, 1, e, 1);
    if (temp.tmin == 1) ans += temp.cnt;
    for (int i = 0, v; i < eb[l].size(); i++) {
      v = eb[l][i];
      if (v > r) continue;
      lct.Cut(l, v);
      seg.Change(v, e, 1, 1, e, 1);
    }
    seg.Change(l, r, -1, 1, e, 1);
  }
  printf("%lld\n", ans);
  return 0;
}