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;
const int N = 2e3 + 10, M = 2e5 + 10;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int n, m;
int w[N][N];
long long ans;
vector<int> vec[M];
struct LCT {
  int ch[M][2], fa[M], rev[M], stk[M];
  bool nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
  void dorev(int x) { swap(ch[x][0], ch[x][1]), rev[x] ^= 1; }
  void psd(int x) {
    if (rev[x]) {
      if (ch[x][0]) dorev(ch[x][0]);
      if (ch[x][1]) dorev(ch[x][1]);
      rev[x] = 0;
    }
  }
  void rotate(int x) {
    psd(fa[x]), psd(x);
    int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
    if (nroot(y)) ch[z][ch[z][1] == y] = x;
    fa[x] = z;
    if (w) fa[w] = y;
    ch[y][k] = w;
    ch[x][k ^ 1] = y;
    fa[y] = x;
  }
  void splay(int x) {
    int y = x, z = 0;
    stk[++z] = y;
    while (nroot(y)) stk[++z] = y = fa[y];
    while (z) psd(stk[z--]);
    while (nroot(x)) {
      int y = fa[x], z = fa[y];
      if (nroot(y)) rotate((ch[y][0] == x) ^ (ch[z][0] == y) ? x : y);
      rotate(x);
    }
  }
  void access(int x) {
    for (int y = 0; x; y = x, x = fa[y]) splay(x), ch[x][1] = y;
  }
  int findroot(int x) {
    access(x), splay(x);
    while (ch[x][0]) psd(x), x = ch[x][0];
    splay(x);
    return x;
  }
  void makeroot(int x) {
    access(x), splay(x);
    dorev(x);
  }
  void split(int x, int y) {
    makeroot(x);
    access(y), splay(y);
  }
  bool dolink(int x, int y) {
    makeroot(x);
    if (findroot(y) == x) return 0;
    fa[x] = y;
    return 1;
  }
  void docut(int x, int y) {
    makeroot(x);
    if (findroot(y) == x && fa[y] == x && !ch[y][0]) fa[y] = ch[x][1] = 0;
  }
} T;
struct SegTree {
  int mn[M << 2], num[M << 2], tag[M << 2];
  void psd(int x) {
    if (tag[x]) {
      tag[x << 1] += tag[x], mn[x << 1] += tag[x];
      tag[x << 1 | 1] += tag[x], mn[x << 1 | 1] += tag[x];
      tag[x] = 0;
    }
  }
  void upd(int x) {
    if (mn[x << 1] < mn[x << 1 | 1]) mn[x] = mn[x << 1], num[x] = num[x << 1];
    if (mn[x << 1] == mn[x << 1 | 1])
      mn[x] = mn[x << 1], num[x] = num[x << 1] + num[x << 1 | 1];
    if (mn[x << 1] > mn[x << 1 | 1])
      mn[x] = mn[x << 1 | 1], num[x] = num[x << 1 | 1];
  }
  void build(int x, int l, int r) {
    if (l == r) {
      mn[x] = 0, num[x] = 1;
      return;
    }
    int mid = (l + r) >> 1;
    build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
    upd(x);
  }
  void modify(int x, int l, int r, int lx, int rx, int v) {
    if (lx <= l && r <= rx) {
      mn[x] += v, tag[x] += v;
      return;
    }
    int mid = (l + r) >> 1;
    psd(x);
    if (lx <= mid) modify(x << 1, l, mid, lx, rx, v);
    if (rx > mid) modify(x << 1 | 1, mid + 1, r, lx, rx, v);
    upd(x);
  }
  int query(int x, int l, int r, int lx, int rx) {
    if (lx <= l && r <= rx) return mn[x] == 1 ? num[x] : 0;
    psd(x);
    int mid = (l + r) >> 1, ret = 0;
    if (lx <= mid) ret += query(x << 1, l, mid, lx, rx);
    if (rx > mid) ret += query(x << 1 | 1, mid + 1, r, lx, rx);
    return ret;
  }
} S;
int main() {
  cin >> 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 k = 0; k <= 3; k++) {
        int x = i + dx[k], y = j + dy[k];
        if (x < 1 || y < 1 || x > n || y > m || w[x][y] > w[i][j]) continue;
        vec[w[i][j]].push_back(w[x][y]);
        vec[w[x][y]].push_back(w[i][j]);
      }
  int t = n * m, r = 0;
  S.build(1, 1, t);
  for (int l = 1; l <= t; l++) {
    for (int i = r + 1; i <= t; i++) {
      int flag = 1, tot = 0;
      for (int j = 0; j < vec[i].size(); j++) {
        int x = vec[i][j];
        if (x < i && x >= l) {
          ++tot;
          if (!T.dolink(i, x)) {
            flag = 0;
            break;
          }
        }
      }
      if (!flag) {
        for (int j = 0; j < vec[i].size(); j++) {
          int x = vec[i][j];
          if (x < i && x >= l) T.docut(i, x);
        }
        break;
      }
      r = i;
      S.modify(1, 1, t, i, i, i - l + 1);
      S.modify(1, 1, t, i, t, -tot);
    }
    ans += S.query(1, 1, t, l, r);
    for (int j = 0; j < vec[l].size(); j++) {
      int x = vec[l][j];
      if (x > l && x <= r) T.docut(l, x), S.modify(1, 1, t, x, t, 1);
    }
    S.modify(1, 1, t, l, r, -1);
  }
  printf("%lld\n", ans);
  return 0;
}