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;
const int N = 1001, M = 1001, K = 200001;
int f[N][M], n, m;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
vector<int> graph[K];
int root[K], child[K][2], father[K], rev[K];
void cover(int u) { rev[u] ^= 1; }
void pushdown(int u) {
  if (rev[u]) {
    swap(child[u][0], child[u][1]);
    if (child[u][0]) cover(child[u][0]);
    if (child[u][1]) cover(child[u][1]);
    rev[u] = 0;
  }
}
void rotate(int u, int c) {
  int fu = father[u], cu = child[u][c ^ 1];
  if (root[fu])
    root[fu] = false, root[u] = true, father[u] = father[fu];
  else {
    int gu = father[fu], t = (child[gu][1] == fu);
    child[gu][t] = u, father[u] = gu;
  }
  child[fu][c] = cu, father[cu] = fu;
  child[u][c ^ 1] = fu, father[fu] = u;
}
int s[N], top = 0;
void splay(int u) {
  for (int v = u;; v = father[v]) {
    s[++top] = v;
    if (root[v]) break;
  }
  for (; top; top--) pushdown(s[top]);
  while (!root[u]) {
    int fu = father[u], c = (child[fu][1] == u);
    if (root[fu])
      rotate(u, c);
    else {
      int gu = father[fu], c_ = (child[gu][1] == fu);
      if (c ^ c_)
        rotate(u, c), rotate(u, c_);
      else
        rotate(fu, c_), rotate(u, c);
    }
  }
}
void access(int u) {
  int now = 0;
  while (u) {
    splay(u);
    if (child[u][1]) root[child[u][1]] = true;
    if (now) root[now] = false;
    child[u][1] = now, now = u, u = father[u];
  }
}
int leftmost(int u) {
  int v = u;
  while (true) {
    pushdown(v);
    if (child[v][0])
      v = child[v][0];
    else
      break;
  }
  return splay(v), v;
}
int findroot(int u) { return access(u), splay(u), leftmost(u); }
void changeroot(int u) { access(u), splay(u), cover(u); }
void link(int u, int v) { changeroot(u), father[u] = v; }
void cut(int u, int v) {
  changeroot(u), access(v), splay(v);
  root[u] = root[v] = true, child[v][0] = 0, father[u] = 0;
}
int sgt_cnt[K << 2], sgt_mn[K << 2], tag[K << 2];
void build(int l, int r, int now) {
  int mid = l + r >> 1;
  sgt_mn[now] = 0, tag[now] = 0;
  if (l < r) {
    build(l, mid, now << 1), build(mid + 1, r, now << 1 | 1);
    sgt_cnt[now] = sgt_cnt[now << 1] + sgt_cnt[now << 1 | 1];
  } else
    sgt_cnt[now] = 1;
}
void pushup(int now) {
  sgt_mn[now] = min(sgt_mn[now << 1], sgt_mn[now << 1 | 1]) + tag[now];
  sgt_cnt[now] = 0;
  if (sgt_mn[now] == sgt_mn[now << 1] + tag[now])
    sgt_cnt[now] += sgt_cnt[now << 1];
  if (sgt_mn[now] == sgt_mn[now << 1 | 1] + tag[now])
    sgt_cnt[now] += sgt_cnt[now << 1 | 1];
}
void change(int left, int right, int l, int r, int val, int now) {
  int mid = l + r >> 1;
  if (l == left && r == right)
    sgt_mn[now] += val, tag[now] += val;
  else {
    if (right <= mid)
      change(left, right, l, mid, val, now << 1);
    else if (left > mid)
      change(left, right, mid + 1, r, val, now << 1 | 1);
    else
      change(left, mid, l, mid, val, now << 1),
          change(mid + 1, right, mid + 1, r, val, now << 1 | 1);
    pushup(now);
  }
}
int query(int left, int right, int l, int r, int val, int now) {
  int mid = l + r >> 1;
  if (left > right) return 0;
  if (l == left && r == right)
    return sgt_mn[now] == val ? sgt_cnt[now] : 0;
  else if (right <= mid)
    return query(left, right, l, mid, val - tag[now], now << 1);
  else if (left > mid)
    return query(left, right, mid + 1, r, val - tag[now], now << 1 | 1);
  else
    return query(left, mid, l, mid, val - tag[now], now << 1) +
           query(mid + 1, right, mid + 1, r, val - tag[now], now << 1 | 1);
}
int l = 0, r = 0;
long long ans = 0ll;
bool check() {
  map<int, int> total;
  for (int i = 0; i < graph[r].size(); i++) {
    int to = graph[r][i];
    if (to <= r && to > l) total[findroot(to)]++;
  }
  for (map<int, int>::iterator it = total.begin(); it != total.end(); it++) {
    if ((*it).second > 1) return false;
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) scanf("%d", &f[i][j]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        int x = i + dx[k], y = j + dy[k];
        if (1 <= x && x <= n && 1 <= y && y <= m)
          graph[f[i][j]].push_back(f[x][y]);
      }
    }
  }
  for (int i = 1; i <= n * m; i++)
    root[i] = true, child[i][0] = child[i][1] = father[i] = 0;
  build(1, n * m, 1);
  for (r = 1; r <= n * m; r++) {
    change(1, r, 1, n * m, 1, 1);
    while (l < r && !check()) {
      l++;
      for (int j = 0; j < graph[l].size(); j++) {
        int to = graph[l][j];
        if (to >= l && to < r) cut(l, to), change(1, l, 1, n * m, 1, 1);
      }
    }
    for (int j = 0; j < graph[r].size(); j++) {
      int to = graph[r][j];
      if (to <= r && to > l) link(r, to), change(1, to, 1, n * m, -1, 1);
    }
    ans += query(l + 1, r, 1, n * m, 1, 1);
  }
  printf("%lld\n", ans);
  return 0;
}