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 std::sort;
const int MAXN = 2 * 1e5 + 50;
int c[2][MAXN], f[MAXN];
bool rev[MAXN];
inline void pushdown(int x) {
  if (rev[x]) {
    rev[c[0][x]] ^= 1, rev[c[1][x]] ^= 1;
    c[0][x] ^= c[1][x] ^= c[0][x] ^= c[1][x];
    rev[x] = 0;
  }
}
inline bool isroot(int x) {
  return (!(c[0][f[x]] == x || c[1][f[x]] == x) || f[x] == 0);
}
inline bool get(int x) { return c[1][f[x]] == x; }
inline void rotate(int x) {
  bool r = get(x), rf = get(f[x]);
  int y = f[x], z = f[y], a = c[!r][x];
  f[x] = z;
  if (!isroot(y)) c[rf][z] = x;
  f[y] = x, c[!r][x] = y;
  f[a] = y, c[r][y] = a;
}
inline void pushall(int x) {
  if (!isroot(x)) pushall(f[x]);
  pushdown(x);
}
inline void splay(int x) {
  pushall(x);
  for (; !isroot(x); rotate(x))
    if (!isroot(f[x])) rotate((get(f[x]) == get(x)) ? f[x] : x);
}
inline void access(int x) {
  for (int pre = 0; x; pre = x, x = f[x]) {
    splay(x);
    c[1][x] = pre;
  }
}
inline void setroot(int x) {
  access(x), splay(x);
  rev[x] = 1;
}
inline bool connected2(int x, int y) {
  if (x == y) return 1;
  access(x), splay(x);
  access(y), splay(y);
  return f[x] != 0;
}
inline int findroot(int x) {
  access(x), splay(x);
  while (c[0][x]) x = c[0][x], pushdown(x);
  splay(x);
  return x;
}
inline bool connected(int x, int y) {
  if (findroot(x) == findroot(y))
    return 1;
  else
    return 0;
}
inline void cut(int x, int y) {
  setroot(x), access(y), splay(x);
  c[1][x] = f[y] = 0;
}
inline void link(int x, int y) {
  setroot(x);
  f[x] = y;
}
int N = 1, min[3 * MAXN], cnt[3 * MAXN], det[3 * MAXN];
bool lazy[3 * MAXN];
inline void pushupSeg(int v) {
  if (min[v << 1] == min[(v << 1) | 1]) {
    min[v] = min[v << 1];
    cnt[v] = cnt[v << 1] + cnt[(v << 1) | 1];
  } else if (min[v << 1] < min[(v << 1) | 1])
    min[v] = min[v << 1], cnt[v] = cnt[v << 1];
  else
    min[v] = min[(v << 1) | 1], cnt[v] = cnt[(v << 1) | 1];
}
inline void pushdownSeg(int v) {
  if (lazy[v]) {
    min[v << 1] += det[v], min[(v << 1) | 1] += det[v];
    det[v << 1] += det[v], det[(v << 1) | 1] += det[v];
    lazy[v << 1] = lazy[(v << 1) | 1] = 1;
    lazy[v] = 0;
    det[v] = 0;
  }
}
inline void updata(int l, int r, int d, int v = 1, int nl = 1, int nr = N) {
  if (l > r) return;
  if (l == nl && r == nr) {
    min[v] += d;
    det[v] += d;
    lazy[v] = 1;
    return;
  }
  pushdownSeg(v);
  int mid = (nl + nr) >> 1;
  if (r <= mid)
    updata(l, r, d, v << 1, nl, mid);
  else if (l > mid)
    updata(l, r, d, (v << 1) | 1, mid + 1, nr);
  else
    updata(l, mid, d, v << 1, nl, mid),
        updata(mid + 1, r, d, (v << 1) | 1, mid + 1, nr);
  pushupSeg(v);
}
inline int query(int l, int r, int v = 1, int nl = 1, int nr = N) {
  if (l == nl && r == nr) {
    if (min[v] != 1)
      return 0;
    else
      return cnt[v];
  }
  pushdownSeg(v);
  int mid = (nl + nr) >> 1;
  if (r <= mid)
    return query(l, r, v << 1, nl, mid);
  else if (l > mid)
    return query(l, r, (v << 1) | 1, mid + 1, nr);
  else
    return query(l, mid, v << 1, nl, mid) +
           query(mid + 1, r, (v << 1) | 1, mid + 1, nr);
}
int posx[MAXN], posy[MAXN], dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int map[2000][2000], n, m;
inline int read() {
  int x = 0;
  char cc = getchar();
  while (cc < '0' || cc > '9') cc = getchar();
  while (cc >= '0' && cc <= '9')
    x = (x << 3) + (x << 1) + (48 ^ cc), cc = getchar();
  return x;
}
inline int outmap(int x, int y) {
  if (x < 0 || x >= n) return -1;
  if (y < 0 || y >= m) return -1;
  return map[x][y];
}
inline int test() {
  N = 8;
  for (int k = 1, count = N, d = 1; k <= N; d += k, k <<= 1, count >>= 1)
    for (int i = 0; i < k; ++i) cnt[d + i] = count;
  printf("%d\n", query(1, 8));
  updata(1, 4, 1);
  printf("%d\n", query(1, 8));
  updata(1, 3, -1);
  printf("%d\n", query(1, 8));
  updata(1, 2, -8);
  printf("%d\n", query(1, 1));
  return 0;
}
int main() {
  n = read(), m = read();
  while (N < n * m) N <<= 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      map[i][j] = read();
      posx[map[i][j]] = i, posy[map[i][j]] = j;
    }
  for (int k = 1, count = N, d = 1; k <= N; d += k, k <<= 1, count >>= 1)
    for (int i = 0; i < k; ++i) cnt[d + i] = count;
  long long ans = 0;
  for (int l = 1, r = 0; l <= n * m; ++l) {
    for (int r2 = r + 1; r2 <= n * m; ++r2) {
      int lnk[4], tot = 0, count = 0;
      bool brek = 0;
      for (int d = 0; d < 4; ++d) {
        int to = outmap(posx[r2] + dx[d], posy[r2] + dy[d]);
        if (to == -1 || to > r2 || to < l) continue;
        if (!connected(r2, to))
          link(r2, to), lnk[tot++] = to, ++count;
        else {
          brek = 1;
          break;
        }
      }
      if (brek) {
        for (int i = 0; i < tot; ++i) cut(r2, lnk[i]);
        break;
      }
      updata(r2, n * m, 1 - count);
      r = r2;
    }
    ans += query(l, r);
    int lnk[4], tot = 0;
    for (int d = 0; d < 4; ++d) {
      int to = outmap(posx[l] + dx[d], posy[l] + dy[d]);
      if (to == -1) continue;
      if (connected(l, to)) cut(l, to), lnk[tot++] = to;
    }
    sort(lnk, lnk + tot);
    if (tot > 0)
      updata(l, lnk[0] - 1, -1);
    else
      updata(l, m * n, -1);
    for (int i = 1; i < tot; ++i) {
      if (!connected(lnk[i - 1], lnk[i])) updata(lnk[i], n * m, 1);
    }
  }
  printf("%lld", ans);
}