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>
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
  unsigned long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int myrand() { return abs((int)mt()); }
struct node_t {
  node_t *p, *l, *r;
  int size, rev;
  int s, w;
  node_t() : p(0), l(0), r(0), size(1), rev(0), s(0), w(1) {}
};
int isrt(node_t* x) { return !(x->p) || (x->p->l != x && x->p->r != x); }
int left(node_t* x) { return x->p->l == x; }
void setchild(node_t* x, node_t* p, int l) {
  (l ? p->l : p->r) = x;
  if (x) x->p = p;
}
void push(node_t* x) {
  node_t* u = x->l;
  node_t* v = x->r;
  if (x->rev) {
    if (u) swap(u->l, u->r), u->rev ^= 1;
    if (v) swap(v->l, v->r), v->rev ^= 1;
    x->rev = 0;
  }
}
int size(node_t* x) { return x ? x->size : 0; }
int sum(node_t* x) { return x ? x->s : 0; }
void pull(node_t* x) {
  x->size = size(x->l) + 1 + size(x->r);
  x->s = sum(x->l) + x->w + sum(x->r);
}
void rotate(node_t* x) {
  node_t *p = x->p, *g = p->p;
  int l = left(x);
  setchild(l ? x->r : x->l, p, l);
  if (!isrt(p))
    setchild(x, g, left(p));
  else
    x->p = g;
  setchild(p, x, !l);
  pull(p);
}
node_t* splay(node_t* x) {
  push(x);
  while (!isrt(x)) {
    node_t *p = x->p, *g = p->p;
    if (g) push(g);
    push(p), push(x);
    if (!isrt(p)) rotate(left(x) != left(p) ? x : p);
    rotate(x);
  }
  pull(x);
  return x;
}
node_t* access(node_t* x) {
  node_t* z = 0;
  for (node_t* y = x; y; y = y->p) {
    splay(y);
    y->w += sum(y->r);
    y->r = z;
    y->w -= sum(y->r);
    pull(z = y);
  }
  splay(x);
  return z;
}
void link(node_t* x, node_t* p) {
  access(x), access(p);
  x->p = p;
  p->w += sum(x);
}
void cut(node_t* x) {
  access(x);
  x->l->p = 0, x->l = 0;
  pull(x);
}
void makeroot(node_t* x) {
  access(x);
  x->rev ^= 1;
  swap(x->l, x->r);
}
node_t* findroot(node_t* x) {
  access(x);
  while (x->l) push(x), x = x->l;
  push(x);
  return splay(x);
}
node_t* lca(node_t* x, node_t* y) {
  if (findroot(x) != findroot(y)) return 0;
  access(x);
  return access(y);
}
int connect(node_t* x, node_t* y) {
  if (x == y) return 1;
  access(x), access(y);
  return x->p != 0;
}
int treequery(node_t* x) {
  access(x);
  return x->w;
}
const int maxn = 2e5 + 5;
int n, m;
vector<vector<int> > a;
node_t pool[maxn];
void makeroot(int u) { makeroot(pool + u); }
void link(int u, int v) { link(pool + u, pool + v); }
void cut(int u) { cut(pool + u); }
int findroot(int u) { return findroot(pool + u) - pool; }
int lca(int u, int v) {
  node_t* a = lca(pool + u, pool + v);
  if (!a) return -1;
  return a - pool;
}
int connect(int u, int v) { return connect(pool + u, pool + v); }
int treequery(int u) { return treequery(pool + u); }
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int inside(int u, int v) {
  if (!(0 <= u && u < n)) return 0;
  if (!(0 <= v && v < m)) return 0;
  return 1;
}
pair<int, int> merge(pair<int, int> a, pair<int, int> b) {
  pair<int, int> res = make_pair(min(a.first, b.first), 0);
  if (res.first == a.first) {
    res.second += a.second;
  }
  if (res.first == b.first) {
    res.second += b.second;
  }
  return res;
}
pair<int, int> st[maxn << 2];
int lz[maxn << 2];
void build(int p, int L, int R) {
  if (L == R) {
    st[p] = make_pair(0, 1);
    return;
  }
  build(p << 1, L, L + R >> 1);
  build(p << 1 | 1, (L + R >> 1) + 1, R);
  st[p] = merge(st[p << 1], st[p << 1 | 1]);
}
void push(int p, int L, int R) {
  if (lz[p]) {
    st[p].first += lz[p];
    if (L < R) {
      for (int i = (0); i < (2); ++i) {
        lz[p << 1 | i] += lz[p];
      }
    }
    lz[p] = 0;
  }
}
void upd(int p, int l, int r, int L, int R, int val) {
  push(p, L, R);
  if (R < l || r < L) return;
  if (l <= L && R <= r) {
    lz[p] = val;
    push(p, L, R);
    return;
  }
  upd(p << 1, l, r, L, L + R >> 1, val);
  upd(p << 1 | 1, l, r, (L + R >> 1) + 1, R, val);
  st[p] = merge(st[p << 1], st[p << 1 | 1]);
}
pair<int, int> query(int p, int l, int r, int L, int R) {
  push(p, L, R);
  if (R < l || r < L) return make_pair(INF, 1);
  if (l <= L && R <= r) {
    return st[p];
  }
  return merge(query(p << 1, l, r, L, L + R >> 1),
               query(p << 1 | 1, l, r, (L + R >> 1) + 1, R));
}
void chemthan() {
  cin >> n >> m;
  a.resize(n, vector<int>(m));
  vector<pair<int, int> > from(n * m + 1);
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (m); ++j)
      cin >> a[i][j], from[a[i][j]] = make_pair(i, j);
  build(1, 1, n * m);
  long long res = 0;
  int ptr = 1;
  vector<vector<int> > adj(n * m + 1);
  for (int i = (1); i < (n * m + 1); ++i) {
    int x = from[i].first;
    int y = from[i].second;
    upd(1, 1, i, 1, n * m, 1);
    for (int d = (0); d < (4); ++d) {
      int nx = x + dx[d];
      int ny = y + dy[d];
      vector<int> vals;
      if (inside(nx, ny) && ptr <= a[nx][ny] && a[nx][ny] < i) {
        vals.push_back(a[nx][ny]);
      }
      sort((vals).begin(), (vals).end()), reverse((vals).begin(), (vals).end());
      for (int x : vals) {
        while (ptr <= x && findroot(x) == i) {
          if (findroot(ptr) == i) {
            cut(ptr);
          } else {
            for (int y : adj[ptr]) {
              makeroot(y);
              if (findroot(ptr) == y) {
                cut(ptr);
              }
            }
          }
          ptr++;
        }
        if (ptr <= x) {
          makeroot(x);
          link(x, i);
          adj[x].push_back(i);
          upd(1, ptr, x, 1, n * m, -1);
        }
      }
    }
    res += query(1, ptr, i, 1, n * m).second;
  }
  cout << res << "\n";
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(0), cin.tie(0);
  if (argc > 1) {
    assert(freopen(argv[1], "r", stdin));
  }
  if (argc > 2) {
    assert(freopen(argv[2], "wb", stdout));
  }
  chemthan();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
