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 = 1010, M = 200010;
const int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int n, m, lb = 1, rb = 1;
int a[N][N], idx[M], idy[M];
int fa[M], ch[2][M], sta[M], top;
bool rv[M];
int tr[M * 4], sum[M * 4], lz[M * 4], fst, snd;
long long ans = 1;
inline int Min(const int &x, const int &y) { return x < y ? x : y; }
inline bool nroot(int u) { return ch[0][fa[u]] == u || ch[1][fa[u]] == u; }
inline void Rev(int u) { rv[u] ^= 1, swap(ch[0][u], ch[1][u]); }
inline void pd(int u) {
  if (rv[u]) Rev(ch[0][u]), Rev(ch[1][u]), rv[u] = 0;
}
void rotate(int u) {
  int y = fa[u], z = fa[y], k = ch[1][y] == u, w = ch[k ^ 1][u];
  if (nroot(y)) ch[ch[1][z] == y][z] = u;
  ch[k][y] = w, ch[k ^ 1][u] = y;
  if (w) fa[w] = y;
  fa[u] = z, fa[y] = u;
}
void splay(int u) {
  int y = u, z;
  for (sta[top = 1] = y; nroot(y); sta[++top] = y = fa[y])
    ;
  for (; top; pd(sta[top--]))
    ;
  for (; nroot(u); rotate(u)) {
    y = fa[u], z = fa[y];
    if (nroot(y)) rotate((ch[0][y] == u) ^ (ch[0][z] == y) ? u : y);
  }
}
inline void access(int u) {
  for (int y = 0; u; u = fa[y = u]) splay(u), ch[1][u] = y;
}
inline void makeroot(int u) { access(u), splay(u), Rev(u); }
inline void split(int x, int y) { makeroot(x), access(y), splay(y); }
inline int findroot(int u) {
  access(u), splay(u);
  for (; ch[0][u]; pd(u), u = ch[0][u])
    ;
  splay(u);
  return u;
}
inline void lnk(int x, int y) { makeroot(x), fa[x] = y; }
inline void cut(int x, int y) { split(x, y), fa[x] = ch[0][y] = 0; }
void pushup(int u) {
  sum[u] = 0, tr[u] = Min(tr[u * 2], tr[u * 2 + 1]);
  if (tr[u] == tr[u * 2]) sum[u] += sum[u * 2];
  if (tr[u] == tr[u * 2 + 1]) sum[u] += sum[u * 2 + 1];
}
void pushdown(int u) {
  if (!lz[u]) return;
  lz[u * 2] += lz[u], lz[u * 2 + 1] += lz[u];
  tr[u * 2] += lz[u], tr[u * 2 + 1] += lz[u];
  lz[u] = 0;
}
void build(int u, int l, int r) {
  tr[u] = 0, sum[u] = r - l + 1;
  if (l >= r) return;
  int mid = (l + r) >> 1;
  build(u * 2, l, mid), build(u * 2 + 1, mid + 1, r);
}
void mdy(int u, int l, int r, int L, int R, int w) {
  if (L <= l && r <= R) {
    tr[u] += w, lz[u] += w;
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(u);
  if (L <= mid) mdy(u * 2, l, mid, L, R, w);
  if (R > mid) mdy(u * 2 + 1, mid + 1, r, L, R, w);
  pushup(u);
}
void ask(int u, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    if (fst > tr[u]) fst = tr[u], snd = 0;
    if (fst == tr[u]) snd += sum[u];
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(u);
  if (L <= mid) ask(u * 2, l, mid, L, R);
  if (R > mid) ask(u * 2 + 1, mid + 1, r, L, R);
}
bool check() {
  static int tmp[5];
  int len = 0;
  for (int i = 0; i < 4; i++) {
    int x = idx[rb + 1] + dir[i][0], y = idy[rb + 1] + dir[i][1];
    if (!x || !y || x > n || y > m || a[x][y] < lb || a[x][y] > rb) continue;
    tmp[++len] = findroot(a[x][y]);
  }
  sort(tmp + 1, tmp + len + 1);
  for (int i = 1; i < len; i++)
    if (tmp[i] == tmp[i + 1]) return 0;
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &a[i][j]), idx[a[i][j]] = i, idy[a[i][j]] = j;
  build(1, 1, n * m), mdy(1, 1, n * m, 1, 1, 1);
  for (; rb < n * m;) {
    for (; lb < rb && !check(); ++lb)
      for (int i = 0; i < 4; i++) {
        int x = idx[lb] + dir[i][0], y = idy[lb] + dir[i][1];
        if (!x || !y || x > n || y > m || a[x][y] < lb || a[x][y] > rb)
          continue;
        cut(lb, a[x][y]);
      }
    ++rb, mdy(1, 1, n * m, lb, rb, 1);
    for (int i = 0; i < 4; i++) {
      int x = idx[rb] + dir[i][0], y = idy[rb] + dir[i][1];
      if (!x || !y || x > n || y > m || a[x][y] < lb || a[x][y] > rb) continue;
      lnk(rb, a[x][y]), mdy(1, 1, n * m, lb, a[x][y], -1);
    }
    fst = M, snd = 0, ask(1, 1, n * m, lb, rb);
    if (fst == 1) ans += snd;
  }
  printf("%I64d\n", ans);
  return 0;
}