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>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
using namespace std;
namespace io {
const int L = (1 << 20) + 1;
char buf[L], *S, *T, c;
char getchar() {
  if (__builtin_expect(S == T, 0)) {
    T = (S = buf) + fread(buf, 1, L, stdin);
    return (S == T ? EOF : *S++);
  }
  return *S++;
}
int inp() {
  int x = 0, f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}
unsigned inpu() {
  unsigned x = 0;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    ;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x;
}
long long inp_ll() {
  long long x = 0;
  int f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}
char B[25], *outs = B + 20, *outr = B + 20;
template <class T>
inline void print(register T a, register char x = 0) {
  if (x) *--outs = x, x = 0;
  if (!a)
    *--outs = '0';
  else
    while (a) *--outs = (a % 10) + 48, a /= 10;
  if (x) *--outs = x;
  fwrite(outs, outr - outs, 1, stdout);
  outs = outr;
}
};  // namespace io
using io ::inp;
using io ::inp_ll;
using io ::inpu;
using io ::print;
using i32 = int;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
long long power(long long a, long long b, long long p) {
  if (!b) return 1;
  long long t = power(a, b / 2, p);
  t = t * t % p;
  if (b & 1) t = t * a % p;
  return t;
}
long long exgcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long px, py;
  long long d = exgcd(b, a % b, px, py);
  x = py;
  y = px - a / b * py;
  return d;
}
template <class T>
inline void freshmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <class T>
inline void freshmax(T &a, const T &b) {
  if (a < b) a = b;
}
const int MAXN = 200010;
const int MOD = 1000000009;
const f80 MI = f80(1) / MOD;
const int INF = 1000000000;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
struct node {
  node *pre, *son[2];
  int rev;
} nodes[MAXN];
node *nil = nodes;
struct LinkCutTree {
  void init(int n) {
    nil->pre = nil->son[0] = nil->son[1] = nil;
    nil->rev = 0;
    for (int i = 1; i <= n; ++i) {
      nodes[i].pre = nodes[i].son[0] = nodes[i].son[1] = nil;
      nodes[i].rev = 0;
    }
  }
  bool is_root(node *x) { return x->pre->son[0] != x && x->pre->son[1] != x; }
  void reverse(node *x) { x->rev ^= 1; }
  void clear(node *x) {
    if (x->rev) {
      reverse(x->son[0]);
      reverse(x->son[1]);
      swap(x->son[0], x->son[1]);
      x->rev = 0;
    }
  }
  void update(node *x) {}
  void rotate(node *x) {
    node *y = x->pre, *z = y->pre;
    int p = y->son[1] == x, q = p ^ 1;
    if (!is_root(y)) z->son[z->son[1] == y] = x;
    x->pre = z;
    y->pre = x;
    if (x->son[q]) x->son[q]->pre = y;
    y->son[p] = x->son[q];
    x->son[q] = y;
    update(y);
    update(x);
  }
  void splay(node *x) {
    list<node *> Q;
    node *it = x;
    for (; !is_root(it); it = it->pre) Q.push_back(it);
    Q.push_back(it);
    while (!Q.empty()) {
      node *it = Q.back();
      Q.pop_back();
      clear(it);
    }
    while (!is_root(x)) {
      node *y = x->pre, *z = y->pre;
      if (!is_root(y)) rotate((y->son[0] == x) ^ (z->son[0] == y) ? x : y);
      rotate(x);
    }
  }
  void access(node *x0) {
    for (node *x = x0, *y = nil; x != nil; x = x->pre) {
      splay(x);
      x->son[1] = y;
      y = x;
    }
    splay(x0);
  }
  node *belong(node *x) {
    access(x);
    for (; x->son[0] != nil; x = x->son[0])
      ;
    return x;
  }
  bool check(node *x, node *y) {
    node *fx = belong(x);
    node *fy = belong(y);
    return fx == fy;
  }
  void link(node *x, node *y) {
    access(x);
    reverse(x);
    x->pre = y;
  }
  void cut(node *x, node *y) {
    access(y);
    splay(x);
    if (x->pre == y)
      x->pre = nil;
    else {
      access(x);
      splay(y);
      if (y->pre == x) y->pre = nil;
    }
  }
  bool check(int x, int y) { return check(nodes + x, nodes + y); }
  void link(int x, int y) { link(nodes + x, nodes + y); }
  bool try_link(int x, int y) {
    if (check(x, y)) return 1;
    link(x, y);
    return 0;
  }
  void cut(int x, int y) { cut(nodes + x, nodes + y); }
} T;
int n, m;
int a[MAXN];
vector<int> v[MAXN];
int place(int i, int j) { return (i - 1) * m + j; }
struct segtree {
  int add, min, cnt;
} tree[MAXN * 4];
void clear(int k) {
  if (tree[k].add) {
    tree[k << 1].add += tree[k].add;
    tree[k << 1].min += tree[k].add;
    tree[k << 1 | 1].add += tree[k].add;
    tree[k << 1 | 1].min += tree[k].add;
    tree[k].add = 0;
  }
}
void update(int k) {
  tree[k].min = min(tree[k << 1].min, tree[k << 1 | 1].min);
  tree[k].cnt = (tree[k << 1].min == tree[k].min) * tree[k << 1].cnt +
                (tree[k << 1 | 1].min == tree[k].min) * tree[k << 1 | 1].cnt;
}
void build(int k, int L, int R) {
  if (L == R) {
    tree[k].add = 0;
    tree[k].min = 0;
    tree[k].cnt = 1;
    return;
  }
  int m = (L + R) / 2;
  build(k << 1, L, m);
  build(k << 1 | 1, m + 1, R);
  update(k);
}
void add(int k, int L, int R, int x, int y, int p) {
  if (L == x && R == y) {
    tree[k].add += p;
    tree[k].min += p;
    return;
  }
  clear(k);
  int m = (L + R) / 2;
  if (y <= m)
    add(k << 1, L, m, x, y, p);
  else if (x > m)
    add(k << 1 | 1, m + 1, R, x, y, p);
  else {
    add(k << 1, L, m, x, m, p);
    add(k << 1 | 1, m + 1, R, m + 1, y, p);
  }
  update(k);
}
pair<int, int> query(int k, int L, int R, int x, int y) {
  if (L == x && R == y) return pair<int, int>(tree[k].min, tree[k].cnt);
  clear(k);
  int m = (L + R) / 2;
  if (y <= m) return query(k << 1, L, m, x, y);
  if (x > m) return query(k << 1 | 1, m + 1, R, x, y);
  auto lans = query(k << 1, L, m, x, m);
  auto rans = query(k << 1 | 1, m + 1, R, m + 1, y);
  return pair<int, int>(min(lans.first, rans.first),
                        (lans.first <= rans.first) * lans.second +
                            (lans.first >= rans.first) * rans.second);
}
int main() {
  n = inp();
  m = inp();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) a[place(i, j)] = inp();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int d = 0; d < 4; ++d) {
        int x = i + dx[d], y = j + dy[d];
        if (1 <= x && x <= n && 1 <= y && y <= m)
          v[a[place(i, j)]].push_back(a[place(x, y)]);
      }
  long long ans = 0;
  T.init(n * m);
  build(1, 1, n * m);
  for (int i = 1, j = 1; i <= n * m; ++i) {
    while (1) {
      int flag = 0;
      for (auto x : v[i])
        if (j <= x && x < i) flag |= T.try_link(x, i);
      if (!flag) break;
      for (auto x : v[i])
        if (j <= x && x < i) T.cut(x, i);
      for (auto x : v[j]) T.cut(x, j);
      j++;
    }
    add(1, 1, n * m, j, i, 1);
    for (auto x : v[i])
      if (j <= x && x < i) add(1, 1, n * m, j, x, -1);
    auto ret = query(1, 1, n * m, j, i);
    if (ret.first == 1) ans += ret.second;
  }
  cout << ans << endl;
  return 0;
}