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, lim, a[1010][1010], dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
pair<int, int> p[1000100];
long long res;
namespace LCT {
struct Link_Cut_Tree {
  int fa, ch[2];
  bool rev;
} t[200100];
inline int identify(int x) {
  if (x == t[t[x].fa].ch[0]) return 0;
  if (x == t[t[x].fa].ch[1]) return 1;
  return -1;
}
inline void REV(int x) { t[x].rev ^= 1, swap(t[x].ch[0], t[x].ch[1]); }
inline void pushdown(int x) {
  if (!t[x].rev) return;
  if (t[x].ch[0]) REV(t[x].ch[0]);
  if (t[x].ch[1]) REV(t[x].ch[1]);
  t[x].rev = 0;
}
inline void rotate(int x) {
  register int y = t[x].fa, z = t[y].fa, dirx = identify(x), diry = identify(y),
               b = t[x].ch[!dirx];
  if (diry != -1) t[z].ch[diry] = x;
  t[x].fa = z;
  if (b) t[b].fa = y;
  t[y].ch[dirx] = b;
  t[y].fa = x, t[x].ch[!dirx] = y;
}
inline void pushall(int x) {
  if (identify(x) != -1) pushall(t[x].fa);
  pushdown(x);
}
inline void splay(int x) {
  for (pushall(x); identify(x) != -1; rotate(x))
    if (identify(t[x].fa) != -1)
      rotate(identify(x) == identify(t[x].fa) ? t[x].fa : x);
}
inline void access(int x) {
  for (register int y = 0; x; x = t[y = x].fa) splay(x), t[x].ch[1] = y;
}
inline void makeroot(int x) { access(x), splay(x), REV(x); }
inline int findroot(int x) {
  access(x), splay(x), pushdown(x);
  while (t[x].ch[0]) x = t[x].ch[0], pushdown(x);
  splay(x);
  return x;
}
inline void split(int x, int y) { makeroot(x), access(y), splay(y); }
inline bool link(int x, int y) {
  if (findroot(x) == findroot(y)) return false;
  makeroot(x), t[x].fa = y;
  return true;
}
inline void cut(int x, int y) { split(x, y), t[y].ch[0] = t[x].fa = 0; }
}  // namespace LCT
namespace SG {
struct SegTree {
  int mn, num, tag;
} seg[800100];
void ADD(int x, int y) { seg[x].mn += y, seg[x].tag += y; }
void pushdown(int x) {
  ADD(x << 1, seg[x].tag), ADD(x << 1 | 1, seg[x].tag), seg[x].tag = 0;
}
void pushup(int x) {
  seg[x].mn = min(seg[x << 1].mn, seg[x << 1 | 1].mn), seg[x].num = 0;
  if (seg[x].mn == seg[x << 1].mn) seg[x].num += seg[x << 1].num;
  if (seg[x].mn == seg[x << 1 | 1].mn) seg[x].num += seg[x << 1 | 1].num;
}
void build(int x, int l, int r) {
  if (l == r) {
    seg[x].mn = 0, seg[x].num = 1;
    return;
  }
  build(x << 1, l, ((l + r) >> 1)), build(x << 1 | 1, ((l + r) >> 1) + 1, r),
      pushup(x);
}
void modify(int x, int l, int r, int L, int R, int val) {
  if (l > R || r < L) return;
  if (L <= l && r <= R) {
    ADD(x, val);
    return;
  }
  pushdown(x), modify(x << 1, l, ((l + r) >> 1), L, R, val),
      modify(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, val), pushup(x);
}
int query(int x, int l, int r, int L, int R) {
  if (l > R || r < L) return 0;
  if (L <= l && r <= R) return seg[x].mn == 1 ? seg[x].num : 0;
  pushdown(x);
  return query(x << 1, l, ((l + r) >> 1), L, R) +
         query(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R);
}
}  // namespace SG
int main() {
  scanf("%d%d", &n, &m), lim = n * m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &a[i][j]), p[a[i][j]] = make_pair(i, j);
  SG::build(1, 1, lim);
  for (int l = 1, r = 1; l <= lim; l++) {
    while (r <= lim) {
      int x = p[r].first, y = p[r].second;
      int cnt = 0;
      for (int i = 0; i < 4; i++) {
        if (!(a[x + dx[i]][y + dy[i]] >= l && a[x + dx[i]][y + dy[i]] < r))
          continue;
        cnt++;
        if (LCT::link(r, a[x + dx[i]][y + dy[i]])) continue;
        for (i--; i >= 0; i--)
          if (a[x + dx[i]][y + dy[i]] >= l && a[x + dx[i]][y + dy[i]] < r)
            LCT::cut(r, a[x + dx[i]][y + dy[i]]);
        cnt = -1;
        break;
      }
      if (cnt == -1) break;
      SG::modify(1, 1, lim, r, lim, -cnt);
      SG::modify(1, 1, lim, r, r, r - l + 1);
      r++;
    }
    res += SG::query(1, 1, lim, l, r - 1);
    SG::modify(1, 1, lim, l, r - 1, -1);
    int x = p[l].first, y = p[l].second;
    for (int i = 0; i < 4; i++)
      if (a[x + dx[i]][y + dy[i]] >= l && a[x + dx[i]][y + dy[i]] < r)
        LCT::cut(l, a[x + dx[i]][y + dy[i]]),
            SG::modify(1, 1, lim, a[x + dx[i]][y + dy[i]], lim, 1);
  }
  printf("%lld\n", res);
  return 0;
}