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;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
struct BalancedBinaryForest {
  int n;
  vector<bool> isblack;
  vector<int> blackheight;
  vector<int> lft, rgt, par;
  void init(int _n) {
    n = _n;
    isblack = vector<bool>(n, true);
    blackheight = vector<int>(n, 1);
    lft = rgt = par = vector<int>(n, -1);
  }
  int makeblack(int a) {
    if (a != -1 && !isblack[a]) isblack[a] = true, ++blackheight[a];
    return a;
  }
  int disconnect(int &a) {
    int ret = a;
    a = -1;
    if (ret != -1) par[ret] = -1;
    return ret;
  }
  pair<int, int> split(int a) {
    int l = makeblack(disconnect(lft[a])), r = makeblack(disconnect(rgt[a]));
    while (par[a] != -1) {
      int p = par[a], pl = makeblack(disconnect(lft[p])),
          pr = makeblack(disconnect(rgt[p]));
      par[a] = par[p];
      par[p] = -1;
      if (par[a] != -1) (lft[par[a]] == p ? lft[par[a]] : rgt[par[a]]) = a;
      if (a == pl) r = join3(r, p, pr);
      if (a == pr) l = join3(pl, p, l);
    }
    isblack[a] = true;
    blackheight[a] = 1;
    return make_pair(l, r);
  }
  int rotaterootlft(int a) {
    assert(par[a] == -1);
    int c = rgt[a], d = lft[c];
    rgt[a] = d;
    if (d != -1) par[d] = a;
    lft[c] = a;
    par[a] = c;
    par[c] = -1;
    return c;
  }
  int rotaterootrgt(int c) {
    assert(par[c] == -1);
    int a = lft[c], d = rgt[a];
    lft[c] = d;
    if (d != -1) par[d] = c;
    rgt[a] = c;
    par[c] = a;
    par[a] = -1;
    return a;
  }
  int height(int x) { return x == -1 ? 0 : blackheight[x]; }
  int joinlft(int a, int x, int b) {
    if (height(a) == height(b) && (a == -1 || isblack[a])) {
      isblack[x] = false;
      blackheight[x] = height(a);
      lft[x] = a;
      if (a != -1) par[a] = x;
      rgt[x] = b;
      if (b != -1) par[b] = x;
      return x;
    }
    int r = disconnect(rgt[a]);
    rgt[a] = joinlft(r, x, b);
    par[rgt[a]] = a;
    if (!isblack[a] && !isblack[rgt[a]]) {
      makeblack(a);
      return a;
    }
    if (isblack[a] && isblack[rgt[a]] &&
        blackheight[a] == blackheight[rgt[a]]) {
      assert(!isblack[rgt[rgt[a]]]);
      a = rotaterootlft(a);
      isblack[a] = false;
      isblack[rgt[a]] = true;
      ++blackheight[rgt[a]];
      return a;
    }
    return a;
  }
  int joinrgt(int a, int x, int b) {
    if (height(a) == height(b) && (b == -1 || isblack[b])) {
      isblack[x] = false;
      blackheight[x] = height(a);
      lft[x] = a;
      if (a != -1) par[a] = x;
      rgt[x] = b;
      if (b != -1) par[b] = x;
      return x;
    }
    int l = disconnect(lft[b]);
    lft[b] = joinrgt(a, x, l);
    par[lft[b]] = b;
    if (!isblack[b] && !isblack[lft[b]]) {
      makeblack(b);
      return b;
    }
    if (isblack[b] && isblack[lft[b]] &&
        blackheight[b] == blackheight[lft[b]]) {
      assert(!isblack[lft[lft[b]]]);
      b = rotaterootrgt(b);
      isblack[b] = false;
      isblack[lft[b]] = true;
      ++blackheight[lft[b]];
      return b;
    }
    return b;
  }
  int join3(int a, int x, int b) {
    return makeblack(height(a) >= height(b) ? joinlft(a, x, b)
                                            : joinrgt(a, x, b));
  }
  int join2(int a, int b) {
    if (a == -1)
      return b;
    else if (b == -1)
      return a;
    int c = makeblack(disconnect(lft[b])), d = makeblack(disconnect(rgt[b]));
    return join3(join2(a, c), b, d);
  }
  int merge(int a, int b) { return join2(getroot(a), getroot(b)); }
  int getroot(int a) {
    while (a != -1 && par[a] != -1) a = par[a];
    return a;
  }
};
struct ForestDynamicConnectivity {
  int n;
  BalancedBinaryForest bbf;
  queue<int> pool;
  void init(int _n) {
    n = _n;
    bbf.init(n + 2 * (n - 1));
    for (int i = (n); i < (bbf.n); ++i) pool.push(i);
  }
  int getnode() {
    assert(!pool.empty());
    int ret = pool.front();
    pool.pop();
    return ret;
  }
  void releasenode(int x) { pool.push(x); }
  void reroot(int a) {
    int b, c;
    tie(b, c) = bbf.split(a);
    bbf.merge(a, bbf.merge(c, b));
  }
  pair<int, int> addedge(int a, int b) {
    reroot(a);
    reroot(b);
    int ab = getnode(), ba = getnode();
    bbf.merge(bbf.merge(a, ab), bbf.merge(b, ba));
    return make_pair(ab, ba);
  }
  void remedge(pair<int, int> e) {
    int a, b, c;
    tie(a, b) = bbf.split(e.first);
    if (bbf.getroot(a) == bbf.getroot(e.second)) {
      c = b;
      tie(a, b) = bbf.split(e.second);
    } else {
      tie(b, c) = bbf.split(e.second);
    }
    releasenode(e.first), releasenode(e.second);
    bbf.merge(a, c);
  }
  bool areconnected(int a, int b) { return bbf.getroot(a) == bbf.getroot(b); }
};
struct SegTreeMinCounter {
  int n;
  vector<int> smn, smncnt, slazy;
  void sapply(int x, int by) {
    smn[x] += by;
    slazy[x] += by;
  }
  void spush(int x) {
    if (slazy[x] != 0)
      sapply(2 * x + 1, slazy[x]), sapply(2 * x + 2, slazy[x]), slazy[x] = 0;
  }
  void spull(int x) {
    smn[x] = min(smn[2 * x + 1], smn[2 * x + 2]);
    smncnt[x] = (smn[2 * x + 1] == smn[x] ? smncnt[2 * x + 1] : 0) +
                (smn[2 * x + 2] == smn[x] ? smncnt[2 * x + 2] : 0);
  }
  void sinit(int x, int l, int r) {
    slazy[x] = 0;
    if (l == r) {
      smn[x] = 0, smncnt[x] = 1;
    } else {
      int m = l + (r - l) / 2;
      sinit(2 * x + 1, l, m);
      sinit(2 * x + 2, m + 1, r);
      spull(x);
    }
  }
  void smod(int x, int l, int r, int L, int R, int BY) {
    if (L <= l && r <= R) {
      sapply(x, BY);
    } else {
      int m = l + (r - l) / 2;
      spush(x);
      if (L <= m) smod(2 * x + 1, l, m, L, R, BY);
      if (m + 1 <= R) smod(2 * x + 2, m + 1, r, L, R, BY);
      spull(x);
    }
  }
  int sget(int x, int l, int r, int L, int R, int VAL) {
    if (L <= l && r <= R) {
      assert(smn[x] >= VAL);
      return smn[x] == VAL ? smncnt[x] : 0;
    } else {
      int m = l + (r - l) / 2;
      spush(x);
      int ret = 0;
      if (L <= m) ret += sget(2 * x + 1, l, m, L, R, VAL);
      if (m + 1 <= R) ret += sget(2 * x + 2, m + 1, r, L, R, VAL);
      return ret;
    }
  }
  void init(int _n) {
    n = _n;
    smn = smncnt = slazy = vector<int>(4 * n);
    sinit(0, 0, n - 1);
  }
  void mod(int l, int r, int by) { smod(0, 0, n - 1, l, r, by); }
  int get(int l, int r, int val) { return sget(0, 0, n - 1, l, r, val); }
};
const int MAXH = 1000;
const int MAXW = 1000;
const int MAXN = 200000;
const int DX[] = {-1, 0, +1, 0}, DY[] = {0, +1, 0, -1};
int h, w, n;
int g[MAXH][MAXW];
pair<int, int> pos[MAXN];
ForestDynamicConnectivity conn;
vector<pair<int, int>> connedges[MAXN];
SegTreeMinCounter segtree;
long long solve() {
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y) pos[g[x][y]] = make_pair(x, y);
  conn.init(n);
  segtree.init(n);
  long long ret = 0;
  int l = 0;
  for (int r = (0); r < (n); ++r) {
    int rx = pos[r].first, ry = pos[r].second;
    for (int k = (0); k < (4); ++k) {
      int nx = rx + DX[k], ny = ry + DY[k];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || g[nx][ny] < l ||
          g[nx][ny] > r)
        continue;
      while (conn.areconnected(r, g[nx][ny])) {
        for (int i = (0); i < (((int)(connedges[l]).size())); ++i)
          conn.remedge(connedges[l][i]);
        ++l;
      }
      connedges[g[nx][ny]].push_back(conn.addedge(g[nx][ny], r));
      segtree.mod(0, g[nx][ny], -1);
    }
    segtree.mod(0, r, +1);
    int cur = segtree.get(l, r, 1);
    ret += cur;
  }
  return ret;
}
void run() {
  scanf("%d%d", &h, &w);
  n = h * w;
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y) scanf("%d", &g[x][y]), --g[x][y];
  printf("%lld\n", solve());
}
int main() {
  run();
  return 0;
}