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;
const int N = 2e3 + 9;
const int M = 1e6 + 9;
const int inf = 1e9 + 7;
int n, m, a[N][N];
struct pp {
  int x, y;
} p[M];
int l, r, st[M];
struct LCT {
  int fa, ch[2], tag;
} t[M];
bool isroot(int x) { return !(t[t[x].fa].ch[0] == x || t[t[x].fa].ch[1] == x); }
void update(int x) {
  if (!x) return;
  t[x].tag ^= 1;
  swap(t[x].ch[0], t[x].ch[1]);
  return;
}
void pushdown(int x) {
  if (t[x].tag) {
    update(t[x].ch[0]);
    update(t[x].ch[1]);
    t[x].tag = 0;
  }
  return;
}
void rotate(int x) {
  int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
  int w = t[x].ch[!k];
  if (!isroot(y)) t[z].ch[t[z].ch[1] == y] = x;
  t[x].ch[!k] = y, t[y].ch[k] = w;
  t[w].fa = y, t[y].fa = x, t[x].fa = z;
  return;
}
void splay(int x) {
  int y = x, z = 0;
  st[++z] = x;
  while (!isroot(y)) st[++z] = y = t[y].fa;
  while (z) pushdown(st[z--]);
  while (!isroot(x)) {
    int y = t[x].fa, z = t[y].fa;
    if (!isroot(y)) rotate((t[z].ch[1] == y) ^ (t[y].ch[1] == x) ? x : y);
    rotate(x);
  }
  return;
}
void access(int x) {
  for (int y = 0; x; x = t[y = x].fa) {
    splay(x);
    t[x].ch[1] = y;
  }
  return;
}
void makeroot(int x) {
  access(x);
  splay(x);
  update(x);
  return;
}
int findroot(int x) {
  access(x);
  splay(x);
  while (t[x].ch[0]) pushdown(x), x = t[x].ch[0];
  splay(x);
  return x;
}
void link(int x, int y) {
  makeroot(x);
  t[y].fa = x;
  return;
}
void cut(int x, int y) {
  makeroot(x);
  access(y);
  t[x].ch[1] = t[y].fa = 0;
  return;
}
void split(int x, int y) {
  makeroot(x);
  access(y);
  splay(y);
  return;
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool Graph_Connected(int u) {
  vector<int> vec;
  int x = p[u].x, y = p[u].y;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
    if (a[nx][ny] >= l && a[nx][ny] < r) vec.push_back(a[nx][ny]);
  }
  for (int i = 0; i < (int)vec.size(); i++) {
    makeroot(vec[i]);
    for (int j = i + 1; j < (int)vec.size(); j++) {
      x = vec[i], y = vec[j];
      if (findroot(y) == x) return 1;
    }
  }
  return 0;
}
struct segment {
  int mn, num, flag, tag;
} g[M << 2];
segment operator+(const segment x, const segment y) {
  segment z;
  z.tag = 0, z.flag = 0, z.mn = 0, z.num = 0;
  if (!x.flag) {
    z = y, z.tag = 0;
    return z;
  }
  if (!y.flag) {
    z = x, z.tag = 0;
    return z;
  }
  if (x.mn == y.mn) {
    z.flag = 1;
    z.mn = x.mn, z.num = x.num + y.num;
    return z;
  }
  if (x.mn < y.mn)
    z = x, z.tag = 0;
  else
    z = y, z.tag = 0;
  return z;
}
void Pushup(int c) {
  g[c] = g[(c << 1)] + g[(c << 1 | 1)];
  return;
}
void build(int c, int l, int r) {
  g[c].mn = 1, g[c].num = r - l + 1, g[c].flag = 1, g[c].tag = 0;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build((c << 1), l, mid);
  build((c << 1 | 1), mid + 1, r);
  Pushup(c);
  return;
}
void Update(int c, int d, int l, int r) {
  g[c].mn += d, g[c].tag += d;
  return;
}
void Pushdown(int c, int l, int r) {
  int mid = (l + r) >> 1;
  if (g[c].tag) {
    Update((c << 1), g[c].tag, l, mid);
    Update((c << 1 | 1), g[c].tag, mid + 1, r);
    g[c].tag = 0;
  }
  return;
}
void modify(int c, int l, int r, int x, int y, int d) {
  if (x > y) return;
  if (x <= l && r <= y) {
    Update(c, d, l, r);
    return;
  }
  Pushdown(c, l, r);
  int mid = (l + r) >> 1;
  if (x <= mid) modify((c << 1), l, mid, x, y, d);
  if (y > mid) modify((c << 1 | 1), mid + 1, r, x, y, d);
  Pushup(c);
  return;
}
segment query(int c, int l, int r, int x, int y) {
  if (x <= l && r <= y) return g[c];
  Pushdown(c, l, r);
  int mid = (l + r) >> 1;
  segment ans;
  ans.flag = 0;
  if (x <= mid) ans = ans + query((c << 1), l, mid, x, y);
  if (y > mid) ans = ans + query((c << 1 | 1), mid + 1, r, x, y);
  Pushup(c);
  return ans;
}
void del(int u) {
  int x = p[u].x, y = p[u].y;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
    if (a[nx][ny] >= l && a[nx][ny] < r) {
      cut(u, a[nx][ny]);
    }
  }
  return;
}
void linked(int u) {
  int x = p[u].x, y = p[u].y;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
    if (a[nx][ny] >= l && a[nx][ny] < r) {
      modify(1, 1, n * m, l, a[nx][ny], -1);
      link(u, a[nx][ny]);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) p[a[i][j]].x = i, p[a[i][j]].y = j;
  long long ans = 0;
  build(1, 1, n * m);
  for (r = 1, l = 1; r <= n * m; r++) {
    while (Graph_Connected(r)) del(l++);
    modify(1, 1, n * m, l, r, 1);
    linked(r);
    segment fd = query(1, 1, n * m, l, r);
    ans += fd.num;
  }
  printf("%lld\n", ans);
  return 0;
}