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;
namespace TYC {
const int M = 1005, N = 2e5 + 5;
const int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
int n, m, tot, ID[M][M];
pair<int, int> P[N];
inline int read() {
  int x = 0, f = 0, ch = getchar();
  while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
namespace LCT {
struct null_tag {};
struct node {
  static node *null;
  node *fa, *son[2];
  int rev;
  node(null_tag) : rev(0) { fa = son[0] = son[1] = this; }
  node() : rev(0) { fa = son[0] = son[1] = null; }
  int dir() { return fa->son[1] == this; }
  bool isroot() { return fa->son[0] != this && fa->son[1] != this; }
  void reverse() { swap(son[0], son[1]), rev ^= 1; }
  void pushdown() {
    if (rev) {
      if (son[0] != null) son[0]->reverse();
      if (son[1] != null) son[1]->reverse();
      rev = 0;
    }
  }
  void rotate() {
    node *f = fa;
    int d = dir();
    if (!f->isroot()) f->fa->son[f->dir()] = this;
    fa = f->fa;
    f->son[d] = son[d ^ 1];
    if (son[d ^ 1] != null) son[d ^ 1]->fa = f;
    f->fa = this;
    son[d ^ 1] = f;
  }
  void splay() {
    static node *sta[N];
    int top = 0;
    sta[top++] = this;
    for (node *t = this; !t->isroot(); t = t->fa) sta[top++] = t->fa;
    while (top--) sta[top]->pushdown();
    for (; !isroot(); rotate())
      if (!fa->isroot()) (fa->dir() ^ dir() ? this : fa)->rotate();
  }
} *node::null = new node(null_tag()), *Node[N];
inline void access(node *o) {
  for (node *t = node::null; o != node::null; t = o, o = o->fa) {
    o->splay();
    o->son[1] = t;
  }
}
inline void makeroot(node *o) {
  access(o);
  o->splay();
  o->reverse();
}
inline node *findroot(node *o) {
  access(o);
  o->splay();
  while (o->son[0] != node::null) o = o->son[0];
  o->splay();
  return o;
}
inline void split(node *x, node *y) {
  makeroot(x);
  access(y);
  y->splay();
}
inline bool link(node *x, node *y) {
  makeroot(x);
  return findroot(y) != x ? x->fa = y, true : false;
}
inline void cut(node *x, node *y) {
  split(x, y);
  if (y->son[0] != x || x->son[1] != node::null) return;
  y->son[0] = x->fa = node::null;
}
}  // namespace LCT
using namespace LCT;
namespace Tree {
struct node {
  int v, c, tag;
} tr[N << 2];
inline void update(node &o, const node &a, const node &b) {
  if (a.v < b.v)
    o.v = a.v, o.c = a.c;
  else if (a.v > b.v)
    o.v = b.v, o.c = b.c;
  else
    o.v = a.v, o.c = a.c + b.c;
  o.v += o.tag;
}
void build(const int o = 1, const int l = 1, const int r = tot) {
  if (l == r) return void(tr[o] = (node){1, 1, 0});
  int mid = (l + r) >> 1;
  build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r);
  update(tr[o], tr[o << 1], tr[o << 1 | 1]);
}
void add(const int s, const int e, const int v, const int o = 1,
         const int l = 1, const int r = tot) {
  if (s <= l && r <= e) {
    tr[o].v += v;
    tr[o].tag += v;
    return;
  }
  int mid = (l + r) >> 1;
  if (s <= mid) add(s, e, v, o << 1, l, mid);
  if (e > mid) add(s, e, v, o << 1 | 1, mid + 1, r);
  update(tr[o], tr[o << 1], tr[o << 1 | 1]);
}
node query(const int s, const int e, const int o = 1, const int l = 1,
           const int r = tot) {
  if (s <= l && r <= e) return tr[o];
  int mid = (l + r) >> 1;
  node t = (node){0, 0, 0};
  if (e <= mid)
    t = query(s, e, o << 1, l, mid);
  else if (s > mid)
    t = query(s, e, o << 1 | 1, mid + 1, r);
  else
    update(t, query(s, e, o << 1, l, mid), query(s, e, o << 1 | 1, mid + 1, r));
  return t.v += tr[o].tag, t;
}
}  // namespace Tree
void work() {
  n = read(), m = read(), tot = n * m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) P[ID[i][j] = read()] = pair<int, int>(i, j);
  for (int i = 1; i <= tot; i++) Node[i] = new node();
  Tree::build();
  long long ans = 0;
  for (int i = 1, j = 1; i <= tot; i++) {
    for (int k1 = 0, x1, y1, id1; k1 < 4; k1++) {
      id1 = ID[x1 = P[i].first + dx[k1]][y1 = P[i].second + dy[k1]];
      if (1 <= x1 && x1 <= n && 1 <= y1 && y1 <= m && j <= id1 && id1 <= i) {
        while (!link(Node[id1], Node[i])) {
          for (int k2 = 0, x2, y2, id2; k2 < 4; k2++) {
            id2 = ID[x2 = P[j].first + dx[k2]][y2 = P[j].second + dy[k2]];
            if (1 <= x2 && x2 <= n && 1 <= y2 && y2 <= m && j <= id2 &&
                id2 <= i)
              cut(Node[j], Node[id2]);
          }
          ++j;
        }
        Tree::add(1, id1, -1);
      }
    }
    Tree::node t = Tree::query(j, i);
    ans += t.v == 1 ? t.c : 0;
    Tree::add(j, i, 1);
  }
  printf("%I64d\n", ans);
}
}  // namespace TYC
int main() {
  TYC::work();
  return 0;
}