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 N = 2e5 + 5, M = 1005;
const int way[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
struct node {
  int x, y;
} a[N];
int n, m, up, top, qx, qy, qz;
long long ans;
int fa[N], s[N][2], st[N];
bool rev[N];
pair<int, int> f[N << 2];
int b[M][M], c[N << 2];
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) w |= ch == '-', ch = getchar();
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
inline bool pd(int x) { return x == s[fa[x]][1]; }
inline bool isroot(int x) { return x ^ s[fa[x]][0] && x ^ s[fa[x]][1]; }
inline void rotate(int x) {
  int y = fa[x], w = pd(x);
  if ((fa[x] = fa[y]) && !isroot(y)) s[fa[y]][pd(y)] = x;
  if (s[y][w] = s[x][w ^ 1]) fa[s[y][w]] = y;
  s[fa[y] = x][w ^ 1] = y;
}
inline void reverse(int x) {
  if (x) swap(s[x][0], s[x][1]), rev[x] ^= 1;
}
inline void down(int x) {
  if (rev[x]) {
    reverse(s[x][0]), reverse(s[x][1]);
    rev[x] = false;
  }
}
inline void splay(int x) {
  for (int y = st[top = 1] = x; !isroot(y); y = fa[y]) st[++top] = fa[y];
  while (top) down(st[top--]);
  for (int y; !isroot(x); rotate(x))
    if (!isroot(y = fa[x])) rotate(pd(x) == pd(y) ? y : x);
}
inline void access(int x) {
  for (int y = 0; x; x = fa[y = x]) splay(x), s[x][1] = y;
}
inline void mkroot(int x) { access(x), splay(x), reverse(x); }
inline void link(int x, int y) { mkroot(x), fa[x] = y; }
inline void cut(int x, int y) {
  mkroot(x), access(y), splay(y);
  s[y][0] = fa[x] = 0;
}
inline bool connect(int x, int y) {
  mkroot(x), access(y), splay(y);
  while (s[y][0]) y = s[y][0];
  return x == y;
}
inline void down1(int v) {
  if (c[v]) {
    f[v << 1].first += c[v];
    f[v << 1 | 1].first += c[v];
    c[v << 1] += c[v];
    c[v << 1 | 1] += c[v];
    c[v] = 0;
  }
}
void make(int v, int l, int r) {
  if (l == r) {
    f[v].second = 1;
    return;
  }
  int mid = l + r >> 1;
  make(v << 1, l, mid);
  make(v << 1 | 1, mid + 1, r);
  f[v].second = f[v << 1].second + f[v << 1 | 1].second;
}
pair<int, int> cmin(pair<int, int> x, pair<int, int> y) {
  if (x.first < y.first) return x;
  if (x.first > y.first) return y;
  return make_pair(x.first, x.second + y.second);
}
void change(int v, int l, int r) {
  if (qx <= l && r <= qy) {
    f[v].first += qz;
    c[v] += qz;
    return;
  }
  down1(v);
  int mid = l + r >> 1;
  if (qx <= mid) change(v << 1, l, mid);
  if (qy > mid) change(v << 1 | 1, mid + 1, r);
  f[v] = cmin(f[v << 1], f[v << 1 | 1]);
}
pair<int, int> find(int v, int l, int r) {
  if (qx <= l && r <= qy) return f[v];
  down1(v);
  int mid = l + r >> 1;
  pair<int, int> val(N, 0);
  if (qx <= mid) val = find(v << 1, l, mid);
  if (qy > mid) val = cmin(val, find(v << 1 | 1, mid + 1, r));
  return val;
}
inline bool judge(int l, int r) {
  for (int i = 0; i < 3; i++) {
    int x = a[r].x + way[i][0], y = a[r].y + way[i][1];
    if (!x || x > n || !y || y > m) continue;
    if (b[x][y] < l || b[x][y] > r) continue;
    for (int j = i + 1; j < 4; j++) {
      int xx = a[r].x + way[j][0], yy = a[r].y + way[j][1];
      if (!xx || xx > n || !yy || yy > m) continue;
      if (b[xx][yy] < l || b[xx][yy] > r) continue;
      if (connect(b[x][y], b[xx][yy])) return true;
    }
  }
  return false;
}
inline void del(int l) {
  for (int i = 0; i < 4; i++) {
    int x = a[l].x + way[i][0], y = a[l].y + way[i][1];
    if (!x || x > n || !y || y > m) continue;
    if (connect(b[x][y], l)) cut(b[x][y], l);
  }
}
inline void add(int l, int r) {
  for (int i = 0; i < 4; i++) {
    int x = a[r].x + way[i][0], y = a[r].y + way[i][1];
    if (!x || x > n || !y || y > m) continue;
    if (l <= b[x][y] && b[x][y] < r) {
      link(b[x][y], r);
      qx = 1, qy = b[x][y], qz = -1;
      change(1, 1, up);
    }
  }
  qx = l, qy = r, qz = 1;
  change(1, 1, up);
}
int main() {
  n = read(), m = read(), up = n * m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      b[i][j] = read();
      a[b[i][j]] = (node){i, j};
    }
  make(1, 1, up);
  for (int i = 1, j = 1; i <= up; i++) {
    while (judge(j, i)) del(j++);
    add(j, i);
    qx = j, qy = i;
    pair<int, int> sum = find(1, 1, up);
    ans += sum.first == 1 ? sum.second : 0;
  }
  printf("%I64d", ans);
  return 0;
}