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 read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int INF = 9999999;
const int MAXN = 1000100;
const int MAXM = 200010;
int N, M;
int a[MAXM + 1];
namespace LCT {
int ch[MAXN + 1][2];
pair<int, int> mx[MAXN + 1];
bool rev[MAXN + 1];
int fa[MAXN + 1];
int val[MAXN + 1];
inline bool chd(int x) { return ch[fa[x]][1] == x; }
inline bool nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
inline void merge(pair<int, int> &a, pair<int, int> b) {
  if (a.first < b.first) a = b;
}
inline void update(int x) {
  mx[x] = make_pair(val[x], x);
  if (ch[x][0]) merge(mx[x], mx[ch[x][0]]);
  if (ch[x][1]) merge(mx[x], mx[ch[x][1]]);
}
inline void Colorf(int x) {
  if (!x) return;
  rev[x] ^= 1;
  swap(ch[x][0], ch[x][1]);
}
inline void pushdown(int x) {
  if (rev[x]) {
    Colorf(ch[x][0]);
    Colorf(ch[x][1]);
    rev[x] = 0;
  }
}
inline void Con(int son, int Fa, int c) {
  ch[Fa][c] = son;
  fa[son] = Fa;
}
inline void rotate(int x) {
  int y = fa[x], z = fa[y];
  bool cx = chd(x), cy = chd(y);
  int b = ch[x][cx ^ 1];
  if (nroot(y))
    Con(x, z, cy);
  else
    fa[x] = z;
  Con(y, x, cx ^ 1);
  if (b)
    Con(b, y, cx);
  else
    ch[y][cx] = 0;
  update(y);
  update(x);
}
int sta[MAXN + 1];
inline void Splay(int x) {
  int top = 0, y = x;
  sta[++top] = x;
  while (nroot(y)) sta[++top] = y = fa[y];
  while (top) pushdown(sta[top--]);
  while (nroot(x)) {
    y = fa[x];
    if (nroot(y)) {
      rotate((chd(x) ^ chd(y)) ? x : y);
    }
    rotate(x);
  }
  update(x);
}
inline void access(int x) {
  for (int y = 0; x; y = x, x = fa[x]) {
    Splay(x);
    ch[x][1] = y;
    update(x);
  }
}
inline void chroot(int x) {
  access(x);
  Splay(x);
  Colorf(x);
}
inline void split(int x, int y) {
  chroot(x);
  access(y);
  Splay(y);
  return;
}
inline int findroot(int x) {
  access(x);
  Splay(x);
  while (ch[x][0]) x = ch[x][0];
  return x;
}
inline void Cut(int x, int y) {
  chroot(x);
  if (findroot(y) != x || fa[x] != y || ch[x][1]) return;
  fa[x] = 0;
  ch[y][0] = 0;
  update(y);
}
inline void Link(int x, int y) {
  chroot(x);
  if (findroot(y) != x) fa[x] = y;
}
inline void ud(int x) { mx[x] = make_pair(val[x], x); }
}  // namespace LCT
namespace Seg {
int tag[MAXN << 2];
pair<int, int> mn[MAXN << 2];
inline void tage_lazy(int rt) {
  if (tag[rt]) {
    tag[rt << 1] += tag[rt];
    tag[rt << 1 | 1] += tag[rt];
    mn[rt << 1].first += tag[rt];
    mn[rt << 1 | 1].first += tag[rt];
    tag[rt] = 0;
  }
}
inline pair<int, int> mg(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first) return make_pair(a.first, a.second + b.second);
  if (a.first < b.first) return a;
  return b;
}
inline void update(int rt) { mn[rt] = mg(mn[rt << 1], mn[rt << 1 | 1]); }
inline void build(int rt, int l, int r) {
  if (l == r) {
    mn[rt] = make_pair(l, 1);
    return;
  }
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  update(rt);
}
inline void Add(int rt, int l, int r, int L, int R, int x) {
  if (L <= l && R >= r) {
    tag[rt] += x;
    mn[rt].first += x;
    return;
  }
  int mid = (l + r) >> 1;
  tage_lazy(rt);
  if (L <= mid) Add(rt << 1, l, mid, L, R, x);
  if (R > mid) Add(rt << 1 | 1, mid + 1, r, L, R, x);
  update(rt);
}
inline void Query(int rt, int l, int r, int L, int R) {
  if (L <= l && R >= r) {
    mn[0] = mg(mn[0], mn[rt]);
    return;
  }
  int mid = (l + r) >> 1;
  tage_lazy(rt);
  if (L <= mid) Query(rt << 1, l, mid, L, R);
  if (R > mid) Query(rt << 1 | 1, mid + 1, r, L, R);
  update(rt);
  return;
}
}  // namespace Seg
pair<int, int> pos[MAXM + 1];
using namespace LCT;
using namespace Seg;
int Beg[MAXM + 1];
int U[MAXM * 4 + 1], V[MAXM * 4 + 1];
int dis[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<int> vec[MAXM + 1];
vector<int> Vx;
int main() {
  N = read(), M = read();
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      a[(i - 1) * M + j] = read();
      pos[a[(i - 1) * M + j]] = make_pair(i, j);
    }
  }
  int cnte = 0;
  for (int i = 1; i <= N * M; i++) val[i] = -INF, ud(i);
  Beg[N * M + 1] = INF;
  for (int i = N * M; i >= 1; i--) {
    int x = pos[i].first, y = pos[i].second;
    Beg[i] = N * M + 1;
    for (int j = 0; j < 4; j++) {
      int xx = x + dis[j][0];
      int yy = y + dis[j][1];
      if (xx < 1 || yy < 1 || xx > N || yy > M) continue;
      if (a[(xx - 1) * M + yy] < i) continue;
      vec[i + 1].push_back(a[(xx - 1) * M + yy]);
      Vx.push_back(a[(xx - 1) * M + yy]);
      if (findroot((xx - 1) * M + yy) == findroot((x - 1) * M + y)) {
        split((xx - 1) * M + yy, (x - 1) * M + y);
        pair<int, int> ret = mx[(x - 1) * M + y];
        Beg[i] = min(max(ret.first, a[(xx - 1) * M + yy]), Beg[i]);
        int cutnod = ret.second - N * M;
        if (ret.first < a[(xx - 1) * M + yy]) continue;
        Cut(U[cutnod], ret.second);
        Cut(V[cutnod], ret.second);
      }
      U[++cnte] = (xx - 1) * M + yy;
      V[cnte] = (x - 1) * M + y;
      val[cnte + N * M] = a[(xx - 1) * M + yy];
      ud(cnte + N * M);
      Link(cnte + N * M, (x - 1) * M + y);
      Link(cnte + N * M, (xx - 1) * M + yy);
    }
    Beg[i] = min(Beg[i], Beg[i + 1]);
  }
  build(1, 1, N * M);
  long long ans = 0;
  for (int i = 0; i < Vx.size(); i++) Add(1, 1, N * M, Vx[i], N * M, -1);
  for (int i = 1; i <= N * M; i++) {
    if (i > 1) Add(1, 1, N * M, i - 1, N * M, -1);
    for (int j = 0; j < vec[i].size(); j++)
      Add(1, 1, N * M, vec[i][j], N * M, 1);
    mn[0] = make_pair(INF, 0);
    if (i <= Beg[i] - 1) Query(1, 1, N * M, i, Beg[i] - 1);
    if (mn[0].first == 1) ans += mn[0].second;
  }
  cout << ans << endl;
  return 0;
}