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 err_stream {
  template <typename T>
  err_stream& operator<<(T x) {
    return *this;
  }
};
const int MAXN = 200007;
int N, R, C;
int rev[MAXN], par[MAXN], ch[MAXN][2], st[MAXN], top;
bool dir(int x) { return x == ch[par[x]][1]; }
bool isroot(int x) { return x != ch[par[x]][dir(x)]; }
void pushdown(int x) {
  if (rev[x]) {
    swap(ch[x][0], ch[x][1]);
    rev[ch[x][0]] ^= 1;
    rev[ch[x][1]] ^= 1;
    rev[x] = 0;
  }
}
void rot(int x) {
  int p = par[x], d = dir(x);
  if (!isroot(p)) ch[par[p]][dir(p)] = x;
  par[x] = par[p];
  par[ch[p][d] = ch[x][!d]] = p;
  par[ch[x][!d] = p] = x;
}
void splay(int x) {
  st[top = 1] = x;
  for (int u = x; !isroot(u); u = par[u]) st[++top] = par[u];
  while (top) pushdown(st[top--]);
  for (; !isroot(x); rot(x))
    if (!isroot(par[x])) rot(dir(x) == dir(par[x]) ? par[x] : x);
}
void access(int x) {
  for (int y = 0; x; y = x, x = par[y]) {
    splay(x);
    ch[x][1] = y;
  }
}
void make_root(int x) {
  access(x);
  splay(x);
  rev[x] ^= 1;
}
bool link(int x, int y) {
  make_root(x);
  access(y);
  splay(y);
  err_stream() << "link: " << x << ' ' << y << ' ' << par[x] << '\n';
  if (par[x]) return false;
  par[x] = y;
  return true;
}
bool cut(int x, int y) {
  make_root(x);
  access(y);
  splay(y);
  err_stream() << "cut: " << x << ' ' << y << ": " << par[x] << ' ' << ch[y][0]
               << '\n';
  if (par[x] != y || ch[x][1]) return false;
  ch[y][0] = par[x] = 0;
  return true;
}
int mn[MAXN << 2], cnt[MAXN << 2], inc[MAXN << 2];
void push(int rt, int lo, int hi) {
  if (lo < hi && inc[rt]) {
    mn[rt << 1] += inc[rt];
    mn[rt << 1 | 1] += inc[rt];
    inc[rt << 1] += inc[rt];
    inc[rt << 1 | 1] += inc[rt];
    inc[rt] = 0;
  }
}
void pull(int rt) {
  mn[rt] = min(mn[rt << 1], mn[rt << 1 | 1]);
  cnt[rt] = (mn[rt] == mn[rt << 1]) * cnt[rt << 1] +
            (mn[rt] == mn[rt << 1 | 1]) * cnt[rt << 1 | 1];
}
void upd(int l, int r, int x, int rt = 1, int lo = 1, int hi = N) {
  if (hi < l || r < lo) return;
  if (l <= lo && hi <= r) {
    mn[rt] += x, inc[rt] += x;
    return;
  }
  int md = (lo + hi) >> 1;
  push(rt, lo, hi);
  upd(l, r, x, rt << 1, lo, md);
  upd(l, r, x, rt << 1 | 1, md + 1, hi);
  pull(rt);
}
int get(int l, int r, int rt = 1, int lo = 1, int hi = N) {
  if (hi < l || r < lo) return 0;
  if (l <= lo && hi <= r) return (!mn[rt]) * cnt[rt];
  int md = (lo + hi) >> 1;
  push(rt, lo, hi);
  return get(l, r, rt << 1, lo, md) + get(l, r, rt << 1 | 1, md + 1, hi);
}
void build(int rt = 1, int lo = 1, int hi = N) {
  if (lo == hi) {
    mn[rt] = lo, cnt[rt] = 1;
    return;
  }
  int md = (lo + hi) >> 1;
  build(rt << 1, lo, md);
  build(rt << 1 | 1, md + 1, hi);
  pull(rt);
}
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
bool valid(int i, int j) { return 0 <= i && i < R && 0 <= j && j < C; }
vector<vector<int>> A;
vector<pair<int, int>> add, rem;
pair<int, int> pos[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> R >> C;
  N = R * C;
  A.assign(R, vector<int>(C));
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      cin >> A[i][j];
      pos[A[i][j]] = make_pair(i, j);
    }
  }
  int E = (R - 1) * C + R * (C - 1);
  add.reserve(E);
  rem.reserve(E);
  for (int x = 1; x <= N; ++x) {
    int i = pos[x].first, j = pos[x].second;
    for (int d = 0; d < 4; ++d) {
      int i0 = i + dx[d], j0 = j + dy[d];
      if (valid(i0, j0)) {
        if (A[i0][j0] > x) {
          rem.emplace_back(x, A[i0][j0]);
        } else {
          add.emplace_back(x, A[i0][j0]);
        }
      }
    }
  }
  build();
  int64_t ans = 0;
  for (int l = 1, i = 0, j = 0; l <= N; ++l) {
    upd(l, N, -1);
    for (; i < E; ++i) {
      if (add[i].second >= l) {
        if (link(add[i].first, add[i].second)) {
          err_stream() << "Y" << '\n';
          upd(add[i].first, N, -1);
        } else {
          break;
        }
      }
    }
    int r = (i == E ? N + 1 : add[i].first);
    ans += get(l, r - 1);
    err_stream() << "S " << r << ' ' << get(l, r - 1) << '\n';
    for (; j < E && rem[j].first == l; ++j) {
      if (cut(rem[j].first, rem[j].second)) {
        err_stream() << "Y" << '\n';
        upd(rem[j].second, N, 1);
      }
    }
  }
  cout << ans << '\n';
  return 0;
}