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;
using nagai = long long;
const int N = 1 << 18;
int fa[N], sz[N];
int flag[N];
int ch[N][2];
int chnum(int x) { return ch[fa[x]][1] == x; }
int is_r(int x) { return !(ch[fa[x]][1] == x || ch[fa[x]][0] == x); }
void push(int x) {
  if (flag[x]) {
    swap(ch[x][0], ch[x][1]);
    flag[ch[x][0]] ^= 1, flag[ch[x][1]] ^= 1;
    flag[x] = 0;
  }
}
void rev(int x) {
  flag[x] = true;
  push(x);
}
void update(int x) {
  if (x) sz[x] = 1 + sz[ch[x][0]] + sz[ch[x][1]];
}
void attach(int x, int y, int side) {
  fa[x] = y;
  ch[y][side] = x;
}
void check(int x) { assert(sz[x] == sz[ch[x][0]] + sz[ch[x][1]] + 1); }
void rotate(int x) {
  int y = fa[x], z = fa[y], b = chnum(x), t = ch[x][b ^ 1];
  int c = chnum(fa[x]);
  if (!is_r(y))
    attach(x, z, c);
  else
    fa[x] = z;
  attach(y, x, b ^ 1);
  attach(t, y, b);
  update(y);
  update(x);
}
void splay(int x) {
  static int mem[N];
  static int ptr = 0;
  int y = x;
  while (!is_r(y)) mem[ptr++] = y, y = fa[y];
  mem[ptr++] = y;
  while (ptr > 0) push(mem[ptr - 1]), --ptr;
  while (!is_r(x)) {
    int y = fa[x];
    if (!is_r(fa[x])) rotate(chnum(x) == chnum(y) ? y : x);
    rotate(x);
  }
}
void print(int x) {
  if (!x) return;
  check(x);
  print(ch[x][0]);
  cerr << x << ' ';
  print(ch[x][1]);
}
void access(int x) {
  for (int dwn = 0; x; dwn = x, x = fa[x]) splay(x), ch[x][1] = dwn, update(x);
}
void makeroot(int x) { access(x), splay(x), rev(x); }
bool is_conn(int x, int y) {
  if (x == y) return 1;
  makeroot(x);
  makeroot(y);
  return !(fa[x] == 0 && fa[y] == 0);
}
void cut(int x, int y) {
  makeroot(x);
  access(y);
  splay(y);
  assert(ch[y][0] == x);
  ch[y][0] = fa[x] = 0;
  update(y);
}
void link(int x, int y) {
  makeroot(x);
  makeroot(y);
  assert(fa[x] == 0);
  fa[x] = y;
}
int n, m;
const int S = 1010;
int matr[S][S];
int nocyc[N];
int kek(pair<int, int> mem) { return mem.first * m + mem.second + 1; }
vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
bool added[N];
vector<int> neigh(int a) {
  --a;
  int x = a / m, y = a % m;
  vector<int> ans;
  for (int i = 0; i < 4; ++i) {
    int x1 = x + dx[i], y1 = y + dy[i];
    if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m && added[x1 * m + y1 + 1])
      ans.push_back(x1 * m + y1 + 1);
  }
  return ans;
}
bool check(pair<int, int> a) {
  int x = kek(a);
  auto ne = neigh(x);
  for (int i = 0; i < ne.size(); ++i)
    for (int j = 0; j < i; ++j)
      if (is_conn(ne[i], ne[j])) return false;
  return true;
}
void add(pair<int, int> a) {
  int x = kek(a);
  added[x] = true;
  for (int y : neigh(x)) {
    assert(!is_conn(x, y));
    link(x, y);
  }
}
void rem(pair<int, int> a) {
  int x = kek(a);
  added[x] = false;
  for (int y : neigh(x)) {
    assert(is_conn(x, y));
    cut(x, y);
  }
}
const int oo = 1e9;
int C = 1;
struct it {
  int sum[2 * N], mn[2 * N], mn1[2 * N], cntmn[2 * N], cntmn1[2 * N];
  it() {
    for (int i = 2 * N - 1; i >= 0; --i) {
      if (i < n)
        update(i);
      else
        sum[i] = 0, mn[i] = 0, mn1[i] = oo, cntmn[i] = 1, cntmn1[i] = 0;
    }
  }
  void update(int i) {
    int le = 2 * i;
    int ri = 2 * i + 1;
    sum[i] = sum[le] + sum[ri];
    int kek[4] = {mn[le], mn1[le], sum[le] + mn[ri], sum[le] + mn1[ri]};
    mn[i] = oo;
    mn1[i] = oo;
    for (int x : kek)
      if (x < mn[i])
        mn1[i] = mn[i], mn[i] = x;
      else if (x != mn[i] && x < mn1[i])
        mn1[i] = x;
    cntmn[i] = 0;
    cntmn1[i] = 0;
    if (mn[le] == mn[i]) cntmn[i] += cntmn[le];
    if (mn[le] == mn1[i])
      cntmn1[i] += cntmn[le];
    else if (mn1[le] == mn1[i])
      cntmn1[i] += cntmn1[le];
    if (sum[le] + mn[ri] == mn[i]) cntmn[i] += cntmn[ri];
    if (sum[le] + mn[ri] == mn1[i])
      cntmn1[i] += cntmn[ri];
    else if (sum[le] + mn1[ri] == mn1[i])
      cntmn1[i] += cntmn1[ri];
  }
  void add(int i, int ch) {
    i += N;
    sum[i] += ch;
    mn[i] = sum[i];
    mn1[i] = sum[i];
    cntmn[i] = 1;
    cntmn1[i] = 1;
    while (i > 1) update(i >> 1), i >>= 1;
  }
  int get(int x, int l, int r, int qr, int cur) {
    if (cur + mn[x] > C) return 0;
    if (l >= qr) return 0;
    if (r <= qr)
      if (cur + mn[x] == C)
        return cntmn[x];
      else {
        if (cur + mn1[x] == 1) return cntmn1[x];
        return 0;
      }
    int m = (l + r) / 2;
    return get(x * 2, l, m, qr, cur) +
           get(x * 2 + 1, m, r, qr, cur + sum[x * 2]);
  }
} myit;
mt19937 rnd(228);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  n = 1000, m = 200;
  cin >> n >> m;
  vector<int> kekos(n * m);
  iota(kekos.begin(), kekos.end(), 1);
  shuffle(kekos.begin(), kekos.end(), rnd);
  for (int i = 0; i < n * m; ++i) sz[i] = 1;
  int t = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      matr[i][j] = kekos[t++];
      cin >> matr[i][j];
      --matr[i][j];
    }
  vector<pair<int, int>> ord;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) ord.emplace_back(i, j);
  sort(ord.begin(), ord.end(), [&](pair<int, int> a, pair<int, int> b) {
    return matr[a.first][a.second] < matr[b.first][b.second];
  });
  int cur = ord.size();
  for (int i = ord.size() - 1; i >= 0; --i) {
    while (!check(ord[i])) rem(ord[--cur]);
    nocyc[i] = cur;
    add(ord[i]);
  }
  memset(added, 0, sizeof added);
  nagai ansich = 0;
  for (int i = ord.size() - 1; i >= 0; --i) {
    int x = ord[i].first, y = ord[i].second;
    myit.add(i, 1);
    for (int k = 0; k < 4; ++k) {
      int x1 = x + dx[k], y1 = y + dy[k];
      if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m && added[matr[x1][y1]])
        myit.add(matr[x1][y1], -1);
    }
    added[i] = true;
    int mem = myit.get(1, 0, N, nocyc[i], 0);
    ansich += mem;
  }
  cout << ansich << '\n';
}