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;
int n, m, a[1010][1010], posx[200010], posy[200010], px[4] = {1, 0, -1, 0},
                                                     py[4] = {0, 1, 0, -1};
long long ans = 0;
struct pnt {
  pnt *f, *s[2];
  bool rev;
  pnt() : f(NULL), s(), rev(false) {}
} p[200010];
struct tree {
  int l, r, c, num, lz;
  tree *lc, *rc;
  tree(int l, int r)
      : l(l), r(r), c(0), num(r - l + 1), lz(0), lc(NULL), rc(NULL) {}
} * root;
inline bool is_root(pnt *x) {
  return !x->f || x->f->s[0] != x && x->f->s[1] != x;
}
void pushdown(pnt *x) {
  if (x->rev) {
    swap(x->s[0], x->s[1]);
    if (x->s[0]) x->s[0]->rev ^= 1;
    if (x->s[1]) x->s[1]->rev ^= 1;
    x->rev = 0;
  }
}
void rot(pnt *x) {
  pnt *f = x->f, *ff = f->f;
  int w = x == f->s[0];
  f->s[w ^ 1] = x->s[w];
  if (x->s[w]) x->s[w]->f = f;
  if (!is_root(f)) ff->s[f == ff->s[1]] = x;
  x->f = ff;
  x->s[w] = f;
  f->f = x;
}
void splay(pnt *x) {
  static stack<pnt *> sta;
  pnt *hh = x;
  while (!is_root(hh)) sta.push(hh), hh = hh->f;
  sta.push(hh);
  while (!sta.empty()) pushdown(sta.top()), sta.pop();
  while (!is_root(x)) {
    pnt *f = x->f, *ff = f->f;
    if (is_root(f))
      rot(x);
    else if ((x == f->s[0]) == (f == ff->s[0]))
      rot(f), rot(x);
    else
      rot(x), rot(x);
  }
}
void print(pnt *i) {
  printf("i: %d f: %d s[0]: %d s[1]: %d rev: %d\n", i - p, i->f ? i->f - p : 0,
         i->s[0] ? i->s[0] - p : 0, i->s[1] ? i->s[1] - p : 0, i->rev);
}
void access(pnt *x) {
  pnt *last = NULL;
  while (x) {
    splay(x);
    x->s[1] = last;
    last = x;
    x = x->f;
  }
}
pnt *get_root(pnt *x) {
  access(x);
  splay(x);
  while (x->s[0]) x = x->s[0];
  return x;
}
void make_root(pnt *x) {
  access(x);
  splay(x);
  x->rev ^= 1;
}
void link(pnt *x, pnt *y) {
  make_root(y);
  y->f = x;
}
void split(pnt *x, pnt *y) {
  make_root(x);
  access(y);
  splay(y);
}
void cut(pnt *x, pnt *y) {
  if (get_root(x) != get_root(y)) {
    return;
  }
  split(x, y);
  if (y->s[0] != x || x->s[1]) return;
  y->s[0] = NULL;
  x->f = NULL;
}
tree *bt(int l, int r) {
  tree *i = new tree(l, r);
  if (l < r) {
    int md = l + r >> 1;
    i->lc = bt(l, md);
    i->rc = bt(md + 1, r);
  }
  return i;
}
void pushdown(tree *i) {
  if (i->lz) {
    i->lc->lz += i->lz;
    i->lc->c += i->lz;
    i->rc->lz += i->lz;
    i->rc->c += i->lz;
    i->lz = 0;
  }
}
void pushup(tree *i) {
  if (i->lc->c < i->rc->c)
    i->c = i->lc->c, i->num = i->lc->num;
  else if (i->lc->c == i->rc->c)
    i->c = i->lc->c, i->num = i->lc->num + i->rc->num;
  else
    i->c = i->rc->c, i->num = i->rc->num;
}
void chg(tree *i, int l, int r, int c) {
  if (l > r) return;
  if (i->l == l && i->r == r) {
    i->lz += c;
    i->c += c;
    return;
  }
  pushdown(i);
  int md = i->l + i->r >> 1;
  if (r <= md)
    chg(i->lc, l, r, c);
  else if (l > md)
    chg(i->rc, l, r, c);
  else
    chg(i->lc, l, md, c), chg(i->rc, md + 1, r, c);
  pushup(i);
}
int get(tree *i, int l, int r) {
  if (i->l == l && i->r == r) return i->c == 1 ? i->num : 0;
  pushdown(i);
  int md = i->l + i->r >> 1;
  if (r <= md)
    return get(i->lc, l, r);
  else if (l > md)
    return get(i->rc, l, r);
  else
    return get(i->lc, l, md) + get(i->rc, md + 1, r);
}
void erase(int x) {
  for (int i = 0; i < 4; i++) {
    int xx = posx[x] + px[i], yy = posy[x] + py[i];
    if (xx > 0 && xx <= n && yy > 0 && yy <= m) {
      int y = a[xx][yy];
      cut(p + x, p + y);
    }
  }
}
void print(tree *i) {
  if (!i) return;
  printf("l: %d r: %d c: %d num: %d lz: %d\n", i->l, i->r, i->c, i->num, i->lz);
  print(i->lc);
  print(i->rc);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      posx[a[i][j]] = i;
      posy[a[i][j]] = j;
    }
  root = bt(1, n * m);
  for (int l = 1, r = 1; r <= n * m; r++) {
    for (int i = 0; i < 4; i++) {
      int x = posx[r] + px[i], y = posy[r] + py[i];
      if (x > 0 && x <= n && y > 0 && y <= m) {
        int u = a[x][y];
        if (u > r || u < l) continue;
        while (get_root(p + r) == get_root(p + u)) {
          erase(l++);
        }
        if (u >= l) {
          link(p + r, p + u);
          chg(root, l, u, -1);
        }
      }
    }
    chg(root, l, r, 1);
    ans += get(root, l, r);
  }
  printf("%lld\n", ans);
}