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;
struct LCT {
  int ch[1000000][3], fa[1000000], turn[1000000];
  int Get(int x) { return ch[fa[x]][1] == x; }
  int nroot(int x) { return ch[fa[x]][1] == x || ch[fa[x]][0] == x; }
  void pushr(int x) {
    if (!x) return;
    swap(ch[x][0], ch[x][1]);
    turn[x] ^= 1;
  }
  void pushdown(int x) {
    if (turn[x]) {
      pushr(ch[x][0]), pushr(ch[x][1]);
      turn[x] = 0;
    }
  }
  void pushall(int x) {
    if (nroot(x)) pushall(fa[x]);
    pushdown(x);
  }
  void rotate(int x) {
    int y = fa[x], z = fa[y], w = Get(x);
    if (nroot(y)) ch[z][ch[z][1] == y] = x;
    ch[y][w] = ch[x][w ^ 1];
    if (ch[y][w]) fa[ch[y][w]] = y;
    ch[x][w ^ 1] = y;
    fa[y] = x;
    fa[x] = z;
  }
  void splay(int x) {
    pushall(x);
    while (nroot(x)) {
      int y = fa[x];
      if (nroot(y)) Get(x) ^ Get(y) ? rotate(x) : rotate(y);
      rotate(x);
    }
  }
  void access(int x) {
    for (int y = 0; x; x = fa[y = x]) {
      splay(x);
      ch[x][1] = y;
    }
  }
  int findroot(int x) {
    access(x);
    splay(x);
    while (ch[x][0]) x = ch[x][0];
    splay(x);
    return x;
  }
  void makeroot(int x) {
    access(x);
    splay(x);
    pushr(x);
    return;
  }
  int link(int x, int y) {
    makeroot(x);
    if (findroot(y) == x) return false;
    fa[x] = y;
    return true;
  }
  void cut(int x, int y) {
    makeroot(x);
    if (findroot(y) == x && fa[y] == x && !ch[y][0]) {
      fa[y] = ch[x][1] = 0;
    }
    return;
  }
} T;
struct segment_tree {
  int Min[4 * 1000000], Num[4 * 1000000], lazy[4 * 1000000];
  void pushdown(int x) {
    if (lazy[x]) {
      lazy[x << 1] += lazy[x];
      lazy[x << 1 | 1] += lazy[x];
      Min[x << 1] += lazy[x], Min[x << 1 | 1] += lazy[x];
      lazy[x] = 0;
    }
  }
  void pushup(int x) {
    if (Min[x << 1] < Min[x << 1 | 1])
      Min[x] = Min[x << 1], Num[x] = Num[x << 1];
    if (Min[x << 1] > Min[x << 1 | 1])
      Min[x] = Min[x << 1 | 1], Num[x] = Num[x << 1 | 1];
    if (Min[x << 1] == Min[x << 1 | 1])
      Min[x] = Min[x << 1], Num[x] = Num[x << 1] + Num[x << 1 | 1];
  }
  void build(int x, int l, int r) {
    if (l == r) {
      Min[x] = 0, Num[x] = 1;
      return;
    }
    int mid = l + r >> 1;
    build(x << 1, l, mid);
    build(x << 1 | 1, mid + 1, r);
    pushup(x);
  }
  void modify(int x, int l, int r, int ll, int rr, int k) {
    if (ll <= l && r <= rr) {
      lazy[x] += k;
      Min[x] += k;
      return;
    }
    pushdown(x);
    int mid = l + r >> 1;
    if (ll <= mid) modify(x << 1, l, mid, ll, rr, k);
    if (rr > mid) modify(x << 1 | 1, mid + 1, r, ll, rr, k);
    pushup(x);
  }
  int query(int x, int l, int r, int ll, int rr) {
    if (l == ll && r == rr) {
      if (Min[x] == 1)
        return Num[x];
      else
        return 0;
    }
    int mid = l + r >> 1;
    pushdown(x);
    if (rr <= mid)
      return query(x << 1, l, mid, ll, rr);
    else if (ll > mid)
      return query(x << 1 | 1, mid + 1, r, ll, rr);
    else
      return query(x << 1, l, mid, ll, mid) +
             query(x << 1 | 1, mid + 1, r, mid + 1, rr);
  }
} S;
vector<int> V[1000000];
int dx[10] = {1, 0, -1, 0};
int dy[10] = {0, 1, 0, -1};
int n, m, w[3000][3000];
long long ans = 0;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &w[i][j]);
  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 (x < 1 || y < 1 || x > n || y > m) continue;
        if (w[x][y] > w[i][j]) continue;
        V[w[x][y]].push_back(w[i][j]);
        V[w[i][j]].push_back(w[x][y]);
      }
    }
  }
  int R = 0;
  long long t = n * m;
  S.build(1, 1, t);
  for (int i = 1; i <= t; i++) {
    for (int j = R + 1; j <= t; j++) {
      int now = j, flag = 0;
      for (int d = 0; d < V[now].size(); d++) {
        int now1 = V[now][d];
        if (now1 < now && now1 >= i)
          if (!T.link(now1, now)) {
            flag = 1;
            break;
          }
      }
      for (int d = 0; d < V[now].size(); d++) T.cut(now, V[now][d]);
      if (flag == 1) break;
      R = j;
      int tot = 0;
      for (int d = 0; d < V[now].size(); d++) {
        int now1 = V[now][d];
        if (now1 < j && now1 >= i) T.link(now1, now);
        if (now1 < j && now1 >= i) tot++;
      }
      S.modify(1, 1, t, R, R, R - i + 1);
      S.modify(1, 1, t, R, t, -tot);
    }
    ans += 1ll * S.query(1, 1, t, i, R);
    int tot = 0;
    for (int d = 0; d < V[i].size(); d++) {
      if (V[i][d] <= R && V[i][d] > i) {
        T.cut(V[i][d], i);
        S.modify(1, 1, t, V[i][d], t, 1);
      }
    }
    S.modify(1, 1, t, i, R, -1);
  }
  printf("%lld\n", ans);
  return 0;
}