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, N, a[1005][1005];
pair<int, int> pos[200005];
namespace LCT {
int fa[200005], ch[200005][2];
bool rev[200005];
inline bool isc(int x) { return ch[fa[x]][1] == x; }
inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline void rot(int x) {
  int y = fa[x], z = fa[y], c = isc(x);
  !isr(y) && (ch[z][isc(y)] = x);
  (ch[y][c] = ch[x][!c]) && (fa[ch[y][c]] = y);
  fa[ch[x][!c] = y] = x, fa[x] = z;
}
inline void pd(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;
  }
}
inline void pdpath(int x) {
  if (!isr(x)) pdpath(fa[x]);
  pd(x);
}
inline void splay(int x) {
  for (pdpath(x); !isr(x); rot(x))
    if (!isr(fa[x])) rot(isc(fa[x]) == isc(x) ? fa[x] : x);
}
inline void access(int x) {
  for (int y = 0; x; x = fa[y = x]) splay(x), ch[x][1] = y;
}
inline void bert(int x) { access(x), splay(x), rev[x] ^= 1; }
inline int sert(int x) {
  access(x), splay(x);
  for (; ch[x][0]; x = ch[x][0])
    ;
  return x;
}
inline void link(int x, int y) { bert(x), fa[x] = y; }
inline void cut(int x, int y) {
  bert(x), access(y), splay(y), ch[y][0] = fa[x] = 0;
}
inline bool connect(int x, int y) {
  bert(x);
  return sert(y) == x;
}
}  // namespace LCT
struct Segment_Tree {
  pair<int, int> val[200005 << 2];
  int tag[200005 << 2];
  void build(int i, int l, int r) {
    val[i] = pair<int, int>(0, r - l + 1);
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(i << 1, l, mid), build(i << 1 | 1, mid + 1, r);
  }
  inline pair<int, int> merge(pair<int, int> a, pair<int, int> b) {
    if (a.first != b.first) return a.first < b.first ? a : b;
    return pair<int, int>(a.first, a.second + b.second);
  }
  inline void pushdown(int i) {
    if (tag[i])
      val[i << 1].first += tag[i], tag[i << 1] += tag[i],
          val[i << 1 | 1].first += tag[i], tag[i << 1 | 1] += tag[i],
          tag[i] = 0;
  }
  void modify(int i, int l, int r, int first, int second, int v) {
    if (first <= l && r <= second) {
      val[i].first += v, tag[i] += v;
      return;
    }
    int mid = (l + r) >> 1;
    pushdown(i);
    if (first <= mid) modify(i << 1, l, mid, first, second, v);
    if (second > mid) modify(i << 1 | 1, mid + 1, r, first, second, v);
    val[i] = merge(val[i << 1], val[i << 1 | 1]);
  }
  pair<int, int> query(int i, int l, int r, int first, int second) {
    if (first <= l && r <= second) return val[i];
    int mid = (l + r) >> 1;
    pair<int, int> ret = pair<int, int>(1e9, 0);
    pushdown(i);
    if (first <= mid) ret = merge(ret, query(i << 1, l, mid, first, second));
    if (second > mid)
      ret = merge(ret, query(i << 1 | 1, mid + 1, r, first, second));
    return ret;
  }
} T;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool check(int k) {
  for (int i = 0, first; i < 4; i++)
    if (first = a[pos[k].first + dx[i]][pos[k].second + dy[i]])
      for (int j = i + 1, second; j < 4; j++)
        if (second = a[pos[k].first + dx[j]][pos[k].second + dy[j]])
          if (LCT::connect(first, second)) return 0;
  return 1;
}
void del(int k) {
  for (int i = 0, first; i < 4; i++)
    if (first = a[pos[k].first + dx[i]][pos[k].second + dy[i]])
      if (LCT::connect(k, first)) LCT::cut(k, first);
}
void solve(int l, int r) {
  for (int i = 0, first; i < 4; i++)
    if ((first = a[pos[r].first + dx[i]][pos[r].second + dy[i]]) >= l &&
        first <= r)
      LCT::link(first, r), T.modify(1, 1, N, l, first, -1);
  T.modify(1, 1, N, l, r, 1);
}
int main() {
  scanf("%d%d", &n, &m), N = 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]] = pair<int, int>(i, j);
  T.build(1, 1, N);
  long long ans = 0;
  for (int l = 1, r = 1; r <= N; r++) {
    while (!check(r)) del(l++);
    solve(l, r);
    ans += T.query(1, 1, N, l, r).second;
  }
  printf("%I64d\n", ans);
}