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 inf = 0x3f3f3f3f;
int n, m;
int mp[2020][2020];
int pos[200200][2];
int fl[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct LCT {
  int s[200200][2];
  int fa[200200];
  bool rev[200200];
  int get(int x) { return s[fa[x]][1] == x; }
  bool nroot(int x) { return s[fa[x]][0] == x || s[fa[x]][1] == x; }
  void rot(int x) {
    int y = fa[x], z = fa[y], p = get(x), yp = get(y);
    s[y][p] = s[x][!p];
    if (s[x][!p]) fa[s[x][!p]] = y;
    if (nroot(y)) s[z][yp] = x;
    fa[x] = z;
    fa[y] = x;
    s[x][!p] = y;
  }
  void pd(int x) {
    if (!rev[x]) return;
    swap(s[x][0], s[x][1]);
    if (s[x][0]) rev[s[x][0]] ^= 1;
    if (s[x][1]) rev[s[x][1]] ^= 1;
    rev[x] = 0;
  }
  void prepare(int x) {
    if (nroot(x)) prepare(fa[x]);
    pd(x);
  }
  void splay(int x) {
    prepare(x);
    int y;
    while (nroot(x)) {
      y = fa[x];
      if (!nroot(y)) {
        rot(x);
        return;
      }
      if (get(x) == get(y))
        rot(y);
      else
        rot(x);
      rot(x);
    }
  }
  void access(int x) {
    for (int y = 0; x; x = fa[x]) splay(x), s[x][1] = y, y = x;
  }
  void makeroot(int x) {
    access(x);
    splay(x);
    rev[x] ^= 1;
    pd(x);
  }
  int findroot(int x) {
    access(x);
    splay(x);
    pd(x);
    while (s[x][0]) x = s[x][0], pd(x);
    splay(x);
    return x;
  }
  bool connect(int x, int y) { return findroot(x) == findroot(y); }
  void link(int x, int y) {
    makeroot(y);
    fa[y] = x;
  }
  void cut(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
    s[y][0] = 0;
    fa[x] = 0;
  }
} lct;
struct SEG {
  int mi[2000200];
  int mc[2000200];
  int laz[2000200];
  void pd(int rt) {
    if (!laz[rt]) return;
    laz[rt * 2] += laz[rt];
    laz[rt * 2 + 1] += laz[rt];
    mi[rt] += laz[rt];
    laz[rt] = 0;
  }
  void up(int rt) {
    if (mi[rt * 2] < mi[rt * 2 + 1])
      mi[rt] = mi[rt * 2], mc[rt] = mc[rt * 2];
    else {
      mi[rt] = mi[rt * 2 + 1];
      mc[rt] = mc[rt * 2 + 1];
      if (mi[rt * 2] == mi[rt * 2 + 1]) mc[rt] += mc[rt * 2];
    }
  }
  void build(int rt, int l, int r) {
    mc[rt] = r - l + 1;
    if (l == r) return;
    int mid = (l + r) / 2;
    build(rt * 2, l, mid);
    build(rt * 2 + 1, mid + 1, r);
  }
  void change(int rt, int l, int r, int st, int ed, int v) {
    pd(rt);
    if (l > ed || r < st) return;
    if (l >= st && r <= ed) {
      laz[rt] += v;
      pd(rt);
      return;
    }
    int mid = (l + r) / 2;
    change(rt * 2, l, mid, st, ed, v);
    change(rt * 2 + 1, mid + 1, r, st, ed, v);
    up(rt);
  }
  pair<int, int> Co(pair<int, int> a, pair<int, int> b) {
    if (a.first < b.first)
      return a;
    else if (a.first > b.first)
      return b;
    return (pair<int, int>){a.first, a.second + b.second};
  }
  pair<int, int> ask(int rt, int l, int r, int st, int ed) {
    pd(rt);
    if (l > ed || r < st) return (pair<int, int>){inf, 0};
    if (l >= st && r <= ed) {
      return (pair<int, int>){mi[rt], mc[rt]};
    }
    int mid = (l + r) / 2;
    return Co(ask(rt * 2, l, mid, st, ed), ask(rt * 2 + 1, mid + 1, r, st, ed));
  }
  int val(int rt, int l, int r, int p) {
    pd(rt);
    if (l == r) return mi[rt];
    int mid = (l + r) / 2;
    if (mid >= p)
      return val(rt * 2, l, mid, p);
    else
      return val(rt * 2 + 1, mid + 1, r, p);
  }
} seg;
int L = 1, R;
int s;
int tl[11];
bool check(int p) {
  int v;
  int x = pos[p][0], y = pos[p][1], nx, ny;
  bool res = 1;
  int c = 0;
  for (int i = 0; i < 4; ++i) {
    nx = x + fl[i][0];
    ny = y + fl[i][1];
    v = mp[nx][ny];
    if (v >= L && v <= R) {
      if (lct.connect(p, v)) {
        res = 0;
        break;
      }
      lct.link(p, v);
      c++;
      tl[c] = v;
    }
  }
  if (!res) {
    for (int i = 1; i <= c; ++i) lct.cut(p, tl[i]);
    return 0;
  }
  return 1;
}
void del(int p) {
  int x = pos[p][0], y = pos[p][1], nx, ny, v;
  for (int i = 0; i < 4; ++i) {
    nx = x + fl[i][0], ny = y + fl[i][1];
    v = mp[nx][ny];
    if (v >= L && v <= R)
      if (lct.connect(p, v)) lct.cut(p, v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      scanf("%d", &mp[i][j]);
      pos[mp[i][j]][0] = i;
      pos[mp[i][j]][1] = j;
    }
  long long ans = 0;
  s = n * m;
  int x, y, nx, ny, v;
  pair<int, int> cur;
  seg.build(1, 1, s);
  for (R = 1; R <= s; ++R) {
    while (!check(R)) {
      del(L);
      L++;
    }
    seg.change(1, 1, s, L, R, 1);
    x = pos[R][0], y = pos[R][1];
    for (int i = 0; i < 4; ++i) {
      nx = x + fl[i][0];
      ny = y + fl[i][1];
      v = mp[nx][ny];
      if (v >= L && v <= R) seg.change(1, 1, s, L, v, -1);
    }
    cur = seg.ask(1, 1, s, L, R);
    if (cur.first == 1) ans += cur.second;
  }
  printf("%lld\n", ans);
  return 0;
}