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 = 200005, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int n, m, tot, L = 1, R = 1, f[1005][1005], px[N], py[N], fa[N], ch[N][2],
               rev[N], ps[10], tg[N << 2];
struct st {
  int v, s;
} T[N << 2];
long long ans = 1;
inline int wh(int x) { return ch[fa[x]][1] == x; }
inline int isrt(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
void rot(int x) {
  int y = fa[x], z = fa[y], c = wh(x);
  ch[y][c] = ch[x][c ^ 1];
  if (ch[y][c]) fa[ch[y][c]] = y;
  fa[x] = z;
  if (!isrt(y)) ch[z][wh(y)] = x;
  ch[x][c ^ 1] = y;
  fa[y] = x;
}
inline void rever(int x) {
  swap(ch[x][0], ch[x][1]);
  rev[x] ^= 1;
}
inline void dnn(int x) {
  if (rev[x]) rever(ch[x][0]), rever(ch[x][1]), rev[x] = 0;
}
inline void psh(int x) {
  if (!isrt(x)) psh(fa[x]);
  dnn(x);
}
inline void splay(int x) {
  psh(x);
  for (int y = fa[x]; !isrt(x); rot(x), y = fa[x])
    if (!isrt(y)) wh(x) ^ wh(y) ? rot(x) : rot(y);
}
inline void access(int x) {
  for (int y = 0; x; x = fa[y = x]) splay(x), ch[x][1] = y;
}
inline void mkrt(int x) {
  access(x);
  splay(x);
  rever(x);
}
inline int fndrt(int x) {
  access(x);
  splay(x);
  while (ch[x][0]) x = ch[x][0];
  splay(x);
  return x;
}
inline void split(int x, int y) {
  mkrt(x);
  access(y);
  splay(y);
}
inline void link(int x, int y) {
  mkrt(x);
  fa[x] = y;
}
inline void cut(int x, int y) {
  split(x, y);
  fa[x] = ch[y][0] = 0;
}
inline bool chk() {
  int len = 0;
  for (int d = 0; d < 4; d++) {
    int x = px[R + 1] + dx[d], y = py[R + 1] + dy[d];
    if (!x || x == n + 1 || !y || y == m + 1 || f[x][y] < L || f[x][y] > R)
      continue;
    ps[++len] = fndrt(f[x][y]);
  }
  sort(ps + 1, ps + len + 1);
  for (int i = 1; i < len; i++)
    if (ps[i] == ps[i + 1]) return 0;
  return 1;
}
inline st operator+(st a, st b) {
  return (st){min(a.v, b.v), (a.v <= b.v ? a.s : 0) + (b.v <= a.v ? b.s : 0)};
}
inline void cov(int x, int v) {
  T[x].v += v;
  tg[x] += v;
}
inline void dn(int x) {
  if (tg[x]) cov(x << 1, tg[x]), cov(x << 1 | 1, tg[x]), tg[x] = 0;
}
void build(int x, int l, int r) {
  if (l == r) {
    T[x] = (st){0, 1};
    return;
  }
  int mid = l + r >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  T[x] = T[x << 1] + T[x << 1 | 1];
}
void upd(int x, int l, int r, int tl, int tr, int v) {
  if (tl <= l && r <= tr) {
    cov(x, v);
    return;
  }
  int mid = l + r >> 1;
  dn(x);
  if (tl <= mid) upd(x << 1, l, mid, tl, tr, v);
  if (tr > mid) upd(x << 1 | 1, mid + 1, r, tl, tr, v);
  T[x] = T[x << 1] + T[x << 1 | 1];
}
st qry(int x, int l, int r, int tl, int tr) {
  if (tl <= l && r <= tr) return T[x];
  int mid = l + r >> 1;
  dn(x);
  if (tr <= mid) return qry(x << 1, l, mid, tl, tr);
  if (tl > mid) return qry(x << 1 | 1, mid + 1, r, tl, tr);
  return qry(x << 1, l, mid, tl, tr) + qry(x << 1 | 1, mid + 1, r, tl, tr);
}
int main() {
  scanf("%d%d", &n, &m);
  tot = n * m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &f[i][j]), px[f[i][j]] = i, py[f[i][j]] = j;
  build(1, 1, tot);
  upd(1, 1, tot, 1, 1, 1);
  while (R < tot) {
    for (; L < R && !chk(); L++)
      for (int d = 0; d < 4; d++) {
        int x = px[L] + dx[d], y = py[L] + dy[d];
        if (!x || x == n + 1 || !y || y == m + 1 || f[x][y] < L || f[x][y] > R)
          continue;
        cut(L, f[x][y]);
      }
    R++;
    upd(1, 1, tot, L, R, 1);
    for (int d = 0; d < 4; d++) {
      int x = px[R] + dx[d], y = py[R] + dy[d];
      if (!x || x == n + 1 || !y || y == m + 1 || f[x][y] < L || f[x][y] > R)
        continue;
      link(R, f[x][y]);
      upd(1, 1, tot, L, f[x][y], -1);
    }
    st res = qry(1, 1, tot, L, R);
    ans += res.v == 1 ? res.s : 0;
  }
  printf("%lld\n", ans);
  return 0;
}