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 Point {
  int x, y;
  Point(){};
  Point(int a, int b) { x = a, y = b; }
} pos[200010];
struct LinkCutTree {
  struct Splay {
    int ch[2], fa;
    bool rev;
  } t[200010];
  int sta[200010];
  bool notroot(int u) { return t[t[u].fa].ch[0] == u || t[t[u].fa].ch[1] == u; }
  bool get(int u) { return t[t[u].fa].ch[1] == u; }
  void up(int u) {}
  void downn(int u) {
    swap(t[u].ch[0], t[u].ch[1]);
    t[u].rev ^= 1;
  }
  void down(int u) {
    if (t[u].rev) {
      downn(t[u].ch[0]), downn(t[u].ch[1]);
      t[u].rev = 0;
    }
  }
  void rotate(int u) {
    int fa = t[u].fa, gfa = t[fa].fa;
    bool d1 = get(u), d2 = get(fa);
    t[u].fa = gfa;
    if (notroot(fa)) t[gfa].ch[d2] = u;
    int son = t[u].ch[d1 ^ 1];
    t[u].ch[d1 ^ 1] = fa;
    t[fa].fa = u;
    t[fa].ch[d1] = son;
    t[son].fa = fa;
    up(fa);
  }
  void splay(int u) {
    int now = u, top = 0;
    sta[++top] = now;
    while (notroot(now)) sta[++top] = now = t[now].fa;
    while (top) down(sta[top]), top--;
    while (notroot(u)) {
      int fa = t[u].fa;
      if (notroot(fa)) {
        if (get(u) == get(fa))
          rotate(fa);
        else
          rotate(u);
      }
      rotate(u);
    }
    up(u);
  }
  void access(int u) {
    for (int v = 0; u; v = u, u = t[u].fa) {
      splay(u);
      t[u].ch[1] = v;
      up(u);
    }
  }
  void makeroot(int u) {
    access(u);
    splay(u);
    downn(u);
  }
  int findroot(int u) {
    access(u);
    splay(u);
    while (t[u].ch[0]) {
      down(u);
      u = t[u].ch[0];
    }
    splay(u);
    return u;
  }
  void link(int x, int y) {
    makeroot(x);
    t[x].fa = y;
  }
  void cut(int x, int y) {
    makeroot(x);
    findroot(y);
    t[x].ch[1] = t[y].fa = 0;
    up(x);
  }
  bool connected(int x, int y) {
    makeroot(x);
    return findroot(y) == x;
  }
} lct;
struct SegmentTree {
  struct data {
    int val, num;
    data(){};
    data(int a, int b) { val = a, num = b; }
  } minn[200010 << 2];
  int lazy[200010 << 2];
  void up(int k) {
    if (minn[k << 1].val < minn[k << 1 | 1].val)
      minn[k] = minn[k << 1];
    else if (minn[k << 1].val > minn[k << 1 | 1].val)
      minn[k] = minn[k << 1 | 1];
    else
      minn[k] = minn[k << 1], minn[k].num += minn[k << 1 | 1].num;
  }
  void downn(int k, int v) {
    minn[k].val += v;
    lazy[k] += v;
  }
  void down(int k) {
    if (lazy[k]) {
      downn(k << 1, lazy[k]), downn(k << 1 | 1, lazy[k]);
      lazy[k] = 0;
    }
  }
  void build(int k, int l, int r) {
    if (l == r) {
      minn[k] = data(0, 1);
      return;
    }
    int mid = (l + r) >> 1;
    build(k << 1, l, mid);
    build(k << 1 | 1, mid + 1, r);
    up(k);
  }
  void update(int k, int l, int r, int ql, int qr, int v) {
    if (ql <= l && r <= qr) {
      downn(k, v);
      return;
    }
    down(k);
    int mid = (l + r) >> 1;
    if (ql <= mid) update(k << 1, l, mid, ql, qr, v);
    if (qr > mid) update(k << 1 | 1, mid + 1, r, ql, qr, v);
    up(k);
  }
  int query(int k, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) {
      if (minn[k].val == 1) return minn[k].num;
      return 0;
    }
    down(k);
    int mid = (l + r) >> 1, ans = 0;
    if (ql <= mid) ans += query(k << 1, l, mid, ql, qr);
    if (qr > mid) ans += query(k << 1 | 1, mid + 1, r, ql, qr);
    return ans;
  }
} seg;
int n, m, a[1010][1010], id[1010][1010];
int fx[] = {-1, 0, 1, 0}, fy[] = {0, 1, 0, -1};
int minn[200010 << 2], num[200010 << 2];
long long ans;
bool check(int v, int l, int r) {
  for (int i = 0; i < 4; i++) {
    int xi = pos[v].x + fx[i], yi = pos[v].y + fy[i];
    if (xi < 1 || xi > n || yi < 1 || yi > m || a[xi][yi] < l || a[xi][yi] > r)
      continue;
    for (int j = i + 1; j < 4; j++) {
      int xj = pos[v].x + fx[j], yj = pos[v].y + fy[j];
      if (xj < 1 || xj > n || yj < 1 || yj > m || a[xj][yj] < l ||
          a[xj][yj] > r)
        continue;
      if (lct.connected(id[xi][yi], id[xj][yj])) return true;
    }
  }
  return false;
}
void del(int v, int l, int r) {
  for (int i = 0; i < 4; i++) {
    int xx = pos[v].x + fx[i], yy = pos[v].y + fy[i];
    if (xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] < l || a[xx][yy] > r)
      continue;
    lct.cut(id[pos[v].x][pos[v].y], id[xx][yy]);
  }
}
void add(int v, int l, int r) {
  for (int i = 0; i < 4; i++) {
    int xx = pos[v].x + fx[i], yy = pos[v].y + fy[i];
    if (xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] < l || a[xx][yy] > r)
      continue;
    seg.update(1, 1, n * m, a[xx][yy], n * m, -1);
    lct.link(id[pos[v].x][pos[v].y], id[xx][yy]);
  }
}
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]);
      pos[a[i][j]] = Point(i, j);
      id[i][j] = (i - 1) * m + j;
    }
  }
  seg.build(1, 1, n * m);
  int l, r = n * m;
  for (l = n * m; l >= 1; l--) {
    while (l < r && check(l, l + 1, r)) {
      del(r, l + 1, r - 1);
      r--;
    }
    seg.update(1, 1, n * m, l, r, 1);
    add(l, l + 1, r);
    ans += seg.query(1, 1, n * m, l, r);
  }
  printf("%lld\n", ans);
  return 0;
}