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 MAXN = 2e5 + 5, MAXM = 1e3 + 5, tx[] = {1, -1, 0, 0},
          ty[] = {0, 0, 1, -1};
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45) ny = -1;
  if (nc < 0) return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int n, m, tot, l, r, a[MAXM][MAXM], id[MAXM][MAXM];
pair<int, int> pos[MAXN];
namespace Lct {
int fa[MAXN], s[2][MAXN], lazy[MAXN], stk[MAXN];
inline bool nroot(int x) { return s[0][fa[x]] == x || s[1][fa[x]] == x; }
inline void pushr(int x) { lazy[x] ^= 1, swap(s[0][x], s[1][x]); }
inline void pushdown(int x) {
  if (lazy[x]) pushr(s[0][x]), pushr(s[1][x]), lazy[x] = 0;
}
inline void Pushdown(int x) {
  int y = x, z = 0;
  stk[++z] = y;
  for (; nroot(y); stk[++z] = y = fa[y])
    ;
  for (; z; pushdown(stk[z--]))
    ;
}
inline void Rotate(int x) {
  int y = fa[x], z = fa[y], d = s[1][y] == x;
  fa[s[d][y] = s[d ^ 1][x]] = y, fa[x] = z;
  if (nroot(y)) s[s[1][z] == y][z] = x;
  fa[y] = x, s[d ^ 1][x] = y;
}
inline void Splay(int x) {
  Pushdown(x);
  for (int y = fa[x]; nroot(x); Rotate(x), y = fa[x])
    if (nroot(y)) Rotate(s[0][y] == x ^ s[0][fa[y]] == y ? x : y);
}
inline void Access(int x) {
  for (int y = 0; x; x = fa[y = x]) Splay(x), s[1][x] = y;
}
inline void MakeRt(int x) { Access(x), Splay(x), pushr(x); }
inline int FindRt(int x) {
  Access(x), Splay(x);
  for (; s[0][x]; pushdown(x), x = s[0][x])
    ;
  Splay(x);
  return x;
}
inline void Link(int x, int y) {
  MakeRt(x);
  if (FindRt(y) != x) fa[x] = y;
}
inline void Cut(int x, int y) {
  MakeRt(x);
  if (FindRt(y) == x && fa[y] == x && !s[0][y]) fa[y] = s[1][x] = 0;
}
}  // namespace Lct
inline bool check(int x, int y) { return Lct::FindRt(x) == Lct::FindRt(y); }
inline void del(int t) {
  int x = pos[t].first, y = pos[t].second;
  for (int k = 0; k < 4; k++) {
    int px = x + tx[k], py = y + ty[k];
    if (!id[px][py]) continue;
    if (a[px][py] <= r && a[px][py] >= l) Lct::Cut(id[x][y], id[px][py]);
  }
}
inline void Merge(int x, int y) { Lct::Link(x, y); }
namespace SegTree {
int Min[MAXN << 2], cnt[MAXN << 2], t[MAXN << 2];
inline void pushup(int x) {
  Min[x] = min(Min[x << 1], Min[x << 1 | 1]), cnt[x] = 0,
  Min[x] == Min[x << 1] ? cnt[x] += cnt[x << 1] : 0,
  Min[x] == Min[x << 1 | 1] ? cnt[x] += cnt[x << 1 | 1] : 0;
}
inline void pushr(int x, int v) { Min[x] += v, t[x] += v; }
inline void pushdown(int x) {
  if (t[x]) pushr(x << 1, t[x]), pushr(x << 1 | 1, t[x]), t[x] = 0;
}
inline void add(int x, int l, int r, int ql, int qr, int v) {
  if (ql <= l && r <= qr) return pushr(x, v);
  int mid = l + r >> 1;
  pushdown(x);
  if (ql <= mid) add(x << 1, l, mid, ql, qr, v);
  if (qr > mid) add(x << 1 | 1, mid + 1, r, ql, qr, v);
  pushup(x);
}
inline int ask(int x, int l, int r, int ql, int qr) {
  if (Min[x] > 1) return 0;
  if (ql <= l && r <= qr) return Min[x] == 1 ? cnt[x] : 0;
  int mid = l + r >> 1, res = 0;
  pushdown(x);
  if (ql <= mid) res += ask(x << 1, l, mid, ql, qr);
  if (qr > mid) res += ask(x << 1 | 1, mid + 1, r, ql, qr);
  return res;
}
inline void build(int x, int l, int r) {
  Min[x] = 0, cnt[x] = r - l + 1;
  if (l == r) return;
  int mid = l + r >> 1;
  build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
}
}  // namespace SegTree
int main() {
  n = read(), m = read();
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) id[i][j] = ++tot;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) pos[a[i][j] = read()] = make_pair(i, j);
  long long ans = 0;
  SegTree::build(1, 1, n * m);
  for (l = 1, r = 1; r <= n * m; r++) {
    int x = pos[r].first, y = pos[r].second;
    for (int k = 0; k < 4; k++) {
      int px = x + tx[k], py = y + ty[k];
      if (!id[px][py]) continue;
      if (a[px][py] <= r && a[px][py] >= l)
        for (; a[px][py] >= l && l < r && check(id[px][py], id[x][y]);
             del(l), l++)
          ;
      if (a[px][py] <= r && a[px][py] >= l) Merge(id[x][y], id[px][py]);
    }
    SegTree::add(1, 1, n * m, l, r, 1);
    for (int k = 0; k < 4; k++) {
      int px = x + tx[k], py = y + ty[k];
      if (!id[px][py]) continue;
      if (a[px][py] <= r && a[px][py] >= l)
        SegTree::add(1, 1, n * m, l, a[px][py], -1);
    }
    ans += SegTree::ask(1, 1, n * m, l, r);
  }
  cout << ans << "\n";
  return 0;
}