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;
int num[200010], nxt[200010 << 2], to[200010 << 2], head[200010], cnt;
void add(int u, int v) {
  nxt[++cnt] = head[u];
  to[cnt] = v;
  head[u] = cnt;
}
int id[2010][2010];
const int ux[5] = {0, 1, 0, -1}, uy[5] = {1, 0, -1, 0};
struct LCT {
  int ch[200010][2], fa[200010], tag[200010], cnt;
  bool not_root(int u) { return ch[fa[u]][0] == u || ch[fa[u]][1] == u; }
  void set_tag(int u) {
    swap(ch[u][0], ch[u][1]);
    tag[u] ^= 1;
  }
  void push_down(int u) {
    if (!tag[u]) return;
    tag[u] = false;
    set_tag(ch[u][0]);
    set_tag(ch[u][1]);
  }
  void rotate(int u) {
    int f = fa[u], ff = fa[f];
    int k = ch[f][1] == u, v = ch[u][!k];
    if (not_root(f)) ch[ff][ch[ff][1] == f] = u;
    ch[u][!k] = f;
    ch[f][k] = v;
    if (v) fa[v] = f;
    fa[f] = u;
    fa[u] = ff;
  }
  int ton[200010], top;
  void push_all(int u) {
    top = 0;
    while (not_root(u)) ton[++top] = u, u = fa[u];
    ton[++top] = u;
    for (; top; top--) push_down(ton[top]);
  }
  void splay(int u) {
    push_all(u);
    while (not_root(u)) {
      int f = fa[u], ff = fa[f];
      if (not_root(f)) rotate((ch[ff][0] == f) ^ (ch[f][0] == u) ? u : f);
      rotate(u);
    }
  }
  void access(int x) {
    for (int y = 0; x; y = x, x = fa[x]) splay(x), ch[x][1] = y;
  }
  void make_root(int x) {
    access(x);
    splay(x);
    set_tag(x);
  }
  void split(int x, int y) {
    make_root(x);
    access(y);
    splay(y);
  }
  int find_root(int x) {
    access(x);
    splay(x);
    while (ch[x][0]) push_down(x), x = ch[x][0];
    splay(x);
    return x;
  }
  bool link(int u, int v) {
    make_root(u);
    if (find_root(v) == u) return false;
    fa[u] = v;
    return true;
  }
  void cut(int u, int v) {
    make_root(u);
    if (find_root(v) != u || fa[v] != u || ch[v][0]) return;
    fa[v] = 0, ch[u][1] = 0;
  }
} t0;
int val[200010 << 2], tv[200010 << 2], tag[200010 << 2];
void update(int u) {
  tv[u] = 0, val[u] = min(val[u << 1], val[u << 1 | 1]);
  tv[u] = (int)(val[u] == val[u << 1]) * tv[u << 1] +
          (int)(val[u] == val[u << 1 | 1]) * tv[u << 1 | 1];
}
void build(int u, int l, int r) {
  val[u] = 0, tv[u] = r - l + 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}
void set_tag(int u, int v) { tag[u] += v, val[u] += v; }
void push_down(int u) {
  if (tag[u]) set_tag(u << 1, tag[u]), set_tag(u << 1 | 1, tag[u]), tag[u] = 0;
}
void insert(int u, int l, int r, int L, int R, int v) {
  if (L <= l && r <= R) {
    set_tag(u, v);
    return;
  }
  push_down(u);
  int mid = (l + r) >> 1;
  if (L <= mid) insert(u << 1, l, mid, L, R, v);
  if (R > mid) insert(u << 1 | 1, mid + 1, r, L, R, v);
  update(u);
}
int rres, rcnt;
int answer(int u, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    return val[u] == 1 ? tv[u] : 0;
  }
  push_down(u);
  int mid = (l + r) >> 1, ans = 0;
  if (L <= mid) ans += answer(u << 1, l, mid, L, R);
  if (R > mid) ans += answer(u << 1 | 1, mid + 1, r, L, R);
  return ans;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &id[i][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int _ = 0; _ < 4; _++) {
        int x = i + ux[_], y = j + uy[_];
        if (x < 1 || y < 1 || x > n || y > m || id[x][y] > id[i][j]) continue;
        add(id[x][y], id[i][j]), add(id[i][j], id[x][y]);
      }
  int r = 0, t = n * m;
  long long ans = 0;
  build(1, 1, t);
  for (int l = 1; l <= t; l++) {
    for (int u = r + 1; u <= t; u++) {
      bool can = true;
      for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v < u && v >= l && !t0.link(v, u)) {
          can = false;
          break;
        }
      }
      for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v >= u || v < l) continue;
        t0.cut(v, u);
      }
      if (!can) break;
      r = u;
      int tot = 0;
      for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v < u && v >= l) t0.link(v, u), ++tot;
      }
      insert(1, 1, t, r, r, r - l + 1);
      insert(1, 1, t, r, t, -tot);
    }
    ans += answer(1, 1, t, l, r);
    for (int i = head[l]; i; i = nxt[i]) {
      int v = to[i];
      if (v <= r && v > l) t0.cut(v, l), insert(1, 1, t, v, t, 1);
    }
    insert(1, 1, t, l, r, -1);
  }
  printf("%lld\n", ans);
  return 0;
}