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 inf = 0x3f3f3f3f;
int n, m, a[2005][2005], pos[250005], dx[] = {0, 0, 1, -1},
                                      dy[] = {1, -1, 0, 0};
long long ans = 0;
vector<pair<int, int> > e[200005];
int read() {
  int f = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    f = f * 10 + ch - '0';
    ch = getchar();
  }
  return f * w;
}
bool inmap(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m) return 0;
  return 1;
}
int ch[200005][2], rev[200005], fa[200005];
int chk(int x) { return ch[fa[x]][1] == x; }
int isroot(int x) { return (ch[fa[x]][0] != x) && (ch[fa[x]][1] != x); }
void pushdown(int x) {
  if (rev[x]) {
    rev[ch[x][0]] ^= 1;
    rev[ch[x][1]] ^= 1;
    swap(ch[x][0], ch[x][1]);
    rev[x] = 0;
  }
}
void pd(int x) {
  if (!isroot(x)) pd(fa[x]);
  pushdown(x);
}
void rotate(int x) {
  int y = fa[x], z = fa[y], t = chk(x);
  if (!isroot(y)) ch[z][chk(y)] = x;
  fa[x] = z;
  ch[y][t] = ch[x][t ^ 1];
  fa[ch[x][t ^ 1]] = y;
  ch[x][t ^ 1] = y;
  fa[y] = x;
}
void splay(int x) {
  pd(x);
  while (!isroot(x)) {
    int y = fa[x];
    if (!isroot(y)) rotate(chk(y) == chk(x) ? y : x);
    rotate(x);
  }
}
void access(int x) {
  for (int y = 0; x; y = x, x = fa[x]) {
    splay(x);
    ch[x][1] = y;
  }
}
void makeroot(int x) {
  access(x);
  splay(x);
  rev[x] ^= 1;
}
int findroot(int x) {
  access(x);
  splay(x);
  while (ch[x][0]) x = ch[x][0];
  return x;
}
void link(int x, int y) {
  makeroot(x);
  if (findroot(y) != x) fa[x] = y;
}
void split(int x, int y) {
  makeroot(x);
  access(y);
  splay(y);
}
void cut(int x, int y) {
  split(x, y);
  if (ch[y][0] == x) ch[y][0] = 0, fa[x] = 0;
}
struct node {
  int minn, cnt, lazy;
} tree[800005];
void pushup(int tr) {
  if (tree[tr * 2].minn > tree[tr * 2 + 1].minn) {
    tree[tr].minn = tree[tr * 2 + 1].minn;
    tree[tr].cnt = tree[tr * 2 + 1].cnt;
  } else if (tree[tr * 2].minn < tree[tr * 2 + 1].minn) {
    tree[tr].minn = tree[tr * 2].minn;
    tree[tr].cnt = tree[tr * 2].cnt;
  } else {
    tree[tr].minn = tree[tr * 2].minn;
    tree[tr].cnt = tree[tr * 2].cnt + tree[tr * 2 + 1].cnt;
  }
}
void pushdown1(int tr) {
  tree[tr * 2].lazy += tree[tr].lazy;
  tree[tr * 2 + 1].lazy += tree[tr].lazy;
  tree[tr * 2].minn += tree[tr].lazy;
  tree[tr * 2 + 1].minn += tree[tr].lazy;
  tree[tr].lazy = 0;
}
void build(int tr, int l, int r) {
  if (l == r) {
    tree[tr].minn = 0;
    tree[tr].cnt = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(tr * 2, l, mid), build(tr * 2 + 1, mid + 1, r);
  pushup(tr);
}
pair<int, int> query(int tr, int stdl, int stdr, int l, int r) {
  if (l > stdr || r < stdl) return make_pair(inf, 0);
  if (l <= stdl && r >= stdr) return make_pair(tree[tr].minn, tree[tr].cnt);
  pushdown1(tr);
  int mid = (stdl + stdr) / 2;
  pair<int, int> p1 = query(tr * 2, stdl, mid, l, r),
                 p2 = query(tr * 2 + 1, mid + 1, stdr, l, r);
  if (p1.first > p2.first)
    return p2;
  else if (p1.first < p2.first)
    return p1;
  else
    return make_pair(p1.first, p1.second + p2.second);
}
void modify(int tr, int stdl, int stdr, int l, int r, int k) {
  if (l > stdr || r < stdl) return;
  if (l <= stdl && r >= stdr) {
    tree[tr].minn += k;
    tree[tr].lazy += k;
    return;
  }
  pushdown1(tr);
  int mid = (stdl + stdr) / 2;
  modify(tr * 2, stdl, mid, l, r, k),
      modify(tr * 2 + 1, mid + 1, stdr, l, r, k);
  pushup(tr);
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = read(), pos[a[i][j]] = (i - 1) * m + 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];
        e[(i - 1) * m + j].push_back(make_pair((x - 1) * m + y, a[x][y]));
      }
  int hd = 1, tl = 1;
  build(1, 1, n * m);
  modify(1, 1, n * m, 1, n * m, 1);
  while (hd <= n * m) {
    while (tl < n * m) {
      int cnt = 0;
      for (int i = 0; i < e[pos[tl + 1]].size(); i++)
        if (e[pos[tl + 1]][i].second >= hd && e[pos[tl + 1]][i].second <= tl)
          cnt++;
      if (!cnt) {
        modify(1, 1, n * m, ++tl, n * m, 1);
        continue;
      } else {
        int p = 0, flag = 0;
        for (int i = 0; i < e[pos[tl + 1]].size(); i++, p++) {
          if (e[pos[tl + 1]][i].second >= hd &&
              e[pos[tl + 1]][i].second <= tl) {
            if (findroot(e[pos[tl + 1]][i].first) == findroot(pos[tl + 1])) {
              flag = 1;
              break;
            } else
              link(e[pos[tl + 1]][i].first, pos[tl + 1]);
          }
        }
        if (flag) {
          for (int i = p - 1; i >= 0; i--)
            if (e[pos[tl + 1]][i].second >= hd &&
                e[pos[tl + 1]][i].second <= tl)
              cut(e[pos[tl + 1]][i].first, pos[tl + 1]);
          break;
        }
        modify(1, 1, n * m, ++tl, n * m, 1 - cnt);
      }
    }
    pair<int, int> now = query(1, 1, n * m, hd, tl);
    if (now.first == 1) ans += now.second;
    vector<pair<int, int> > tmp;
    for (int i = 0; i < e[pos[hd]].size(); i++)
      if (e[pos[hd]][i].second >= hd && e[pos[hd]][i].second <= tl) {
        tmp.push_back(make_pair(e[pos[hd]][i].second, e[pos[hd]][i].first));
        cut(pos[hd], e[pos[hd]][i].first);
      }
    if (!tmp.size())
      modify(1, 1, n * m, hd + 1, n * m, -1);
    else {
      sort(tmp.begin(), tmp.end());
      for (int i = 0; i < tmp.size(); i++) {
        int l, r;
        if (!i)
          l = hd + 1;
        else
          l = tmp[i - 1].first;
        r = tmp[i].first - 1;
        modify(1, 1, n * m, l, r, i - 1);
      }
      modify(1, 1, n * m, tmp[tmp.size() - 1].first, n * m, tmp.size() - 1);
    }
    hd++;
  }
  printf("%lld\n", ans);
  return 0;
}