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;
int get() {
  char ch;
  while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
    ;
  if (ch == '-') {
    int s = 0;
    while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
    return -s;
  }
  int s = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
  return s;
}
const int N = 1e3 + 5;
const int MAXN = 2e5 + 5;
int n, m;
int a[N][N];
int lef[MAXN];
vector<int> to[MAXN];
struct node {
  int ad, mi, tot;
} tree[MAXN * 2];
void build(int now, int l, int r) {
  tree[now].mi = 0;
  tree[now].tot = r - l + 1;
  if (l == r) return;
  int mid = (l + r) / 2;
  int ls = now + 1, rs = now + (mid - l + 1) * 2;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}
void down(int now, int ls, int rs) {
  tree[ls].mi += tree[now].ad;
  tree[ls].ad += tree[now].ad;
  tree[rs].mi += tree[now].ad;
  tree[rs].ad += tree[now].ad;
  tree[now].ad = 0;
}
void update(int now, int ls, int rs) {
  tree[now].mi = tree[ls].mi, tree[now].tot = tree[ls].tot;
  if (tree[rs].mi < tree[now].mi) tree[now].mi = tree[rs].mi, tree[now].tot = 0;
  if (tree[rs].mi == tree[now].mi) tree[now].tot += tree[rs].tot;
}
void change(int now, int l, int r, int x, int y, int v) {
  if (x <= l && r <= y) {
    tree[now].mi += v;
    tree[now].ad += v;
    return;
  }
  int mid = (l + r) / 2;
  int ls = now + 1, rs = now + (mid - l + 1) * 2;
  down(now, ls, rs);
  if (x <= mid) change(ls, l, mid, x, y, v);
  if (y > mid) change(rs, mid + 1, r, x, y, v);
  update(now, ls, rs);
}
int getans(int now, int l, int r, int x, int y) {
  if (x <= l && r <= y) return tree[now].mi == 1 ? tree[now].tot : 0;
  int mid = (l + r) / 2;
  int ls = now + 1, rs = now + (mid - l + 1) * 2;
  down(now, ls, rs);
  int ret = 0;
  if (x <= mid) ret = getans(ls, l, mid, x, y);
  if (y > mid) ret += getans(rs, mid + 1, r, x, y);
  return ret;
}
struct point {
  int s[2];
  int val, mi;
  bool bz;
} pt[MAXN];
int fa[MAXN], u;
int L_most;
void update(int x) {
  pt[x].mi = min(pt[pt[x].s[0]].mi, pt[pt[x].s[1]].mi);
  pt[x].mi = min(pt[x].mi, pt[x].val);
}
void down(int x) {
  if (pt[x].bz) {
    swap(pt[x].s[0], pt[x].s[1]);
    pt[pt[x].s[0]].bz ^= 1;
    pt[pt[x].s[1]].bz ^= 1;
    pt[x].bz = 0;
  }
}
int pd(int x) {
  if (x == pt[fa[x]].s[0]) return 0;
  if (x == pt[fa[x]].s[1]) return 1;
  return -1;
}
void rotate(int x) {
  int y = fa[x], z = fa[y];
  int tx = pd(x), ty = pd(y);
  if (ty != -1) pt[z].s[ty] = x;
  fa[x] = z;
  if (pt[x].s[tx ^ 1]) fa[pt[x].s[tx ^ 1]] = y;
  pt[y].s[tx] = pt[x].s[tx ^ 1];
  pt[x].s[tx ^ 1] = y;
  fa[y] = x;
  update(y);
  update(x);
  if (ty != -1) update(z);
}
int key[MAXN];
void clear(int x) {
  int k = 0;
  for (; pd(x) != -1; x = fa[x]) key[++k] = x;
  key[++k] = x;
  for (int i = k; i >= 1; i--) down(key[i]);
}
void splay(int x) {
  clear(x);
  for (; pd(x) != -1;) {
    if (pd(fa[x]) != -1) {
      if (pd(x) == pd(fa[x]))
        rotate(fa[x]);
      else
        rotate(x);
    }
    rotate(x);
  }
}
void access(int x) {
  splay(x);
  pt[x].s[1] = 0;
  update(x);
  for (; fa[x];) {
    int y = fa[x];
    splay(y);
    pt[y].s[1] = x;
    update(y);
    splay(x);
  }
}
void makeroot(int x) {
  access(x);
  splay(x);
  pt[x].bz ^= 1;
}
void add(int x, int y) {
  makeroot(x);
  makeroot(y);
  if (!fa[x]) {
    fa[x] = y;
    return;
  }
  int z = pt[y].mi;
  if (z == x) {
    L_most = max(L_most, x + 1);
    return;
  }
  L_most = max(L_most, z + 1);
  splay(z);
  int p = pt[z].s[0];
  fa[p] = 0;
  pt[z].s[0] = 0;
  update(z);
  makeroot(x);
  makeroot(y);
  fa[x] = y;
}
int main() {
  n = get();
  m = get();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) a[i][j] = get();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m - 1; j++) {
      int l = min(a[i][j], a[i][j + 1]), r = max(a[i][j], a[i][j + 1]);
      to[r].push_back(l);
    }
  for (int i = 1; i <= n - 1; i++)
    for (int j = 1; j <= m; j++) {
      int l = min(a[i][j], a[i + 1][j]), r = max(a[i][j], a[i + 1][j]);
      to[r].push_back(l);
    }
  int k = n * m;
  build(1, 1, k);
  long long ans = 0;
  L_most = 1;
  u = k;
  for (int i = 1; i <= k; i++) pt[i].mi = pt[i].val = i;
  pt[0].mi = pt[0].val = 1e9;
  for (int i = 1; i <= k; i++) {
    change(1, 1, k, 1, i, 1);
    for (auto c : to[i]) {
      change(1, 1, k, 1, c, -1);
      add(c, i);
    }
    ans = ans + getans(1, 1, k, L_most, i);
  }
  cout << ans << endl;
  return 0;
}