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 = 3e5 + 5;
vector<int> adj[N];
int cyc[N], rdeg[N], deg[N];
int par[N], s[N];
vector<int> hist, lst = {0};
inline int qry(int x) { return x == par[x] ? x : qry(par[x]); }
int join(int x, int y) {
  if ((x = qry(x)) == (y = qry(y))) return 1;
  if (s[x] > s[y]) swap(x, y);
  s[y] += s[x], par[x] = y;
  hist.push_back(x);
  return 0;
}
inline void snapshot() { lst.push_back((int)(hist).size()); }
inline void rollback() {
  while ((int)(hist).size() > lst.back()) {
    int x = hist.back();
    s[par[x]] -= s[x], par[x] = x;
    hist.pop_back();
  }
  lst.pop_back();
}
int n;
int itL[N], itR[N];
void solve(int l, int r, int oL = 1, int oR = n) {
  if (l > r) return;
  snapshot();
  int m = (l + r) / 2;
  for (int i = min(r - 1, oL); i >= m; i--)
    for (int j = itL[i]; j < (int)(adj[i]).size() && adj[i][j] <= oL; j++)
      join(i, adj[i][j]);
  cyc[m] = oR;
  for (int i = max(m, oL + 1); i <= oR; i++) {
    int ok = 0;
    for (int j = itR[i]; j >= 0 && ok == 0 && adj[i][j] >= m; j--)
      ok += join(i, adj[i][j]);
    if (ok) {
      cyc[m] = i - 1;
      break;
    }
  }
  rollback();
  snapshot();
  for (int i = min(r - 1, oL); i >= m - 1; i--)
    for (int j = itL[i]; j < (int)(adj[i]).size() && adj[i][j] <= oL; j++)
      join(i, adj[i][j]);
  solve(l, m - 1, oL, cyc[m]);
  rollback();
  snapshot();
  for (int i = max(r, oL + 1); i <= cyc[m]; i++)
    for (int j = itR[i]; j >= 0 && adj[i][j] >= r; j--) join(i, adj[i][j]);
  solve(m + 1, r, cyc[m], oR);
  rollback();
}
pair<int, int> seg[N * 4];
int lz[N * 4];
void build(int l, int r, int k) {
  if (l == r) {
    seg[k] = make_pair(l, 1);
    return;
  }
  int m = (l + r) / 2;
  build(l, m, k * 2);
  build(m + 1, r, k * 2 + 1);
  seg[k] = min(seg[k * 2], seg[k * 2 + 1]);
  if (seg[k * 2].first == seg[k * 2 + 1].first)
    seg[k].second = seg[k * 2].second + seg[k * 2 + 1].second;
}
void upd(int l, int r, int k, int x, int y, int v) {
  if (y < l || r < x) return;
  if (x <= l && r <= y) {
    lz[k] += v;
    seg[k].first += v;
    return;
  }
  int m = (l + r) / 2;
  upd(l, m, k * 2, x, y, v);
  upd(m + 1, r, k * 2 + 1, x, y, v);
  seg[k] = min(seg[k * 2], seg[k * 2 + 1]);
  if (seg[k * 2].first == seg[k * 2 + 1].first)
    seg[k].second = seg[k * 2].second + seg[k * 2 + 1].second;
  seg[k].first += lz[k];
}
pair<int, int> qry(int l, int r, int k, int x, int y) {
  if (y < l || r < x) return make_pair(N, 1);
  if (x <= l && r <= y) return seg[k];
  int m = (l + r) / 2;
  pair<int, int> lx = qry(l, m, k * 2, x, y);
  pair<int, int> rx = qry(m + 1, r, k * 2 + 1, x, y);
  if (lx.first == rx.first)
    lx.second += rx.second;
  else if (lx.first > rx.first)
    swap(lx, rx);
  lx.first += lz[k];
  return lx;
}
int dx[4] = {0, 1};
int dy[4] = {1, 0};
int a[1005][1005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int _n, _m;
  cin >> _n >> _m;
  n = _n * _m;
  auto get_index = [&](int i, int j) { return a[i][j]; };
  for (int i = 1; i <= _n; i++)
    for (int j = 1; j <= _m; j++) cin >> a[i][j];
  for (int i = 1; i <= _n; i++) {
    for (int j = 1; j <= _m; j++) {
      for (int k = 0; k < 2; k++) {
        int ni = i + dx[k], nj = j + dy[k];
        if (!ni || !nj || ni > _n || nj > _m) continue;
        adj[get_index(i, j)].push_back(get_index(ni, nj));
        adj[get_index(ni, nj)].push_back(get_index(i, j));
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    par[i] = i, s[i] = 1;
    sort((adj[i]).begin(), (adj[i]).end());
    itL[i] = lower_bound((adj[i]).begin(), (adj[i]).end(), i) - adj[i].begin();
    itR[i] =
        upper_bound((adj[i]).begin(), (adj[i]).end(), i) - adj[i].begin() - 1;
  }
  solve(1, n);
  build(1, n, 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = itL[i]; j < (int)(adj[i]).size(); j++)
      upd(1, n, 1, adj[i][j], n, -1);
  for (int i = 1, j = 1; i <= n; i++) {
    ans += qry(1, n, 1, i, cyc[i]).second;
    for (int j = itL[i]; j < (int)(adj[i]).size(); j++)
      upd(1, n, 1, adj[i][j], n, +1);
    upd(1, n, 1, 1, n, -1);
  }
  cout << ans << "\n";
}