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 = 200005;
int n, m, id[maxn], ch[maxn][2], fa[maxn], nx[4] = {1, -1, 0, 0},
                                           ny[4] = {0, 0, 1, -1};
bool rev[maxn];
vector<int> g[maxn];
inline void reverse(int x) {
  if (!x) return;
  swap(ch[x][0], ch[x][1]), rev[x] ^= 1;
}
inline void pushdown(int x) {
  if (rev[x]) reverse(ch[x][0]), reverse(ch[x][1]), rev[x] = 0;
}
inline bool nrt(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
inline void rotate(int x) {
  int y = fa[x], z = fa[y], k = (ch[y][1] == x), u = ch[x][k ^ 1];
  if (nrt(y)) ch[z][ch[z][1] == y] = x;
  ch[y][k] = u, ch[x][k ^ 1] = y;
  if (u) fa[u] = y;
  fa[y] = x, fa[x] = z;
}
inline void splay(int x) {
  static int sta[maxn], top;
  int y, z;
  top = 0;
  for (y = x; nrt(y); y = fa[y]) sta[++top] = y;
  pushdown(y);
  for (; top; top--) pushdown(sta[top]);
  while (nrt(x)) {
    y = fa[x], z = fa[y];
    if (nrt(y)) rotate((ch[y][1] == x) ^ (ch[z][1] == y) ? x : y);
    rotate(x);
  }
}
inline void access(int x) {
  for (int y = 0; x; y = x, x = fa[x]) splay(x), ch[x][1] = y;
}
inline void makeroot(int x) { access(x), splay(x), reverse(x); }
inline int findroot(int x) {
  access(x), splay(x);
  for (; ch[x][0]; x = ch[x][0]) pushdown(x);
  splay(x);
  return x;
}
inline void split(int x, int y) { makeroot(x), access(y), splay(y); }
inline bool link(int x, int y) {
  makeroot(x);
  if (findroot(y) == x) return false;
  fa[x] = y;
  return true;
}
inline void cut(int x, int y) {
  split(x, y);
  if (fa[x] == y && !ch[x][1]) fa[x] = ch[y][0] = 0;
}
inline pair<int, int> operator+(const pair<int, int> &a,
                                const pair<int, int> &b) {
  return pair<int, int>(
      min(a.first, b.first),
      (a.first <= b.first) * a.second + (a.first >= b.first) * b.second);
}
struct Segment_Tree {
  pair<int, int> sum[maxn << 2];
  int add[maxn << 2];
  inline void pushdown(int rt) {
    if (add[rt])
      sum[(rt << 1)].first += add[rt], sum[(rt << 1 | 1)].first += add[rt],
          add[(rt << 1)] += add[rt], add[(rt << 1 | 1)] += add[rt], add[rt] = 0;
  }
  void build(int rt, int l, int r) {
    if (l == r) {
      sum[rt] = pair<int, int>(0, 1);
      return;
    }
    int mid = (l + r) >> 1;
    build((rt << 1), l, mid), build((rt << 1 | 1), mid + 1, r),
        sum[rt] = sum[(rt << 1)] + sum[(rt << 1 | 1)];
  }
  void update(int rt, int l, int r, int x, int y, int z) {
    if (x <= l && r <= y) {
      sum[rt].first += z, add[rt] += z;
      return;
    }
    pushdown(rt);
    int mid = (l + r) >> 1;
    if (x <= mid) update((rt << 1), l, mid, x, y, z);
    if (y > mid) update((rt << 1 | 1), mid + 1, r, x, y, z);
    sum[rt] = sum[(rt << 1)] + sum[(rt << 1 | 1)];
  }
  pair<int, int> query(int rt, int l, int r, int x, int y) {
    if (x <= l && r <= y) return sum[rt];
    pushdown(rt);
    int mid = (l + r) >> 1;
    if (y <= mid) return query((rt << 1), l, mid, x, y);
    if (x > mid) return query((rt << 1 | 1), mid + 1, r, x, y);
    return query((rt << 1), l, mid, x, y) +
           query((rt << 1 | 1), mid + 1, r, x, y);
  }
} T;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n * m; i++) scanf("%d", &id[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 0; k < 4; k++) {
        int x = i + nx[k], y = j + ny[k];
        if (x < 1 || x > n || y < 1 || y > m) continue;
        g[id[(i - 1) * m + j]].push_back(id[(x - 1) * m + y]);
      }
  n *= m, T.build(1, 1, n);
  long long ans = 0;
  for (int i = 1, j = 1; i <= n; i++) {
    for (; j <= n; j++) {
      int fl = 0, ecnt = 0;
      for (auto k : g[j])
        if (k >= i && k < j) {
          if (!link(j, k)) {
            fl = k;
            break;
          }
          ecnt++;
        }
      if (fl) {
        for (auto k : g[j])
          if (k >= i && k < j) {
            if (fl == k) break;
            cut(j, k);
          }
        break;
      }
      T.update(1, 1, n, j, j, j - i + 1), T.update(1, 1, n, j, n, -ecnt);
    }
    ans += T.query(1, 1, n, i, j - 1).second;
    for (auto k : g[i])
      if (k > i && k < j) cut(i, k), T.update(1, 1, n, k, n, 1);
    T.update(1, 1, n, i, j - 1, -1);
  }
  printf("%lld\n", ans);
  return 0;
}