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 = 1001, M = 2e5 + 1;
struct LCT {
  int f[M], ch[M][2];
  bool rev[M];
  bool isroot(int x) { return (ch[f[x]][0] != x) && (ch[f[x]][1] != x); }
  void Rev(int x) {
    swap(ch[x][0], ch[x][1]);
    rev[x] ^= 1;
  }
  void down(int x) {
    if (rev[x]) {
      Rev(ch[x][0]), Rev(ch[x][1]);
      rev[x] = 0;
    }
  }
  void pushdown(int x) {
    if (f[x]) pushdown(f[x]);
    down(x);
  }
  void rotate(int x) {
    int y = f[x], z = f[y], k = (ch[z][1] == y), d = (ch[y][1] == x);
    if (!isroot(y)) {
      ch[z][k] = x;
    }
    f[x] = z;
    ch[y][d] = ch[x][d ^ 1], f[ch[x][d ^ 1]] = y;
    ch[x][d ^ 1] = y, f[y] = x;
  }
  void splay(int x) {
    pushdown(x);
    while (!isroot(x)) {
      int y = f[x], z = f[y];
      if (!isroot(y)) (ch[z][1] == y) ^ (ch[y][1] == x) ? rotate(x) : rotate(y);
      rotate(x);
    }
  }
  void access(int x) {
    for (int y = 0; x; y = x, x = f[x]) {
      splay(x);
      ch[x][1] = y;
    }
  }
  void makeroot(int x) {
    access(x);
    splay(x);
    Rev(x);
  }
  int findroot(int x) {
    access(x);
    splay(x);
    for (; ch[x][0]; down(x), x = ch[x][0]) {
      splay(x);
    }
    return x;
  }
  void split(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
  }
  void link(int x, int y) { makeroot(x), f[x] = y; }
  void cut(int x, int y) { split(x, y), f[x] = ch[y][0] = 0; }
} T;
int sum[M << 2], tag[M << 2], v[M << 2];
int maxn, num;
void up(int x) {
  v[x] = min(v[x << 1], v[x << 1 | 1]), sum[x] = 0;
  if (v[x << 1] == v[x]) sum[x] += sum[x << 1];
  if (v[x << 1 | 1] == v[x]) sum[x] += sum[x << 1 | 1];
}
void down(int x) {
  if (tag[x]) {
    tag[x << 1] += tag[x], v[x << 1] += tag[x];
    tag[x << 1 | 1] += tag[x], v[x << 1 | 1] += tag[x];
    tag[x] = 0;
  }
}
void check(int x) {
  if (v[x] < maxn) {
    maxn = v[x], num = sum[x];
  } else if (v[x] == maxn) {
    num += sum[x];
  }
}
void build(int x, int l, int r) {
  if (l == r) {
    sum[x] = r - l + 1;
    v[x] = 0;
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
  up(x);
}
void change(int x, int l, int r, int L, int R, int w) {
  if (L <= l && r <= R) {
    tag[x] += w;
    v[x] += w;
    return;
  }
  down(x);
  int mid = (l + r) >> 1;
  if (L <= mid) change(x << 1, l, mid, L, R, w);
  if (R > mid) change(x << 1 | 1, mid + 1, r, L, R, w);
  up(x);
}
void ask(int x, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    check(x);
    return;
  }
  down(x);
  int mid = (l + r) >> 1;
  if (L <= mid) ask(x << 1, l, mid, L, R);
  if (R > mid) ask(x << 1 | 1, mid + 1, r, L, R);
  up(x);
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int a[N][N], idx[M], idy[M];
int n, m, nowl = 1, nowr = 1, p[10];
bool judge() {
  p[0] = 0;
  for (int i = 0; i < 4; ++i) {
    int x = idx[nowr + 1] + dx[i], y = idy[nowr + 1] + dy[i];
    if (!x || !y || x > n || y > m || a[x][y] < nowl || a[x][y] > nowr) {
      continue;
    }
    p[++p[0]] = T.findroot(a[x][y]);
  }
  sort(p + 1, p + 1 + p[0]);
  for (int i = 1; i < p[0]; ++i)
    if (p[i] == p[i + 1]) return false;
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      scanf("%d", &a[i][j]);
      idx[a[i][j]] = i, idy[a[i][j]] = j;
    }
  build(1, 1, n * m), change(1, 1, n * m, 1, 1, 1);
  long long ans = 1;
  for (; nowr < n * m;) {
    for (; nowl < nowr && !judge(); ++nowl) {
      for (int i = 0; i < 4; ++i) {
        int x = idx[nowl] + dx[i], y = idy[nowl] + dy[i];
        if (!x || !y || x > n || y > m || a[x][y] < nowl || a[x][y] > nowr) {
          continue;
        }
        T.cut(nowl, a[x][y]);
      }
    }
    ++nowr, change(1, 1, n * m, nowl, nowr, 1);
    for (int i = 0; i < 4; ++i) {
      int x = idx[nowr] + dx[i], y = idy[nowr] + dy[i];
      if (!x || !y || x > n || y > m || a[x][y] < nowl || a[x][y] > nowr) {
        continue;
      }
      T.link(nowr, a[x][y]), change(1, 1, n * m, nowl, a[x][y], -1);
    }
    maxn = 1e9, num = 0;
    ask(1, 1, n * m, nowl, nowr);
    ans += (maxn == 1) * num;
  }
  printf("%I64d\n", ans);
  return 0;
}