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;
const int N = 2e5 + 5;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int swap(int &x, int &y) {
  int t = x;
  x = y;
  y = t;
}
int n, m, a[2005][2005], ch[N][2], fa[N], sze[N], p[N];
bool notRT(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
int sk[N];
vector<int> S[N];
void tag(int x) {
  swap(ch[x][0], ch[x][1]);
  p[x] ^= 1;
}
void pushdown(int x) {
  if (p[x]) {
    p[x] = 0;
    if (ch[x][0]) tag(ch[x][0]);
    if (ch[x][1]) tag(ch[x][1]);
  }
}
void pushup(int x) { sze[x] = sze[ch[x][0]] + sze[ch[x][1]] + 1; }
void rotate(int x) {
  int y = fa[x], z = fa[y];
  bool k = ch[y][1] == x;
  int w = ch[x][k ^ 1];
  if (notRT(y)) ch[z][ch[z][1] == y] = x;
  ch[x][k ^ 1] = y;
  ch[y][k] = w;
  if (w) fa[w] = y;
  fa[y] = x;
  fa[x] = z;
  pushup(y);
  pushup(x);
}
void splay(int x) {
  int y = x, z = 0;
  sk[++z] = x;
  while (notRT(y)) sk[++z] = y = fa[y];
  while (z) pushdown(sk[z--]);
  while (notRT(x)) {
    y = fa[x];
    z = fa[y];
    if (notRT(y)) rotate(((ch[z][1] == y) ^ (ch[y][1] == x)) ? x : y);
    rotate(x);
  }
}
void access(int x) {
  for (int y = 0; x; x = fa[y = x]) splay(x), ch[x][1] = y, pushup(x);
}
void makeroot(int x) {
  access(x);
  splay(x);
  tag(x);
}
int findroot(int x) {
  access(x);
  splay(x);
  while (ch[x][0]) pushdown(x), x = ch[x][0];
  splay(x);
  return x;
}
bool link(int x, int y) {
  makeroot(x);
  if (findroot(y) != x)
    fa[x] = y;
  else
    return 0;
  return 1;
}
bool cut(int x, int y) {
  makeroot(x);
  if (findroot(y) == x && !ch[y][0] && fa[y] == x) {
    fa[y] = ch[x][1] = 0;
    pushup(x);
    return 1;
  }
  return 0;
}
struct PT {
  int mn, cnt, lzy;
  PT() {}
  PT(int _mn, int _cnt, int _lzy) {
    mn = _mn;
    cnt = _cnt;
    lzy = _lzy;
  }
  PT operator+(const PT &q) const {
    PT res(0, 0, 0);
    if (mn == q.mn) {
      res.mn = mn;
      res.cnt = cnt + q.cnt;
    } else if (mn < q.mn) {
      res.mn = mn;
      res.cnt = cnt;
    } else {
      res.mn = q.mn;
      res.cnt = q.cnt;
    }
    return res;
  }
} t[N << 2];
void change(int i, int k) {
  t[i].lzy += k;
  t[i].mn += k;
}
void pdn(int i) {
  if (t[i].lzy) {
    change((i << 1), t[i].lzy);
    change((i << 1 | 1), t[i].lzy);
    t[i].lzy = 0;
  }
}
void build(int i, int l, int r) {
  t[i].cnt = r - l + 1;
  if (l == r) return;
  build((i << 1), l, ((l + r) >> 1));
  build((i << 1 | 1), ((l + r) >> 1) + 1, r);
}
void updata(int i, int l, int r, int x, int y, int k) {
  if (x <= l && r <= y) {
    change(i, k);
    return;
  }
  pdn(i);
  if (x <= ((l + r) >> 1)) updata((i << 1), l, ((l + r) >> 1), x, y, k);
  if (y > ((l + r) >> 1)) updata((i << 1 | 1), ((l + r) >> 1) + 1, r, x, y, k);
  t[i] = t[(i << 1)] + t[(i << 1 | 1)];
}
PT query(int i, int l, int r, int x, int y) {
  if (x <= l && r <= y) return t[i];
  PT res(998299, 0, 0);
  pdn(i);
  if (x <= ((l + r) >> 1)) res = res + query((i << 1), l, ((l + r) >> 1), x, y);
  if (y > ((l + r) >> 1))
    res = res + query((i << 1 | 1), ((l + r) >> 1) + 1, r, x, y);
  return res;
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) a[i][j] = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < m; ++j)
      S[a[i][j]].push_back(a[i][j + 1]), S[a[i][j + 1]].push_back(a[i][j]);
  for (int i = 1; i < n; ++i)
    for (int j = 1; j <= m; ++j)
      S[a[i][j]].push_back(a[i + 1][j]), S[a[i + 1][j]].push_back(a[i][j]);
  long long ans = 0;
  build(1, 1, n * m);
  for (int L = 1, R = 0; L <= n * m; ++L) {
    while (R < n * m) {
      ++R;
      bool f = 1;
      for (int i = S[R].size() - 1; i >= 0; --i) {
        int v = S[R][i];
        if (v >= L && v <= R) {
          if (!link(v, R)) {
            f = 0;
            break;
          }
          updata(1, 1, n * m, R, n * m, -1);
        }
      }
      if (f == 0) {
        for (int i = S[R].size() - 1; i >= 0; --i) {
          int v = S[R][i];
          if (v >= L && v <= R)
            if (cut(v, R))
              updata(1, 1, n * m, R, n * m, 1);
            else
              break;
        }
        --R;
        break;
      }
      updata(1, 1, n * m, R, n * m, 1);
    }
    PT res = query(1, 1, n * m, L, R);
    if (res.mn == 1) ans += res.cnt;
    updata(1, 1, n * m, L, n * m, -1);
    for (int i = S[L].size() - 1; i >= 0; --i) {
      int v = S[L][i];
      if (v >= L && v <= R)
        if (cut(v, L)) updata(1, 1, n * m, v, n * m, 1);
    }
  }
  printf("%lld", ans);
  return 0;
}