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;
int n, m;
int mat[1009][1009];
int f[200009], ch[200009][2], rv[200009], st[200009];
int l[800009], r[800009], mn[800009], sm[800009], lz[800009];
pair<int, int> pos[200009];
int dk[5][2] = {{}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void rot(int x) {
  int y = f[x], z = f[y], dx = x == ch[y][1], dy = y == ch[z][1];
  if (y == ch[f[y]][1] || y == ch[f[y]][0]) ch[z][dy] = x;
  f[x] = z;
  f[y] = x;
  f[ch[x][dx ^ 1]] = y;
  ch[y][dx] = ch[x][dx ^ 1];
  ch[x][dx ^ 1] = y;
}
void down(int x) {
  if (rv[x])
    swap(ch[x][0], ch[x][1]), rv[ch[x][0]] ^= 1, rv[ch[x][1]] ^= 1, rv[x] = 0;
}
void splay(int x) {
  int y, z, tp = 0;
  for (y = x; y == ch[f[y]][1] || y == ch[f[y]][0]; y = f[y]) st[++tp] = y;
  st[++tp] = y;
  for (int i = tp; i >= 1; i--) down(st[i]);
  while (x == ch[f[x]][1] || x == ch[f[x]][0]) {
    y = f[x], z = f[y];
    if (y == ch[f[y]][1] || y == ch[f[y]][0])
      rot((x == ch[y][1]) ^ (y == ch[z][1]) ? x : y);
    rot(x);
  }
}
void access(int x) {
  for (int y = 0; x; y = x, x = f[x]) {
    splay(x);
    ch[x][1] = y;
  }
}
int findrt(int x) {
  access(x);
  splay(x);
  down(x);
  while (ch[x][0]) down(x = ch[x][0]);
  splay(x);
  return x;
}
void makert(int x) {
  access(x);
  splay(x);
  rv[x] ^= 1;
}
int lnk(int x, int y) {
  makert(y);
  if (findrt(x) == y) return 0;
  f[y] = x;
  return 1;
}
int cut(int x, int y) {
  makert(x);
  if (findrt(y) == x && f[y] == x && !ch[y][0]) {
    f[y] = ch[x][1] = 0;
    return 1;
  }
  return 0;
}
int judge(int x, int y) { return findrt(x) == findrt(y); }
void downs(int a) {
  if (!lz[a]) return;
  int la = a << 1, ra = a << 1 | 1, t = lz[a];
  mn[la] += t, mn[ra] += t;
  lz[la] += t, lz[ra] += t;
  lz[a] = 0;
}
void upd(int a) {
  int la = a << 1, ra = a << 1 | 1;
  if (mn[la] == mn[ra])
    mn[a] = mn[la], sm[a] = sm[la] + sm[ra];
  else {
    mn[a] = min(mn[la], mn[ra]);
    sm[a] = mn[la] < mn[ra] ? sm[la] : sm[ra];
  }
}
void build(int a, int ll, int rr) {
  l[a] = ll;
  r[a] = rr;
  mn[a] = 0x7F7F7F7F;
  sm[a] = 1;
  if (ll < rr) {
    int mid = (ll + rr) >> 1;
    build(a << 1, ll, mid);
    build(a << 1 | 1, mid + 1, rr);
    upd(a);
  }
}
void add(int a, int ll, int rr, int val) {
  if (l[a] > rr || r[a] < ll) return;
  if (l[a] >= ll && r[a] <= rr) {
    mn[a] += val;
    lz[a] += val;
    return;
  }
  downs(a);
  add(a << 1, ll, rr, val);
  add(a << 1 | 1, ll, rr, val);
  upd(a);
}
int ask(int a, int ll, int rr) {
  if (l[a] > rr || r[a] < ll) return 0;
  if (l[a] >= ll && r[a] <= rr) {
    if (mn[a] != 1) return 0;
    return sm[a];
  }
  downs(a);
  return ask(a << 1, ll, rr) + ask(a << 1 | 1, ll, rr);
}
int test(int lp, int rp) {
  int px = pos[lp].first, py = pos[lp].second;
  int tmp[5], tp = 0;
  for (int i = 1; i <= 4; i++) {
    int np = mat[px + dk[i][0]][py + dk[i][1]];
    if (np >= lp && np <= rp) tmp[++tp] = np;
  }
  for (int i = 1; i <= tp - 1; i++) {
    makert(tmp[i]);
    for (int j = i + 1; j <= tp; j++)
      if (findrt(tmp[j]) == tmp[i]) return 0;
  }
  return 1;
}
void solve() {
  build(1, 1, n * m);
  int rp = n * m;
  long long ans = 0;
  for (int lp = n * m; lp >= 1; lp--) {
    while (!test(lp, rp)) {
      int rx = pos[rp].first, ry = pos[rp].second;
      for (int i = 1; i <= 4; i++) {
        int np = mat[rx + dk[i][0]][ry + dk[i][1]];
        if (np >= lp && np <= rp) cut(rp, np);
      }
      rp--;
    }
    int lx = pos[lp].first, ly = pos[lp].second;
    for (int i = 1; i <= 4; i++) {
      int np = mat[lx + dk[i][0]][ly + dk[i][1]];
      if (np >= lp && np <= rp) lnk(lp, np);
    }
    add(1, lp, lp, -0x7F7F7F7F);
    add(1, lp, rp, 1);
    for (int i = 1; i <= 4; i++) {
      int np = mat[lx + dk[i][0]][ly + dk[i][1]];
      if (np >= lp && np <= rp) add(1, np, rp, -1);
    }
    ans += ask(1, lp, rp);
  }
  printf("%I64d", ans);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", mat[i] + j);
      pos[mat[i][j]] = pair<int, int>(i, j);
    }
  solve();
  return 0;
}