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>
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = false;
  while (!isdigit(c)) {
    if (c == '-') flag = true;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
  if (flag) x = -x;
}
using namespace std;
const int inf = 987654321;
inline void MIN(int &a, int b) {
  if (b < a) a = b;
}
inline void MAX(int &a, int b) {
  if (a < b) a = b;
}
int n, m, nn;
int h[1010][1010], X[201000], Y[201000];
bool vis[201000];
namespace Seg {
struct Data {
  int ls, rs, mx, mxcnt, tag;
  Data() { ls = rs = mxcnt = tag = mx = 0; }
} dat[201000 << 2];
int ttot;
inline void pushup(int cur) {
  if (dat[dat[cur].ls].mx == dat[dat[cur].rs].mx) {
    dat[cur].mx = dat[dat[cur].ls].mx;
    dat[cur].mxcnt = dat[dat[cur].ls].mxcnt + dat[dat[cur].rs].mxcnt;
  } else if (dat[dat[cur].ls].mx > dat[dat[cur].rs].mx) {
    dat[cur].mx = dat[dat[cur].ls].mx, dat[cur].mxcnt = dat[dat[cur].ls].mxcnt;
  } else {
    dat[cur].mx = dat[dat[cur].rs].mx, dat[cur].mxcnt = dat[dat[cur].rs].mxcnt;
  }
}
inline void pusht(int cur, int v) {
  if (!cur) return;
  dat[cur].mx += v;
  dat[cur].tag += v;
}
inline void pushdown(int cur) {
  if (dat[cur].tag)
    pusht(dat[cur].ls, dat[cur].tag), pusht(dat[cur].rs, dat[cur].tag),
        dat[cur].tag = 0;
}
void ins(int L, int R, int pos, int v, int &cur) {
  if (!cur) cur = ++ttot;
  if (L == R) return dat[cur].mx = v, dat[cur].mxcnt = 1, void();
  int mid = (L + R) >> 1;
  pushdown(cur);
  if (pos <= mid)
    ins(L, mid, pos, v, dat[cur].ls);
  else
    ins(mid + 1, R, pos, v, dat[cur].rs);
  pushup(cur);
}
void add(int L, int R, int l, int r, int v, int cur) {
  if (!cur) return;
  if (l <= L && R <= r) return pusht(cur, v), void();
  int mid = (L + R) >> 1;
  pushdown(cur);
  if (l <= mid) add(L, mid, l, r, v, dat[cur].ls);
  if (r > mid) add(mid + 1, R, l, r, v, dat[cur].rs);
  pushup(cur);
}
void del(int L, int R, int pos, int cur) {
  if (L == R) return dat[cur].mx = -inf, void();
  int mid = (L + R) >> 1;
  if (pos <= mid)
    del(L, mid, pos, dat[cur].ls);
  else
    del(mid + 1, R, pos, dat[cur].rs);
  pushup(cur);
}
inline int query(int v) {
  if (dat[1].mx != v) return 0;
  return dat[1].mxcnt;
}
}  // namespace Seg
inline void calc_edge(int u, int v, int t) {
  Seg::add(1, nn, 1, min(u, v), t, 1);
}
int son[201000][2], fa[201000], tag[201000], mn[201000];
inline void pushup(int cur) {
  mn[cur] = cur;
  if (son[cur][0]) MIN(mn[cur], mn[son[cur][0]]);
  if (son[cur][1]) MIN(mn[cur], mn[son[cur][1]]);
}
inline bool notroot(int cur) {
  return son[fa[cur]][0] == cur || son[fa[cur]][1] == cur;
}
inline bool get_which(int cur) { return son[fa[cur]][1] == cur; }
inline void rotate(int cur) {
  int faa = fa[cur], fafa = fa[faa];
  bool flag = get_which(cur);
  fa[cur] = fafa;
  if (notroot(faa)) son[fafa][get_which(faa)] = cur;
  son[faa][flag] = son[cur][flag ^ 1];
  if (son[cur][flag ^ 1]) fa[son[cur][flag ^ 1]] = faa;
  fa[faa] = cur;
  son[cur][flag ^ 1] = faa;
  pushup(faa);
}
inline void pusht(int cur) {
  if (!cur) return;
  swap(son[cur][0], son[cur][1]);
  tag[cur] ^= 1;
}
inline void pushdown(int cur) {
  if (tag[cur]) pusht(son[cur][0]), pusht(son[cur][1]), tag[cur] = 0;
}
int stk[201000], stop;
inline void splay(int cur) {
  int x = cur;
  while (notroot(x)) stk[++stop] = x, x = fa[x];
  pushdown(x);
  while (stop) pushdown(stk[stop--]);
  for (int faa = fa[cur]; notroot(cur); rotate(cur), faa = fa[cur])
    if (notroot(faa)) rotate(get_which(cur) == get_which(faa) ? faa : cur);
  pushup(cur);
}
inline void Access(int x) {
  for (int lst = 0; x; lst = x, x = fa[x]) splay(x), son[x][1] = lst, pushup(x);
}
inline void make_root(int x) { Access(x), splay(x), pusht(x); }
inline int find_root(int x) {
  Access(x);
  splay(x);
  while (son[x][0]) x = son[x][0];
  splay(x);
  return x;
}
inline void Split(int x, int y) { make_root(x), Access(y), splay(y); }
inline void Link(int x, int y) {
  make_root(x), make_root(y);
  fa[x] = y;
}
inline void Cut(int x, int y) {
  make_root(x), Access(y), splay(y);
  fa[x] = son[y][0] = 0;
  pushup(y);
}
int jzptmp;
int ptr;
inline void Erase(int p) {
  int x = X[p], y = Y[p];
  if (vis[h[x][y - 1]] && h[x][y - 1] != jzptmp)
    Cut(p, h[x][y - 1]), calc_edge(p, h[x][y - 1], -1);
  if (vis[h[x][y + 1]] && h[x][y + 1] != jzptmp)
    Cut(p, h[x][y + 1]), calc_edge(p, h[x][y + 1], -1);
  if (vis[h[x - 1][y]] && h[x - 1][y] != jzptmp)
    Cut(p, h[x - 1][y]), calc_edge(p, h[x - 1][y], -1);
  if (vis[h[x + 1][y]] && h[x + 1][y] != jzptmp)
    Cut(p, h[x + 1][y]), calc_edge(p, h[x + 1][y], -1);
  vis[p] = false;
  Seg::del(1, nn, p, 1);
}
inline void Addedge(int x, int y) {
  if (find_root(x) != find_root(y)) {
    Link(x, y);
    calc_edge(x, y, 1);
    return;
  }
  Split(x, y);
  int mnid = mn[y];
  jzptmp = x;
  while (ptr < mnid) ++ptr, Erase(ptr);
  if (vis[y]) Link(x, y), calc_edge(x, y, 1);
}
int main() {
  read(n), read(m);
  nn = n * m;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      read(h[i][j]);
      X[h[i][j]] = i;
      Y[h[i][j]] = j;
    }
  }
  long long ans = 0;
  int jzproot = 0;
  for (int i = 1; i <= nn; ++i) {
    int x = X[i], y = Y[i];
    vis[i] = true;
    Seg::ins(1, nn, i, i, jzproot);
    if (vis[h[x - 1][y]]) Addedge(i, h[x - 1][y]);
    if (vis[h[x + 1][y]]) Addedge(i, h[x + 1][y]);
    if (vis[h[x][y - 1]]) Addedge(i, h[x][y - 1]);
    if (vis[h[x][y + 1]]) Addedge(i, h[x][y + 1]);
    int res = Seg::query(i);
    ans += res;
  }
  printf("%lld\n", ans);
  return 0;
}
