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;
int n, m, total;
namespace LCT {
int sta[200009];
struct data {
  int ch[2], rev, f;
} s[200009];
int get(int x) { return s[s[x].f].ch[1] == x; }
int isrt(int x) { return !(s[s[x].f].ch[0] == x || s[s[x].f].ch[1] == x); }
void rotate(int x) {
  int old = s[x].f, fold = s[old].f, which = get(x);
  if (!isrt(old)) s[fold].ch[s[fold].ch[1] == old] = x;
  s[old].ch[which] = s[x].ch[which ^ 1], s[s[old].ch[which]].f = old;
  s[x].ch[which ^ 1] = old, s[old].f = x, s[x].f = fold;
}
void mark(int x) { swap(s[x].ch[0], s[x].ch[1]), s[x].rev ^= 1; }
void pushdown(int x) {
  if (s[x].rev) {
    if (s[x].ch[0]) mark(s[x].ch[0]);
    if (s[x].ch[1]) mark(s[x].ch[1]);
    s[x].rev = 0;
  }
}
void splay(int x) {
  int u = x, v = 0, fa;
  for (sta[++v] = u; !isrt(u); u = s[u].f) sta[++v] = s[u].f;
  for (; v; --v) pushdown(sta[v]);
  for (u = s[u].f; (fa = s[x].f) != u; rotate(x))
    if (s[fa].f != u) rotate(get(fa) == get(x) ? fa : x);
}
void Access(int x) {
  for (int y = 0; x; y = x, x = s[x].f) {
    splay(x);
    s[x].ch[1] = y;
  }
}
void makert(int x) { Access(x), splay(x), mark(x); }
int findrt(int x) {
  Access(x), splay(x);
  while (s[x].ch[0]) pushdown(x), x = s[x].ch[0];
  return x;
}
void link(int x, int y) { makert(x), makert(y), s[y].f = x; }
void cut(int x, int y) {
  makert(x), Access(y), splay(y);
  s[y].ch[0] = s[x].f = 0;
}
};  // namespace LCT
namespace seg {
struct node {
  int mn, num_mn, sum;
  node() { mn = num_mn = sum = 0; }
  node operator+(const node &b) const {
    node c;
    c.sum = sum + b.sum;
    c.mn = min(b.sum + mn, b.mn);
    if (b.sum + mn == c.mn) c.num_mn += num_mn;
    if (b.mn == c.mn) c.num_mn += b.num_mn;
    return c;
  }
} s[200009 << 2];
void build(int l, int r, int now) {
  s[now].mn = 1000000;
  if (l == r) {
    s[now].mn = s[now].num_mn = s[now].sum = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, now << 1), build(mid + 1, r, now << 1 | 1);
  s[now] = s[now << 1] + s[now << 1 | 1];
}
void update(int l, int r, int now, int p, int v) {
  if (l == r) {
    s[now].sum += v;
    s[now].mn += v;
    s[now].num_mn = 1;
    return;
  }
  int mid = (l + r) >> 1;
  if (p <= mid)
    update(l, mid, now << 1, p, v);
  else
    update(mid + 1, r, now << 1 | 1, p, v);
  s[now] = s[now << 1] + s[now << 1 | 1];
}
node query(int l, int r, int now, int L, int R) {
  if (l >= L && r <= R) return s[now];
  int mid = (l + r) >> 1;
  if (L <= mid && R > mid)
    return query(l, mid, now << 1, L, R) +
           query(mid + 1, r, now << 1 | 1, L, R);
  else if (L <= mid)
    return query(l, mid, now << 1, L, R);
  else
    return query(mid + 1, r, now << 1 | 1, L, R);
}
};  // namespace seg
int l, r;
struct Pos {
  int x, y;
  Pos(int x = 0, int y = 0) : x(x), y(y) {}
} pos[2006 * 2006];
int G[2006][2006];
int go(int x, int y) {
  if (y >= l && y <= r) {
    if (LCT::findrt(x) == LCT::findrt(y))
      return 1;
    else {
      LCT::link(x, y);
      return 0;
    }
  } else
    return 0;
}
void recover(int x, int y) {
  if (y >= l && y <= r) {
    if (LCT::findrt(x) == LCT::findrt(y)) LCT::cut(x, y);
  }
}
int check(int x) {
  int flag = 1;
  int xx = pos[x].x, yy = pos[x].y;
  if (xx - 1 >= 1 && go(x, G[xx - 1][yy])) flag = 0;
  if (xx + 1 <= n && go(x, G[xx + 1][yy])) flag = 0;
  if (yy - 1 >= 1 && go(x, G[xx][yy - 1])) flag = 0;
  if (yy + 1 <= m && go(x, G[xx][yy + 1])) flag = 0;
  recover(x, G[xx - 1][yy]);
  recover(x, G[xx + 1][yy]);
  recover(x, G[xx][yy - 1]);
  recover(x, G[xx][yy + 1]);
  return flag;
}
void update(int x, int y) {
  if (y >= l && y <= r) {
    LCT::cut(x, y);
    seg::update(1, total, 1, x, 1);
  }
}
void Del(int x) {
  int xx = pos[x].x, yy = pos[x].y;
  update(x, G[xx - 1][yy]);
  update(x, G[xx + 1][yy]);
  update(x, G[xx][yy - 1]);
  update(x, G[xx][yy + 1]);
}
void rese(int x, int y) {
  if (y >= l && y <= r) {
    LCT::link(x, y);
    seg::update(1, total, 1, y, -1);
  }
}
void Add(int x) {
  int xx = pos[x].x, yy = pos[x].y;
  rese(x, G[xx - 1][yy]);
  rese(x, G[xx + 1][yy]);
  rese(x, G[xx][yy - 1]);
  rese(x, G[xx][yy + 1]);
}
int main() {
  int i, j;
  long long ans = 0;
  scanf("%d%d", &n, &m), total = n * m;
  seg::build(1, total, 1);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j) scanf("%d", &G[i][j]), pos[G[i][j]] = Pos(i, j);
  for (l = r = 1; r <= total; ++r) {
    while (!check(r)) {
      Del(l), ++l;
    }
    Add(r);
    seg::node p = seg::query(1, total, 1, l, r);
    if (p.mn == 1) ans += (long long)p.num_mn;
  }
  printf("%lld\n", ans);
  return 0;
}