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 gi() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
  return sum;
}
const int maxn = 200005;
const int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int n, m, N;
int f[maxn], ch[maxn][2], rev[maxn], x[maxn], y[maxn];
int p[1005][1005];
inline void rotate(int x) {
  int fa = f[x], gfa = f[fa], k = (ch[f[x]][1] == x);
  ch[fa][k] = ch[x][k ^ 1];
  f[ch[x][k ^ 1]] = fa;
  ch[x][k ^ 1] = fa;
  if (!(ch[f[fa]][0] != fa && ch[f[fa]][1] != fa))
    ch[gfa][(ch[f[fa]][1] == fa)] = x;
  f[fa] = x;
  f[x] = gfa;
}
inline void pushdown(int x) {
  if (!rev[x]) return;
  if (ch[x][0]) (swap(ch[ch[x][0]][0], ch[ch[x][0]][1]), rev[ch[x][0]] ^= 1);
  if (ch[x][1]) (swap(ch[ch[x][1]][0], ch[ch[x][1]][1]), rev[ch[x][1]] ^= 1);
  rev[x] = 0;
}
inline void splay(int x) {
  static int stk[maxn], top, fa;
  stk[top = 1] = x;
  while (!(ch[f[x]][0] != x && ch[f[x]][1] != x)) stk[++top] = x = f[x];
  while (top) pushdown(stk[top--]);
  x = stk[1];
  while (!(ch[f[x]][0] != x && ch[f[x]][1] != x)) {
    fa = f[x];
    if (!(ch[f[fa]][0] != fa && ch[f[fa]][1] != fa))
      (ch[f[x]][1] == x) ^ (ch[f[fa]][1] == fa) ? rotate(x) : rotate(fa);
    rotate(x);
  }
}
inline void access(int x) {
  for (int y = 0; x; y = x, x = f[x]) splay(x), ch[x][1] = y;
}
inline void make_root(int x) {
  access(x);
  splay(x);
  (swap(ch[x][0], ch[x][1]), rev[x] ^= 1);
}
inline void link(int x, int y) {
  make_root(x);
  make_root(y);
  f[y] = x;
}
inline void cut(int x, int y) {
  make_root(x);
  access(y);
  splay(y);
  ch[y][0] = f[x] = 0;
}
inline int find(int x) {
  access(x);
  splay(x);
  while (ch[x][0]) x = ch[x][0];
  splay(x);
  return x;
}
int Min[maxn << 2], sum[maxn << 2], tag[maxn << 2];
void build(int s, int l, int r) {
  if (l == r) return Min[s] = 0, sum[s] = 1, void();
  build((s << 1), l, ((l + r) >> 1));
  build((s << 1 | 1), ((l + r) >> 1) + 1, r);
  if (Min[(s << 1)] == Min[(s << 1 | 1)])
    sum[s] = sum[(s << 1)] + sum[(s << 1 | 1)];
  else if (Min[(s << 1)] < Min[(s << 1 | 1)])
    sum[s] = sum[(s << 1)];
  else
    sum[s] = sum[(s << 1 | 1)];
  Min[s] += tag[s];
}
void insert(int s, int l, int r, int ql, int qr, int v) {
  if (ql <= l && r <= qr) return tag[s] += v, Min[s] += v, void();
  if (ql <= ((l + r) >> 1)) insert((s << 1), l, ((l + r) >> 1), ql, qr, v);
  if (qr >= ((l + r) >> 1) + 1)
    insert((s << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, v);
  if (Min[(s << 1)] == Min[(s << 1 | 1)])
    sum[s] = sum[(s << 1)] + sum[(s << 1 | 1)];
  else if (Min[(s << 1)] < Min[(s << 1 | 1)])
    sum[s] = sum[(s << 1)];
  else
    sum[s] = sum[(s << 1 | 1)];
  Min[s] = min(Min[(s << 1)], Min[(s << 1 | 1)]) + tag[s];
}
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
  return a.first == b.first ? make_pair(a.first, a.second + b.second)
                            : min(a, b);
}
pair<int, int> query(int s, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) return make_pair(Min[s], sum[s]);
  pair<int, int> res(1e9, 0);
  if (ql <= ((l + r) >> 1)) res = query((s << 1), l, ((l + r) >> 1), ql, qr);
  if (qr >= ((l + r) >> 1) + 1)
    res = res + query((s << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr);
  res.first += tag[s];
  return res;
}
int check(int l, int r) {
  for (int x1, y1, i = 0; i < 4; ++i) {
    x1 = x[r] + d[i][0];
    y1 = y[r] + d[i][1];
    if (p[x1][y1] < l || p[x1][y1] >= r) continue;
    for (int x2, y2, j = i + 1; j < 4; ++j) {
      x2 = x[r] + d[j][0];
      y2 = y[r] + d[j][1];
      if (p[x2][y2] < l || p[x2][y2] >= r) continue;
      if (find(p[x1][y1]) == find(p[x2][y2])) return 1;
    }
  }
  return 0;
}
void add(int t, int v, int l, int r) {
  insert(1, 1, N, 1, t, v);
  for (int x1, y1, i = 0; i < 4; ++i) {
    x1 = x[t] + d[i][0];
    y1 = y[t] + d[i][1];
    if (p[x1][y1] < l || p[x1][y1] >= r) continue;
    insert(1, 1, N, 1, min(t, p[x1][y1]), -v);
    if (v > 0)
      assert(find(p[x1][y1]) != find(t)), link(p[x1][y1], t);
    else
      assert(find(p[x1][y1]) == find(t)), cut(p[x1][y1], t);
  }
}
int main() {
  n = gi();
  m = gi();
  N = n * m;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) p[i][j] = gi(), x[p[i][j]] = i, y[p[i][j]] = j;
  long long ans = 0;
  pair<int, int> res;
  build(1, 1, N);
  for (int r = 1, l = 1; r <= N; ++r) {
    while (l < r && check(l, r)) {
      add(l, -1, l, r), ++l;
    }
    add(r, 1, l, r);
    res = query(1, 1, N, l, r);
    ans += res.first == 1 ? res.second : 0;
  }
  printf("%lld\n", ans);
  return 0;
}