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;
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') t = true, ch = getchar();
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
struct Node {
  int ff, ch[2], rev;
} t[200200];
bool isroot(int x) { return t[t[x].ff].ch[0] != x && t[t[x].ff].ch[1] != x; }
void rotate(int x) {
  int y = t[x].ff, z = t[y].ff;
  int k = t[y].ch[1] == x;
  if (!isroot(y)) t[z].ch[t[z].ch[1] == y] = x;
  t[x].ff = z;
  t[y].ch[k] = t[x].ch[k ^ 1];
  t[t[x].ch[k ^ 1]].ff = y;
  t[x].ch[k ^ 1] = y;
  t[y].ff = x;
}
void pushdown(int x) {
  if (t[x].rev)
    t[(t[x].ch[0])].rev ^= 1, t[(t[x].ch[1])].rev ^= 1,
        swap((t[x].ch[0]), (t[x].ch[1])), t[x].rev ^= 1;
}
int S[200200], top;
void Splay(int x) {
  S[top = 1] = x;
  for (int i = x; !isroot(i); i = t[i].ff) S[++top] = t[i].ff;
  while (top) pushdown(S[top--]);
  while (!isroot(x)) {
    int y = t[x].ff, z = t[y].ff;
    if (!isroot(y))
      (t[y].ch[0] == x) ^ (t[z].ch[0] == y) ? rotate(x) : rotate(y);
    rotate(x);
  }
}
void access(int x) {
  for (int y = 0; x; y = x, x = t[x].ff) Splay(x), (t[x].ch[1]) = y;
}
void makeroot(int x) {
  access(x);
  Splay(x);
  t[x].rev ^= 1;
}
void split(int x, int y) {
  makeroot(x);
  access(y);
  Splay(y);
}
void link(int x, int y) {
  makeroot(x);
  t[x].ff = y;
}
void cut(int x, int y) {
  split(x, y);
  t[y].ch[0] = t[x].ff = 0;
}
int findroot(int x) {
  access(x);
  Splay(x);
  while ((t[x].ch[0])) x = (t[x].ch[0]);
  return x;
}
int n, m;
long long ans;
int a[1010][1010];
int X[200200], Y[200200];
int d[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
struct SegMent {
  struct Node {
    int v, s;
  } t[200200 << 2];
  int tag[200200 << 2];
  Node Merge(Node a, Node b) {
    Node c;
    c.v = min(a.v, b.v);
    c.s = 0;
    if (c.v == a.v) c.s += a.s;
    if (c.v == b.v) c.s += b.s;
    return c;
  }
  void pushup(int now) { t[now] = Merge(t[(now << 1)], t[(now << 1 | 1)]); }
  void Build(int now, int l, int r) {
    if (l == r) {
      t[now] = (Node){1, 1};
      return;
    }
    int mid = (l + r) >> 1;
    Build((now << 1), l, mid);
    Build((now << 1 | 1), mid + 1, r);
    pushup(now);
  }
  void puttag(int now, int w) {
    t[now].v += w;
    tag[now] += w;
  }
  void pushdown(int now) {
    puttag((now << 1), tag[now]);
    puttag((now << 1 | 1), tag[now]);
    tag[now] = 0;
  }
  void Modify(int now, int l, int r, int L, int R, int w) {
    if (L <= l && r <= R) {
      puttag(now, w);
      return;
    }
    int mid = (l + r) >> 1;
    pushdown(now);
    if (L <= mid) Modify((now << 1), l, mid, L, R, w);
    if (R > mid) Modify((now << 1 | 1), mid + 1, r, L, R, w);
    pushup(now);
  }
  Node Query(int now, int l, int r, int L, int R) {
    if (L == l && r == R) return t[now];
    int mid = (l + r) >> 1;
    pushdown(now);
    if (R <= mid) return Query((now << 1), l, mid, L, R);
    if (L > mid) return Query((now << 1 | 1), mid + 1, r, L, R);
    return Merge(Query((now << 1), l, mid, L, mid),
                 Query((now << 1 | 1), mid + 1, r, mid + 1, R));
  }
  int Query(int l, int r) {
    Node a = Query(1, 1, n * m, l, r);
    return a.v == 1 ? a.s : 0;
  }
} T;
bool check(int l, int r, int p) {
  for (int i = 0; i < 4; ++i) {
    int x = X[p] + d[i][0], y = Y[p] + d[i][1];
    if (x < 0 || y < 0 || x > n || y > m) continue;
    if (!(l <= a[x][y] && a[x][y] <= r)) continue;
    for (int j = i + 1; j < 4; ++j) {
      int xx = X[p] + d[j][0], yy = Y[p] + d[j][1];
      if (xx < 0 || yy < 0 || xx > n || yy > m) continue;
      if (!(l <= a[xx][yy] && a[xx][yy] <= r)) continue;
      if (findroot(a[x][y]) == findroot(a[xx][yy])) return false;
    }
  }
  return true;
}
void Work(int l, int r, int p, int opt) {
  for (int i = 0; i < 4; ++i) {
    int x = X[p] + d[i][0], y = Y[p] + d[i][1];
    if (x < 0 || y < 0 || x > n || y > m) continue;
    if (!(l <= a[x][y] && a[x][y] <= r)) continue;
    if (opt == -1)
      cut(p, a[x][y]);
    else
      link(p, a[x][y]), T.Modify(1, 1, n * m, 1, a[x][y], -1);
  }
}
int main() {
  n = read();
  m = read();
  T.Build(1, 1, n * m);
  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) X[a[i][j]] = i, Y[a[i][j]] = j;
  for (int i = 1, p = 1; i <= n * m; ++i) {
    while (!check(p, i - 1, i)) Work(p + 1, i - 1, p, -1), ++p;
    Work(p, i - 1, i, 1);
    ans += T.Query(p, i);
    T.Modify(1, 1, n * m, p, i, 1);
  }
  printf("%I64d\n", ans);
  return 0;
}