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 maxN = 1e3 + 5;
const int maxNode = 2e5 + 5;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int N, M;
int f[maxN][maxN];
int posx[maxNode], posy[maxNode];
int tr[maxNode];
class LCT {
 private:
  int fa[maxNode];
  int son[maxNode][2];
  bool rev[maxNode];

 public:
  void rotate(int x) {
    int y = fa[x], z = fa[y], i = (son[y][1] == x), c = son[x][i ^ 1];
    fa[x] = z;
    if ((son[fa[y]][0] != (y) && son[fa[y]][1] != (y)) == 0)
      son[z][son[z][1] == y] = x;
    fa[y] = x;
    son[x][i ^ 1] = y;
    son[y][i] = c;
    if (c) fa[c] = y;
  }
  inline void tagrev(int x) {
    swap(son[x][0], son[x][1]);
    rev[x] ^= 1;
  }
  inline void pushdown(int now) {
    if (rev[now]) {
      if (son[now][0]) tagrev(son[now][0]);
      if (son[now][1]) tagrev(son[now][1]);
      rev[now] = 0;
    }
  }
  void splay(int x) {
    static int anc[maxNode];
    int tp = 0, u = x;
    while ((son[fa[u]][0] != (u) && son[fa[u]][1] != (u)) == 0)
      anc[++tp] = u, u = fa[u];
    if (u) anc[++tp] = u;
    while (tp) pushdown(anc[tp--]);
    while ((son[fa[x]][0] != (x) && son[fa[x]][1] != (x)) == 0) rotate(x);
  }
  void access(int x) {
    for (int v = 0; x; v = x, x = fa[x]) {
      splay(x);
      son[x][1] = v;
    }
  }
  inline void makeroot(int x) {
    access(x);
    splay(x);
    tagrev(x);
  }
  inline void link(int x, int y) {
    makeroot(x);
    splay(y);
    fa[x] = y;
  }
  inline void cut(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
    fa[x] = son[y][0] = 0;
  }
  int findroot(int x) {
    while ((son[fa[x]][0] != (x) && son[fa[x]][1] != (x)) == 0) {
      x = fa[x];
    }
    while (son[x][0]) {
      x = son[x][0];
    }
    return x;
  }
  inline bool iscon(int x, int y) {
    makeroot(x), access(y);
    return findroot(y) == x;
  }
} T;
class Segtree {
 private:
  static const int maxNode = ::maxNode << 2;
  class Node {
   public:
    int minval, mintot, lazy;
    Node() {}
    Node(int _minval, int _mintot, int _lazy)
        : minval(_minval), mintot(_mintot), lazy(_lazy) {}
    friend Node operator+(const Node& a, const Node& b) {
      Node ret;
      ret.minval = min(a.minval, b.minval);
      ret.lazy = ret.mintot = 0;
      if (ret.minval == a.minval) {
        ret.mintot += a.mintot;
      }
      if (ret.minval == b.minval) {
        ret.mintot += b.mintot;
      }
      return ret;
    }
  } nd[maxNode];
  inline void pushdown(int now) {
    if (nd[now].lazy) {
      int& cur = nd[now].lazy;
      nd[(now << 1)].lazy += cur, nd[(now << 1)].minval += cur;
      nd[(now << 1 | 1)].lazy += cur, nd[(now << 1 | 1)].minval += cur;
      cur = 0;
    }
  }

 public:
  void build(int now, int l, int r) {
    if (l == r) {
      nd[now].mintot = 1;
      return;
    }
    build((now << 1), l, ((l + r) >> 1)),
        build((now << 1 | 1), ((l + r) >> 1) + 1, r);
    nd[now] = nd[(now << 1)] + nd[(now << 1 | 1)];
  }
  void add(int now, int l, int r, int ql, int qr, int v) {
    if (ql <= l && qr >= r) {
      nd[now].lazy += v;
      nd[now].minval += v;
      return;
    }
    pushdown(now);
    if (ql <= ((l + r) >> 1)) add((now << 1), l, ((l + r) >> 1), ql, qr, v);
    if (qr > ((l + r) >> 1))
      add((now << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, v);
    nd[now] = nd[(now << 1)] + nd[(now << 1 | 1)];
  }
  Node getsum(int now, int l, int r, int ql, int qr) {
    if (ql <= l && qr >= r) {
      return nd[now];
    }
    pushdown(now);
    Node ret = Node(0, 0, 0);
    if (ql <= ((l + r) >> 1)) {
      ret = getsum((now << 1), l, ((l + r) >> 1), ql, qr);
      if (qr > ((l + r) >> 1))
        ret = ret + getsum((now << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr);
    } else if (ql > ((l + r) >> 1)) {
      ret = getsum((now << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr);
    }
    return ret;
  }
  inline int getsum(int l, int r) { return getsum(1, 1, N * M, l, r).mintot; }
} seg;
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= M; ++j) {
      scanf("%d", &f[i][j]), posx[f[i][j]] = i, posy[f[i][j]] = j;
    }
  }
  tr[1] = 1;
  for (int r = 2, l = 1; r <= N * M; ++r) {
    int x = posx[r], y = posy[r];
    vector<int> around;
    around.clear();
    for (int d = 0; d < 4; ++d) {
      int u = f[x + dx[d]][y + dy[d]];
      if (u >= l && u <= r) around.push_back(u);
    }
    for (auto i : around) {
      for (auto j : around)
        if (i < j) {
          while (T.iscon(i, j) && l <= r) {
            for (int d = 0; d < 4; ++d) {
              int u = f[posx[l] + dx[d]][posy[l] + dy[d]];
              if (u >= l && u < r) {
                T.cut(u, l);
              }
            }
            l++;
          }
        }
    }
    tr[r] = l;
    for (auto i : around) {
      T.link(r, i);
    }
  }
  seg.build(1, 1, N * M);
  long long ans = 0;
  for (int r = 1; r <= N * M; ++r) {
    seg.add(1, 1, N * M, tr[r], r, 1);
    for (int d = 0; d < 4; ++d) {
      int u = f[posx[r] + dx[d]][posy[r] + dy[d]];
      if (u >= tr[r] && u <= r) {
        seg.add(1, 1, N * M, tr[r], u, -1);
      }
    }
    ans += seg.getsum(tr[r], r);
  }
  cout << ans << endl;
  return 0;
}