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 vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
class node {
 public:
  int id;
  node* l;
  node* r;
  node* p;
  bool rev;
  node(int id) : id(id) {
    l = r = p = NULL;
    rev = false;
  }
  void apply() { rev ^= 1; }
  void push() {
    if (rev) {
      swap(l, r);
      if (l != NULL) {
        l->apply();
      }
      if (r != NULL) {
        r->apply();
      }
      rev = false;
    }
  }
  void pull() {
    if (l != NULL) {
      l->p = this;
    }
    if (r != NULL) {
      r->p = this;
    }
  }
};
bool is_bst_root(node* v) {
  return (v->p == NULL || (v->p->l != v && v->p->r != v));
}
void rotate(node* v) {
  node* u = v->p;
  v->p = u->p;
  if (v->p != NULL) {
    if (v->p->l == u) {
      v->p->l = v;
    }
    if (v->p->r == u) {
      v->p->r = v;
    }
  }
  if (v == u->l) {
    u->l = v->r;
    v->r = u;
  } else {
    u->r = v->l;
    v->l = u;
  }
  u->pull();
  v->pull();
}
void splay(node* v) {
  stack<node*> st;
  st.push(v);
  for (node* u = v; !is_bst_root(u); u = u->p) {
    st.push(u->p);
  }
  while (!st.empty()) {
    st.top()->push();
    st.pop();
  }
  while (!is_bst_root(v)) {
    node* u = v->p;
    if (!is_bst_root(u)) {
      if ((u->l == v) ^ (u->p->l == u)) {
        rotate(v);
      } else {
        rotate(u);
      }
    }
    rotate(v);
  }
}
void access(node* v) {
  node* u = NULL;
  while (v != NULL) {
    splay(v);
    v->r = u;
    u = v;
    v = v->p;
  }
}
void make_root(node* v) {
  access(v);
  splay(v);
  v->apply();
}
int find_root(node* v) {
  access(v);
  splay(v);
  while (v->push(), v->l != NULL) {
    v = v->l;
  }
  splay(v);
  return v->id;
}
bool link(node* v, node* u) {
  if (find_root(v) == find_root(u)) {
    return false;
  } else {
    make_root(v);
    v->p = u;
    return true;
  }
}
void cut(node* v, node* u) {
  make_root(v);
  access(u);
  splay(u);
  u->l = v->p = NULL;
}
class segtree {
 public:
  class node {
   public:
    int value = 0;
    int cnt = 0;
    int tag = 0;
    void apply(int l, int r, int v) {
      value += v;
      tag += v;
    }
  };
  vector<node> tree;
  int n;
  inline node unite(const node& l, const node& r) {
    node res;
    res.value = min(l.value, r.value);
    if (res.value == l.value) {
      res.cnt += l.cnt;
    }
    if (res.value == r.value) {
      res.cnt += r.cnt;
    }
    return res;
  }
  segtree(int n) : n(n) { tree.resize(n * 2 - 1); }
  inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); }
  inline void push(int x, int l, int r) {
    int y = l + r >> 1, z = x + (y - l + 1 << 1);
    if (tree[x].tag) {
      tree[x + 1].apply(l, y, tree[x].tag);
      tree[z].apply(y + 1, r, tree[x].tag);
      tree[x].tag = 0;
    }
  }
  void build(int x, int l, int r) {
    if (l == r) {
      tree[x].value = l;
      tree[x].cnt = 1;
    } else {
      int y = l + r >> 1, z = x + (y - l + 1 << 1);
      build(x + 1, l, y);
      build(z, y + 1, r);
      pull(x, z);
    }
  }
  template <typename... T>
  void modify(int x, int l, int r, int ll, int rr, const T&... v) {
    if (ll <= l && r <= rr) {
      tree[x].apply(l, r, v...);
    } else {
      int y = l + r >> 1, z = x + (y - l + 1 << 1);
      push(x, l, r);
      if (ll <= y) {
        modify(x + 1, l, y, ll, rr, v...);
      }
      if (rr > y) {
        modify(z, y + 1, r, ll, rr, v...);
      }
      pull(x, z);
    }
  }
  node query(int x, int l, int r, int ll, int rr) {
    if (ll <= l && r <= rr) {
      return tree[x];
    } else {
      int y = l + r >> 1, z = x + (y - l + 1 << 1);
      push(x, l, r);
      if (rr <= y) {
        return query(x + 1, l, y, ll, rr);
      } else if (ll > y) {
        return query(z, y + 1, r, ll, rr);
      } else {
        return unite(query(x + 1, l, y, ll, rr), query(z, y + 1, r, ll, rr));
      }
    }
  }
  void build() { build(0, 0, n - 1); }
  template <typename... T>
  void modify(int l, int r, const T&... v) {
    modify(0, 0, n - 1, l, r, v...);
  }
  node query(int l, int r) { return query(0, 0, n - 1, l, r); }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> board(n, vector<int>(m));
  vector<pair<int, int>> pos(n * m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> board[i][j];
      --board[i][j];
      pos[board[i][j]] = make_pair(i, j);
    }
  }
  vector<node*> tree(n * m);
  for (int i = 0; i < n * m; ++i) {
    tree[i] = new node(i);
  }
  vector<int> right(n * m);
  for (int i = 0, j = 0; i < n * m; ++i) {
    while (j < n * m) {
      int x = pos[j].first, y = pos[j].second;
      vector<int> sons;
      vector<int> roots;
      for (int k = 0; k < 4; ++k) {
        int xx = x + dx[k], yy = y + dy[k];
        if (xx >= 0 && xx < n && yy >= 0 && yy < m && board[xx][yy] < j &&
            board[xx][yy] >= i) {
          sons.push_back(board[xx][yy]);
          roots.push_back(find_root(tree[board[xx][yy]]));
        }
      }
      bool can = true;
      for (int k = 0; k < (int)roots.size(); ++k) {
        for (int l = 0; l < k; ++l) {
          if (roots[k] == roots[l]) {
            can = false;
            break;
          }
        }
        if (!can) {
          break;
        }
      }
      if (can) {
        for (auto k : sons) {
          link(tree[j], tree[k]);
        }
        ++j;
      } else {
        break;
      }
    }
    right[i] = j - 1;
    int x = pos[i].first, y = pos[i].second;
    for (int k = 0; k < 4; ++k) {
      int xx = x + dx[k], yy = y + dy[k];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && board[xx][yy] < j &&
          board[xx][yy] >= i) {
        cut(tree[i], tree[board[xx][yy]]);
      }
    }
  }
  segtree seg(n * m);
  seg.build();
  long long ans = 0;
  for (int i = n * m - 1; ~i; --i) {
    int x = pos[i].first, y = pos[i].second;
    for (int j = 0; j < 4; ++j) {
      int xx = x + dx[j], yy = y + dy[j];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && board[xx][yy] >= i) {
        seg.modify(board[xx][yy], n * m - 1, -1);
      }
    }
    segtree::node res = seg.query(i, right[i]);
    if (res.value == i) {
      ans += res.cnt;
    }
  }
  cout << ans << "\n";
  return 0;
}