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 = 200000 + 7, L = 2000 + 7,
          Fx[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, m, t, A[L][L];
vector<int> E[N], tem;
struct LCT {
  int Sign[N], Fa[N], Son[N][2];
  inline bool Where(int x) { return Son[Fa[x]][1] == x; }
  inline bool noroot(int x) { return Son[Fa[x]][0] == x || Son[Fa[x]][1] == x; }
  inline void MakeSign(int x) {
    if (x) swap(Son[x][0], Son[x][1]), Sign[x] ^= 1;
  }
  inline void Pushdown(int x) {
    if (Sign[x]) MakeSign(Son[x][0]), MakeSign(Son[x][1]), Sign[x] ^= 1;
  }
  void PPushdown(int x) {
    if (noroot(x)) PPushdown(Fa[x]);
    Pushdown(x);
  }
  inline void Rotate(int x) {
    int y = Fa[x], z = Fa[y], ySon = Where(x), zSon = Where(y),
        B = Son[x][ySon ^ 1];
    if (noroot(y)) Son[z][zSon] = x;
    Fa[x] = z;
    if (B) Fa[B] = y;
    Son[y][ySon] = B;
    Fa[y] = x, Son[x][ySon ^ 1] = y;
  }
  inline void Splay(int x) {
    PPushdown(x);
    while (noroot(x)) {
      int fa = Fa[x];
      if (noroot(fa))
        if (Where(fa) ^ Where(x))
          Rotate(x);
        else
          Rotate(fa);
      Rotate(x);
    }
  }
  inline void Access(int x) {
    for (int y = 0; x; y = x, x = Fa[x]) Splay(x), Son[x][1] = y;
  }
  inline void MakeRoot(int x) { Access(x), Splay(x), MakeSign(x); }
  inline int FindRoot(int x) {
    Access(x), Splay(x);
    while (Son[x][0]) Pushdown(x), x = Son[x][0];
    return x;
  }
  inline bool Link(int x, int y) {
    MakeRoot(x);
    if (FindRoot(y) == x) return 0;
    return Fa[x] = y, 1;
  }
  inline void Cut(int x, int y) {
    MakeRoot(x);
    if (FindRoot(y) == x && x == Son[y][0] && !Son[x][1]) Fa[x] = Son[y][0] = 0;
  }
} lct;
int Sign[N << 2];
struct NFO {
  int Min, tot;
  friend NFO operator+(NFO A, NFO B) {
    NFO C;
    C.tot = 0, C.Min = min(A.Min, B.Min);
    if (C.Min == A.Min) C.tot += A.tot;
    if (C.Min == B.Min) C.tot += B.tot;
    return C;
  }
} T[N << 2];
void Build(int u, int l, int r) {
  T[u].Min = 0, T[u].tot = r - l + 1;
  if (l == r) return;
  int Mid = (l + r) >> 1;
  Build((u << 1), l, Mid), Build((u << 1 | 1), Mid + 1, r);
}
inline void Pushdown(int u) {
  if (Sign[u]) {
    Sign[(u << 1)] += Sign[u], T[(u << 1)].Min += Sign[u];
    Sign[(u << 1 | 1)] += Sign[u], T[(u << 1 | 1)].Min += Sign[u];
    Sign[u] = 0;
  }
}
void Update(int u, int l, int r, int L, int R, int w) {
  if (L <= l && R >= r) {
    Sign[u] += w, T[u].Min += w;
    return;
  }
  Pushdown(u);
  int Mid = (l + r) >> 1;
  if (Mid >= L) Update((u << 1), l, Mid, L, R, w);
  if (Mid < R) Update((u << 1 | 1), Mid + 1, r, L, R, w);
  T[u] = T[(u << 1)] + T[(u << 1 | 1)];
}
NFO Query(int u, int l, int r, int L, int R) {
  if (L <= l && R >= r) return T[u];
  Pushdown(u);
  int Mid = (l + r) >> 1;
  if (Mid >= R)
    return Query((u << 1), l, Mid, L, R);
  else if (Mid < L)
    return Query((u << 1 | 1), Mid + 1, r, L, R);
  return Query((u << 1), l, Mid, L, R) + Query((u << 1 | 1), Mid + 1, r, L, R);
}
int main() {
  scanf("%d%d", &n, &m), t = n * m;
  for (int i = (1), iLimit = (n); i <= iLimit; (++(i)))
    for (int j = (1), jLimit = (m); j <= jLimit; (++(j))) scanf("%d", A[i] + j);
  for (int i = (1), iLimit = (n); i <= iLimit; (++(i)))
    for (int j = (1), jLimit = (m); j <= jLimit; (++(j)))
      for (int k = (0), kLimit = (3); k <= kLimit; (++(k))) {
        int nx = i + Fx[k][0], ny = j + Fx[k][1];
        if (nx < 1 || ny < 1 || nx > n || ny > m) continue;
        E[A[i][j]].push_back(A[nx][ny]);
      }
  int r = 0, tot;
  bool flag;
  long long Ans = 0;
  Build(1, 1, t);
  for (int l = (1), lLimit = (t); l <= lLimit; (++(l))) {
    for (int R = (r + 1), RLimit = (t); R <= RLimit; (++(R))) {
      flag = 0, tem.clear();
      for (auto v : E[R])
        if (v < R && v >= l) {
          if (!lct.Link(v, R)) {
            flag = 1;
            break;
          }
          tem.push_back(v);
        }
      for (auto v : tem) lct.Cut(v, R);
      if (flag) break;
      r = R, tot = 0;
      for (auto v : E[R])
        if (v < R && v >= l) lct.Link(v, R), (++(tot));
      Update(1, 1, t, r, t, -tot);
      Update(1, 1, t, r, r, r - l + 1);
    }
    NFO tem = Query(1, 1, t, l, r);
    tot = 0;
    if (tem.Min == 1) Ans += tem.tot;
    for (auto v : E[l])
      if (v > l && v <= r) {
        lct.Cut(v, l);
        Update(1, 1, t, v, t, 1);
      }
    Update(1, 1, t, l, r, -1);
  }
  printf("%lld", Ans);
  return 0;
}