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 __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;
template <typename T>
inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = (iS == iT
                            ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),
                               (iS == iT ? EOF : *iS++))
                            : *iS++)))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48),
    ch = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),
                      (iS == iT ? EOF : *iS++))
                   : *iS++);
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline long long read_ll() {
  long long x;
  return read(x), x;
}
template <typename T>
inline void chkmin(T &a, T b) {
  a = a < b ? a : b;
}
const int MAXN = 200010;
int n, m, N, L, R;
int a[2020][2020];
int vis[2020][2020];
int E[2020][2020][4];
int px[MAXN];
int py[MAXN];
struct SegmentTree {
  struct Node {
    int t, m, c;
  } T[MAXN << 2];
  inline void pushup(int x) {
    T[x].m = min(T[(x << 1)].m, T[(x << 1 | 1)].m);
    T[x].c = (T[(x << 1)].m == T[x].m ? T[(x << 1)].c : 0) +
             (T[(x << 1 | 1)].m == T[x].m ? T[(x << 1 | 1)].c : 0);
  }
  inline void pushdown(int x) {
    int t = T[x].t;
    if (!t) return;
    T[(x << 1)].t += t, T[(x << 1)].m += t;
    T[(x << 1 | 1)].t += t, T[(x << 1 | 1)].m += t;
    T[x].t = 0;
  }
  inline void build(int x, int l, int r) {
    if (l == r) return T[x].c = 1, T[x].m = 1e9, void();
    int mid = (l + r) >> 1;
    build((x << 1), l, mid), build((x << 1 | 1), mid + 1, r), pushup(x);
  }
  inline void Modify(int x, int l, int r, int p, int v) {
    if (l == r) return T[x].m = v, void();
    int mid = (l + r) >> 1;
    pushdown(x);
    p <= mid ? Modify((x << 1), l, mid, p, v)
             : Modify((x << 1 | 1), mid + 1, r, p, v);
    pushup(x);
  }
  inline void Add(int x, int l, int r, int L, int R, int v) {
    if (L <= l && r <= R) return T[x].m += v, T[x].t += v, void();
    int mid = (l + r) >> 1;
    pushdown(x);
    if (L <= mid) Add((x << 1), l, mid, L, R, v);
    if (mid < R) Add((x << 1 | 1), mid + 1, r, L, R, v);
    pushup(x);
  }
  inline int Sum() { return T[1].m == 1 ? T[1].c : 0; }
} seg;
struct LinkCutTree {
  struct Node {
    int fa, son[2], rev;
  } T[MAXN];
  inline int isroot(int x) {
    return T[T[x].fa].son[0] != x && T[T[x].fa].son[1] != x;
  }
  inline void reverse(int x) { T[x].rev ^= 1, swap(T[x].son[0], T[x].son[1]); }
  inline void pushdown(int x) {
    if (T[x].rev) {
      T[x].rev = 0;
      if (T[x].son[0]) reverse(T[x].son[0]);
      if (T[x].son[1]) reverse(T[x].son[1]);
    }
  }
  inline void Push(int x) {
    if (!isroot(x)) Push(T[x].fa);
    pushdown(x);
  }
  inline void rotate(int x) {
    int y = T[x].fa, z = T[y].fa, p = (T[y].son[1] == x),
        q = (T[z].son[1] == y), a = T[x].son[p ^ 1];
    if (!isroot(y)) T[z].son[q] = x;
    T[x].son[p ^ 1] = y, T[y].son[p] = a;
    if (a) T[a].fa = y;
    T[y].fa = x, T[x].fa = z;
  }
  inline void Splay(int x) {
    Push(x);
    while (!isroot(x)) {
      int y = T[x].fa, z = T[y].fa;
      if (!isroot(y)) rotate((T[z].son[1] == y) ^ (T[y].son[1] == x) ? x : y);
      rotate(x);
    }
  }
  inline void access(int x) {
    for (int la = 0; x; T[x].son[1] = la, la = x, x = T[x].fa) Splay(x);
  }
  inline int findroot(int x) {
    access(x), Splay(x);
    while (T[x].son[0]) x = T[x].son[0], pushdown(x);
    return Splay(x), x;
  }
  inline void makeroot(int x) { access(x), Splay(x), reverse(x); }
  inline void Cut(int x, int y) {
    makeroot(x), access(y), Splay(y), T[x].fa = T[y].son[0] = 0;
  }
  inline void Link(int x, int y) {
    makeroot(x), access(y), Splay(y), T[x].fa = y;
  }
  inline int Q(int x, int y) { return makeroot(x), findroot(y) == x; }
} lct;
inline void Del(int x) {
  int X = px[x], Y = py[x];
  if (E[X][Y][0]) lct.Cut(x, a[X + 1][Y]), E[X][Y][0] = E[X + 1][Y][1] = 0;
  if (E[X][Y][1]) lct.Cut(x, a[X - 1][Y]), E[X][Y][1] = E[X - 1][Y][0] = 0;
  if (E[X][Y][2]) lct.Cut(x, a[X][Y + 1]), E[X][Y][2] = E[X][Y + 1][3] = 0;
  if (E[X][Y][3]) lct.Cut(x, a[X][Y - 1]), E[X][Y][3] = E[X][Y - 1][2] = 0;
  vis[X][Y] = 0;
}
inline void solve(int X1, int Y1, int X2, int Y2, int fx) {
  if (!vis[X2][Y2]) return;
  int a1 = a[X1][Y1], a2 = a[X2][Y2];
  while (L < R && lct.Q(a1, a2)) seg.Modify(1, 1, N, L, 1e9), Del(L), ++L;
  if (L <= a2 && a2 <= R) {
    lct.Link(a1, a2), seg.Add(1, 1, N, 1, a2, -1);
    E[X1][Y1][fx] = E[X2][Y2][fx ^ 1] = 1;
  }
}
int main() {
  n = read_int(), m = read_int(), N = n * m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int x = a[i][j] = read_int();
      px[x] = i, py[x] = j;
    }
  long long res = 0;
  seg.build(1, 1, N), L = R = 1;
  while (R <= N) {
    seg.Modify(1, 1, N, R, 0);
    seg.Add(1, 1, N, 1, R, 1);
    int X = px[R], Y = py[R];
    solve(X, Y, X + 1, Y, 0);
    solve(X, Y, X - 1, Y, 1);
    solve(X, Y, X, Y + 1, 2);
    solve(X, Y, X, Y - 1, 3);
    res += seg.Sum();
    vis[X][Y] = 1;
    ++R;
  }
  cout << res << endl;
  return 0;
}