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;
struct LCT {
  int fa[200005], ch[200005][2], tag[200005], st[200005];
  bool un_root(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
  bool get(int x) { return ch[fa[x]][1] == x; }
  void push_rev(int x) {
    swap(ch[x][0], ch[x][1]);
    tag[x] ^= 1;
    return;
  }
  void push_down(int x) {
    if (!tag[x]) return;
    if (ch[x][0]) push_rev(ch[x][0]);
    if (ch[x][1]) push_rev(ch[x][1]);
    tag[x] ^= 1;
    return;
  }
  void rotate(int x) {
    int y = fa[x], z = fa[y], tx = get(x), ty = get(y), p = ch[x][tx ^ 1];
    if (un_root(y)) ch[z][ty] = x;
    fa[x] = z;
    if (p) fa[p] = y;
    ch[y][tx] = p;
    fa[y] = x;
    ch[x][tx ^ 1] = y;
    return;
  }
  void splay(int x) {
    int top = 0, u = x;
    st[++top] = u;
    while (un_root(u)) st[++top] = u = fa[u];
    while (top) push_down(st[top--]);
    while (un_root(x)) {
      int y = fa[x], tx = get(x), ty = get(y);
      if (un_root(y)) rotate(tx == ty ? y : x);
      rotate(x);
    }
    return;
  }
  void access(int x) {
    for (int y = 0; x; x = fa[y = x]) splay(x), ch[x][1] = y;
    return;
  }
  int find_root(int x) {
    access(x);
    splay(x);
    while (ch[x][0]) x = ch[x][0];
    return splay(x), x;
  }
  void make_root(int x) {
    access(x);
    splay(x);
    push_rev(x);
    return;
  }
  bool link(int x, int y) {
    make_root(x);
    if (find_root(y) == x) return false;
    fa[x] = y;
    return true;
  }
  void cut(int x, int y) {
    if (!x || !y) return;
    make_root(x);
    if (find_root(y) == x && fa[y] == x && !ch[y][0]) ch[x][1] = fa[y] = 0;
    return;
  }
} lct;
struct Segment_Tree {
  int mn[200005 << 3], val[200005 << 3], tag[200005 << 3];
  void push_up(int pos) {
    if (mn[pos << 1] < mn[pos << 1 | 1])
      val[pos] = val[pos << 1], mn[pos] = mn[pos << 1];
    if (mn[pos << 1 | 1] < mn[pos << 1])
      val[pos] = val[pos << 1 | 1], mn[pos] = mn[pos << 1 | 1];
    if (mn[pos << 1 | 1] == mn[pos << 1])
      val[pos] = val[pos << 1] + val[pos << 1 | 1], mn[pos] = mn[pos << 1];
    return;
  }
  void push_down(int pos) {
    if (!tag[pos]) return;
    int w = tag[pos];
    tag[pos] = 0;
    mn[pos << 1] += w;
    mn[pos << 1 | 1] += w;
    tag[pos << 1] += w;
    tag[pos << 1 | 1] += w;
    return;
  }
  void build(int pos, int l, int r) {
    if (l == r) return mn[pos] = l, val[pos] = 1, void();
    int mid = (l + r) >> 1;
    build(pos << 1, l, mid);
    build(pos << 1 | 1, mid + 1, r);
    push_up(pos);
    return;
  }
  void update(int pos, int l, int r, int nl, int nr, int w) {
    if (l >= nl && r <= nr) return tag[pos] += w, mn[pos] += w, void();
    int mid = (l + r) >> 1;
    push_down(pos);
    if (mid >= nl) update(pos << 1, l, mid, nl, nr, w);
    if (mid < nr) update(pos << 1 | 1, mid + 1, r, nl, nr, w);
    push_up(pos);
    return;
  }
  int query(int pos, int l, int r, int nl, int nr) {
    if (l >= nl && r <= nr) return val[pos] * (mn[pos] == 0);
    int mid = (l + r) >> 1, sum = 0;
    push_down(pos);
    if (mid >= nl) sum += query(pos << 1, l, mid, nl, nr);
    if (mid < nr) sum += query(pos << 1 | 1, mid + 1, r, nl, nr);
    return sum;
  }
} T;
int n, m;
int a[1003][1003], st[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool vis[1003][1003];
pair<int, int> p[200005];
long long ans;
int main() {
  scanf("%d%d", &n, &m);
  int M = n * m;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      scanf("%d", a[i] + j), p[a[i][j]] = make_pair(i, j);
  vis[p[1].first][p[1].second] = true;
  T.build(1, 1, M);
  for (int l = 1, r = 1; l <= M; ++l) {
    T.update(1, 1, M, l, M, -1);
    while (r < M) {
      ++r;
      bool flag = true;
      int x = p[r].first, y = p[r].second, cnt = 0;
      for (int i = 0; i < 4; ++i) {
        int xx = x + st[i][0], yy = y + st[i][1];
        if (!vis[xx][yy] || xx < 1 || xx > n || yy < 1 || yy > m) continue;
        int tmp = a[xx][yy];
        flag &= lct.link(r, tmp);
        if (!flag) {
          for (int j = 0; j < i; ++j) lct.cut(r, a[x + st[j][0]][y + st[j][1]]);
          break;
        }
        ++cnt;
      }
      if (!flag) {
        --r;
        break;
      }
      T.update(1, 1, M, r, M, -cnt);
      vis[x][y] = true;
    }
    ans += T.query(1, 1, M, l, r);
    int x = p[l].first, y = p[l].second;
    for (int i = 0; i < 4; ++i) {
      int xx = x + st[i][0], yy = y + st[i][1];
      if (!vis[xx][yy] || xx < 1 || xx > n || yy < 1 || yy > m) continue;
      int tmp = a[xx][yy];
      lct.cut(l, tmp);
      T.update(1, 1, M, tmp, M, 1);
    }
    vis[x][y] = false;
  }
  cout << ans << endl;
  return 0;
}