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 ll = long long;
namespace LCT {
int lson[200005], rson[200005], fa[200005], stk[200005];
bool rev[200005];
inline bool isrt(int u) { return lson[fa[u]] != u && rson[fa[u]] != u; }
inline void push_down(int u) {
  if (!u || !rev[u]) return;
  std::swap(lson[u], rson[u]);
  rev[lson[u]] ^= 1;
  rev[rson[u]] ^= 1;
  rev[u] = false;
}
inline void rotate(int u) {
  int v = fa[u];
  int *same = u == lson[v] ? lson : rson, *ano = u == lson[v] ? rson : lson;
  fa[u] = fa[v];
  if (!isrt(v)) (lson[fa[v]] == v ? lson : rson)[fa[v]] = u;
  same[v] = ano[u];
  fa[ano[u]] = v;
  ano[u] = v;
  fa[v] = u;
}
inline void splay(int u) {
  int tp = 0;
  stk[tp++] = u;
  for (int i = u; !isrt(i); i = fa[i]) stk[tp++] = fa[i];
  while (tp--) push_down(stk[tp]);
  while (!isrt(u)) {
    int v = fa[u];
    if (!isrt(v)) {
      if ((lson[fa[v]] == v) ^ (lson[v] == u))
        rotate(u);
      else
        rotate(v);
    }
    rotate(u);
  }
}
inline void access(int u) {
  for (int v = 0; u; v = u, u = fa[u]) {
    splay(u);
    rson[u] = v;
  }
}
inline int getfa(int u) {
  access(u);
  splay(u);
  while (true) {
    push_down(u);
    if (!lson[u]) break;
    u = lson[u];
  }
  splay(u);
  return u;
}
inline void mkrt(int u) {
  access(u);
  splay(u);
  rev[u] ^= 1;
}
inline void link(int u, int v) {
  mkrt(u);
  fa[u] = v;
}
inline void cut(int u, int v) {
  mkrt(u);
  access(v);
  splay(v);
  lson[v] = fa[u] = 0;
}
}  // namespace LCT
namespace segT {
struct data {
  int val, cnt;
  inline data operator+(const data &a) const {
    if (val == a.val) return {val, cnt + a.cnt};
    return val < a.val ? *this : a;
  }
} seg[800005];
int lazy[800005];
inline void push_up(int u) { seg[u] = seg[u << 1] + seg[u << 1 | 1]; }
inline void paint(int u, int x) {
  lazy[u] += x;
  seg[u].val += x;
}
inline void push_down(int u) {
  if (lazy[u]) {
    paint(u << 1, lazy[u]);
    paint(u << 1 | 1, lazy[u]);
    lazy[u] = 0;
  }
}
void build(int u, int l, int r) {
  if (l == r) {
    seg[u] = {l, 1};
    return;
  }
  int m = l + r >> 1;
  build(u << 1, l, m);
  build(u << 1 | 1, m + 1, r);
  push_up(u);
}
void modify(int u, int l, int r, int L, int R, int val) {
  if (L <= l && r <= R) {
    paint(u, val);
    return;
  }
  push_down(u);
  int m = l + r >> 1;
  if (L <= m) modify(u << 1, l, m, L, R, val);
  if (m < R) modify(u << 1 | 1, m + 1, r, L, R, val);
  push_up(u);
}
data query(int u, int l, int r, int L, int R) {
  if (L <= l && r <= R) return seg[u];
  push_down(u);
  int m = l + r >> 1;
  data res = {(int)1e9, 1};
  if (L <= m) res = res + query(u << 1, l, m, L, R);
  if (m < R) res = res + query(u << 1 | 1, m + 1, r, L, R);
  return res;
}
}  // namespace segT
constexpr int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int arr[1005][1005], pos_x[200005], pos_y[200005], lim[200005], bel[5], n, m;
template <typename T>
void for_adj(int u, const T &func) {
  int x = pos_x[u], y = pos_y[u];
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (xx >= 0 && xx < n && yy >= 0 && yy < m) func(arr[xx][yy]);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", arr[i] + j);
      pos_x[--arr[i][j]] = i;
      pos_y[arr[i][j]] = j;
    }
  }
  for (int l = 0, r = 0; l < n * m; l++) {
    while (r < n * m) {
      int cnt = 0;
      for_adj(r, [&](int u) {
        if (u >= l && u < r) bel[cnt++] = LCT::getfa(u + 1) - 1;
      });
      bool flg = false;
      for (int i = 0; i < cnt; i++) {
        for (int j = 0; j < i; j++) flg |= bel[i] == bel[j];
      }
      if (flg) break;
      for_adj(r, [&](int u) {
        if (u >= l && u < r) LCT::link(r + 1, u + 1);
      });
      r++;
    }
    lim[l] = r - 1;
    for_adj(l, [&](int u) {
      if (u >= l && u < r) LCT::cut(l + 1, u + 1);
    });
  }
  segT::build(1, 0, n * m - 1);
  ll ans = 0;
  for (int r = n * m - 1; r >= 0; r--) {
    for_adj(r, [&](int u) {
      if (u >= r) segT::modify(1, 0, n * m - 1, u, n * m - 1, -1);
    });
    auto res = segT::query(1, 0, n * m - 1, r, lim[r]);
    if (res.val == r) ans += res.cnt;
  }
  printf("%lld\n", ans);
  return 0;
}