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>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const int N = 200005, M = 1005;
int n, m, q;
int a[M][M];
int left[N], first[N], second[N];
namespace lct {
int ch[N][2], f[N];
bool tag[N];
bool get(int first) { return ch[f[first]][1] == first; }
bool isroot(int first) {
  return ch[f[first]][0] != first && ch[f[first]][1] != first;
}
void rotate(int first) {
  int second = f[first], z = f[second];
  bool b = get(first);
  if (!isroot(second)) ch[z][get(second)] = first;
  ch[second][b] = ch[first][!b], ch[first][!b] = second;
  f[second] = first, f[first] = z;
  if (ch[second][b]) f[ch[second][b]] = second;
}
int stack[N], top;
void pushdown(int first) {
  if (!tag[first]) return;
  tag[first] = 0, std::swap(ch[first][0], ch[first][1]);
  tag[ch[first][0]] ^= 1, tag[ch[first][1]] ^= 1;
}
void splay(int first) {
  stack[top = 1] = first;
  for (int second = first; !isroot(second); stack[++top] = second = f[second])
    ;
  while (top) pushdown(stack[top--]);
  for (; !isroot(first); rotate(first))
    if (!isroot(f[first]))
      rotate(get(first) ^ get(f[first]) ? first : f[first]);
}
void access(int first) {
  for (int t = 0; first; ch[first][1] = t, t = first, first = f[first])
    splay(first);
}
void makeroot(int first) { access(first), splay(first), tag[first] ^= 1; }
void link(int first, int second) { makeroot(first), f[first] = second; }
void split(int first, int second) {
  makeroot(first), access(second), splay(second);
}
void cut(int first, int second) {
  split(first, second), f[first] = ch[second][0] = 0;
}
bool conn(int first, int second) {
  split(first, second);
  while (pushdown(second), ch[second][0]) second = ch[second][0];
  return splay(second), first == second;
}
}  // namespace lct
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
bool check(int p) {
  int first = ::first[p], second = ::second[p];
  static int c[4];
  int cnt = 0;
  for (int i = 0; i < 4; ++i)
    if (a[first + dx[i]][second + dy[i]] &&
        a[first + dx[i]][second + dy[i]] < p)
      c[cnt++] = a[first + dx[i]][second + dy[i]];
  for (int i = 0; i < cnt; ++i)
    for (int j = i + 1; j < cnt; ++j)
      if (lct::conn(c[i], c[j])) return 0;
  return 1;
}
void remove(int p, int pl) {
  int first = ::first[p], second = ::second[p];
  for (int i = 0; i < 4; ++i)
    if (a[first + dx[i]][second + dy[i]] > p &&
        a[first + dx[i]][second + dy[i]] < pl)
      lct::cut(p, a[first + dx[i]][second + dy[i]]);
}
void insert(int p, int pl) {
  int first = ::first[p], second = ::second[p];
  for (int i = 0; i < 4; ++i)
    if (a[first + dx[i]][second + dy[i]] >= pl &&
        a[first + dx[i]][second + dy[i]] < p)
      lct::link(p, a[first + dx[i]][second + dy[i]]);
}
int tag[N << 2];
std::pair<int, int> sum[N << 2];
std::pair<int, int> operator+(std::pair<int, int> a, std::pair<int, int> b) {
  if (a.first == b.first)
    return std::pair<int, int>(a.first, a.second + b.second);
  else
    return std::min(a, b);
}
void pushup(int o) { sum[o] = sum[o << 1] + sum[o << 1 | 1]; }
void build(int o = 1, int l = 1, int r = q) {
  sum[o] = std::pair<int, int>(-r, 1);
  if (l == r) return;
  int mid = l + r >> 1;
  build(o << 1, l, mid);
  build(o << 1 | 1, mid + 1, r);
}
void settag(int o, int v = -1) { tag[o] += v, sum[o].first += v; }
void pushdown(int o) {
  settag(o << 1, tag[o]);
  settag(o << 1 | 1, tag[o]);
  tag[o] = 0;
}
void modify(int R, int o = 1, int l = 1, int r = q) {
  if (R >= r) return settag(o);
  int mid = l + r >> 1;
  pushdown(o);
  modify(R, o << 1, l, mid);
  if (R > mid) modify(R, o << 1 | 1, mid + 1, r);
  pushup(o);
}
std::pair<int, int> query(int L, int R, int o = 1, int l = 1, int r = q) {
  if (L <= l && R >= r) return sum[o];
  int mid = l + r >> 1;
  std::pair<int, int> ret = std::pair<int, int>(0x3f3f3f3f, 0);
  pushdown(o);
  if (L <= mid) ret = ret + query(L, R, o << 1, l, mid);
  if (R > mid) ret = ret + query(L, R, o << 1 | 1, mid + 1, r);
  return ret;
}
void update(int p) {
  int first = ::first[p], second = ::second[p];
  for (int i = 0; i < 4; ++i)
    if (a[first + dx[i]][second + dy[i]] &&
        a[first + dx[i]][second + dy[i]] < p)
      modify(a[first + dx[i]][second + dy[i]]);
}
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m, q = n * m;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      std::cin >> a[i][j];
      first[a[i][j]] = i, second[a[i][j]] = j;
    }
  int pl = 1;
  for (int i = 1; i <= q; ++i) {
    while (!check(i)) remove(pl++, i);
    insert(i, pl), left[i] = pl;
  }
  build();
  long long ans = 0;
  for (int i = 1; i <= q; ++i) update(i), ans += query(left[i], i).second;
  std::cout << ans << '\n';
  return 0;
}