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;
namespace IO {
char ibuf[(1 << 21) + 1], *iS, *iT;
char Get() {
  return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, (1 << 21) + 1, stdin),
                      (iS == iT ? EOF : *iS++))
                   : *iS++);
}
int read() {
  int x = 0, c = Get();
  while (!isdigit(c)) c = Get();
  while (isdigit(c)) x = x * 10 + c - 48, c = Get();
  return x;
}
}  // namespace IO
using IO::read;
const int N = 2007, M = 1000007;
int n, m;
namespace Graph {
int f[N][N], dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
vector<int> E[M];
void buildG() {
  for (int i = 1, j; i <= n; ++i)
    for (j = 1; j <= m; ++j) f[i][j] = read();
  for (int i = 1, j, k, x, y; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      for (k = 0; k < 4; ++k) {
        x = i + dx[k], y = j + dy[k];
        if (x >= 1 && x <= n && y >= 1 && y <= m) E[f[i][j]].push_back(f[x][y]);
      }
}
}  // namespace Graph
using namespace Graph;
namespace LCT {
int fa[M], ch[M][2], rev[M];
int nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
void pushrev(int x) { swap(ch[x][0], ch[x][1]), rev[x] ^= 1; }
void pushdown(int x) {
  if (!rev[x]) return;
  rev[x] = 0, pushrev(ch[x][0]), pushrev(ch[x][1]);
}
void pushall(int x) {
  if (nroot(x)) pushall(fa[x]);
  pushdown(x);
}
void rotate(int x) {
  int y = fa[x], z = fa[y], k = ch[y][1] == x;
  if (nroot(y)) ch[z][ch[z][1] == y] = x;
  fa[x] = z, fa[ch[x][!k]] = y, ch[y][k] = ch[x][!k], fa[y] = x, ch[x][!k] = y;
}
void splay(int x) {
  pushall(x);
  for (int y; nroot(x); rotate(x))
    if (nroot(y = fa[x])) rotate((ch[fa[y]][0] == y) ^ (ch[y][0] == x) ? x : y);
}
void access(int x) {
  for (int y = 0; x; x = fa[y = x]) splay(x), ch[x][1] = y;
}
void makeroot(int x) { access(x), splay(x), pushrev(x); }
int findroot(int x) {
  access(x), splay(x);
  while (ch[x][0]) x = ch[x][0];
  return splay(x), x;
}
int link(int x, int y) { return makeroot(x), findroot(y) == x ? 0 : fa[x] = y; }
void cut(int x, int y) {
  makeroot(x);
  if (findroot(y) == x && fa[y] == x && !ch[y][0]) fa[y] = ch[x][1] = 0;
}
}  // namespace LCT
using namespace LCT;
namespace SegTree {
int mn[M << 2], sum[M << 2], tag[M << 2];
void pushup(int p) {
  if (mn[p << 1] < mn[p << 1 | 1]) mn[p] = mn[p << 1], sum[p] = sum[p << 1];
  if (mn[p << 1 | 1] < mn[p << 1])
    mn[p] = mn[p << 1 | 1], sum[p] = sum[p << 1 | 1];
  if (mn[p << 1] == mn[p << 1 | 1])
    mn[p] = mn[p << 1], sum[p] = sum[p << 1] + sum[p << 1 | 1];
}
void modify(int p, int v) { mn[p] += v, tag[p] += v; }
void pushdown(int p) {
  modify(p << 1, tag[p]), modify(p << 1 | 1, tag[p]), tag[p] = 0;
}
void buildS(int p, int l, int r) {
  if (l == r) return (void)(sum[p] = 1);
  buildS(p << 1, l, ((l + r) >> 1)), buildS(p << 1 | 1, ((l + r) >> 1) + 1, r),
      pushup(p);
}
void update(int p, int l, int r, int L, int R, int x) {
  if (L <= l && r <= R) return modify(p, x);
  if (tag[p]) pushdown(p);
  if (L <= ((l + r) >> 1)) update(p << 1, l, ((l + r) >> 1), L, R, x);
  if (R > ((l + r) >> 1)) update(p << 1 | 1, ((l + r) >> 1) + 1, r, L, R, x);
  pushup(p);
}
int query(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return mn[p] == 1 ? sum[p] : 0;
  if (tag[p]) pushdown(p);
  return (L <= ((l + r) >> 1) ? query(p << 1, l, ((l + r) >> 1), L, R) : 0) +
         (R > ((l + r) >> 1) ? query(p << 1 | 1, ((l + r) >> 1) + 1, r, L, R)
                             : 0);
}
}  // namespace SegTree
using namespace SegTree;
int main() {
  long long ans = 0;
  int l = 1, r = 0, S;
  n = read(), m = read(), buildG(), buildS(1, 1, S = n * m);
  for (int f, c; l <= S; ++l) {
    for (; r < S;) {
      f = c = 0, ++r;
      for (int v : E[r])
        if (v <= r && v >= l && !link(v, r)) {
          f = 1;
          break;
        }
      for (int v : E[r]) cut(v, r);
      if (f == 1) {
        --r;
        break;
      }
      for (int v : E[r])
        if (v <= r && v >= l) link(v, r), ++c;
      update(1, 1, S, r, r, r - l + 1), update(1, 1, S, r, S, -c);
    }
    ans += query(1, 1, S, l, r);
    for (int v : E[l])
      if (v <= r && v >= l) cut(v, l), update(1, 1, S, v, S, 1);
    update(1, 1, S, l, r, -1);
  }
  printf("%lld", ans);
}