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 gi() {
  int x = 0, w = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') w = 0, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return w ? x : -x;
}
const int N = 2e5 + 5;
int n, m, tot, f[1005][1005], px[N], py[N], fa[N], ch[2][N], rev[N];
int L = 1, R = 1, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, tmp[10],
    tag[N << 2];
pair<int, int> sum[N << 2];
long long ans = 1;
bool son(int x) { return x == ch[1][fa[x]]; }
bool isroot(int x) { return x != ch[0][fa[x]] && x != ch[1][fa[x]]; }
void rotate(int x) {
  int y = fa[x], z = fa[y], c = son(x);
  ch[c][y] = ch[c ^ 1][x];
  if (ch[c][y]) fa[ch[c][y]] = y;
  fa[x] = z;
  if (!isroot(y)) ch[son(y)][z] = x;
  ch[c ^ 1][x] = y;
  fa[y] = x;
}
void rever(int x) {
  swap(ch[0][x], ch[1][x]);
  rev[x] ^= 1;
}
void alldown(int x) {
  if (!isroot(x)) alldown(fa[x]);
  if (rev[x]) rever(ch[0][x]), rever(ch[1][x]), rev[x] = 0;
}
void splay(int x) {
  alldown(x);
  for (int y = fa[x]; !isroot(x); rotate(x), y = fa[x])
    if (!isroot(y)) son(x) ^ son(y) ? rotate(x) : rotate(y);
}
void access(int x) {
  for (int y = 0; x; y = x, x = fa[x]) splay(x), ch[1][x] = y;
}
void makeroot(int x) {
  access(x);
  splay(x);
  rever(x);
}
int findroot(int x) {
  access(x);
  splay(x);
  while (ch[0][x]) x = ch[0][x];
  splay(x);
  return x;
}
void split(int x, int y) {
  makeroot(x);
  access(y);
  splay(y);
}
void link(int x, int y) {
  makeroot(x);
  fa[x] = y;
}
void cut(int x, int y) {
  split(x, y);
  fa[x] = ch[0][y] = 0;
}
bool check() {
  int len = 0;
  for (int d = 0; d < 4; ++d) {
    int x = px[R + 1] + dx[d], y = py[R + 1] + dy[d];
    if (!x || x == n + 1 || !y || y == m + 1 || f[x][y] < L || f[x][y] > R)
      continue;
    tmp[++len] = findroot(f[x][y]);
  }
  sort(tmp + 1, tmp + len + 1);
  for (int i = 1; i < len; ++i)
    if (tmp[i] == tmp[i + 1]) return false;
  return true;
}
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
  pair<int, int> c;
  c.first = min(a.first, b.first);
  if (c.first == a.first) c.second += a.second;
  if (c.first == b.first) c.second += b.second;
  return c;
}
void build(int x, int l, int r) {
  if (l == r) {
    sum[x] = make_pair(0, 1);
    return;
  }
  int mid = l + r >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  sum[x] = sum[x << 1] + sum[x << 1 | 1];
}
void cover(int x, int v) {
  sum[x].first += v;
  tag[x] += v;
}
void down(int x) {
  if (!tag[x]) return;
  cover(x << 1, tag[x]);
  cover(x << 1 | 1, tag[x]);
  tag[x] = 0;
}
void modify(int x, int l, int r, int ql, int qr, int v) {
  if (l >= ql && r <= qr) {
    cover(x, v);
    return;
  }
  down(x);
  int mid = l + r >> 1;
  if (ql <= mid) modify(x << 1, l, mid, ql, qr, v);
  if (qr > mid) modify(x << 1 | 1, mid + 1, r, ql, qr, v);
  sum[x] = sum[x << 1] + sum[x << 1 | 1];
}
pair<int, int> query(int x, int l, int r, int ql, int qr) {
  if (l >= ql && r <= qr) return sum[x];
  down(x);
  int mid = l + r >> 1;
  pair<int, int> res = make_pair(1 << 30, 0);
  if (ql <= mid) res = res + query(x << 1, l, mid, ql, qr);
  if (qr > mid) res = res + query(x << 1 | 1, mid + 1, r, ql, qr);
  return res;
}
int main() {
  n = gi();
  m = gi();
  tot = n * m;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      f[i][j] = gi(), px[f[i][j]] = i, py[f[i][j]] = j;
  build(1, 1, tot);
  modify(1, 1, tot, 1, 1, 1);
  while (R < tot) {
    while (L < R && !check()) {
      for (int d = 0; d < 4; ++d) {
        int x = px[L] + dx[d], y = py[L] + dy[d];
        if (!x || x == n + 1 || !y || y == m + 1 || f[x][y] < L || f[x][y] > R)
          continue;
        cut(L, f[x][y]);
      }
      ++L;
    }
    ++R;
    modify(1, 1, tot, L, R, 1);
    for (int d = 0; d < 4; ++d) {
      int x = px[R] + dx[d], y = py[R] + dy[d];
      if (!x || x == n + 1 || !y || y == m + 1 || f[x][y] < L || f[x][y] > R)
        continue;
      link(R, f[x][y]);
      modify(1, 1, tot, L, f[x][y], -1);
    }
    pair<int, int> res = query(1, 1, tot, L, R);
    ans += res.first == 1 ? res.second : 0;
  }
  printf("%lld\n", ans);
  return 0;
}