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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")
 
#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2
#define v(x) (int)(x - 'a')
 
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
 
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;
 
template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;

namespace Check {
    using namespace std;
    const int N=200005,dx[]={1,0,-1,0},dy[]={0,1,0,-1};
    int n,m,tot,L=1,R=1,f[1005][1005],px[N],py[N],fa[N],ch[N][2],rev[N],ps[10],tg[N<<2],left[N];
    struct st{int v,s;}T[N<<2];ll ans=1;
    inline int wh(int x){return ch[fa[x]][1]==x;}
    inline int isrt(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
    void rot(int x)
    {
    	int y=fa[x],z=fa[y],c=wh(x);
    	ch[y][c]=ch[x][c^1];if(ch[y][c])fa[ch[y][c]]=y;
    	fa[x]=z;if(!isrt(y))ch[z][wh(y)]=x;
    	ch[x][c^1]=y;fa[y]=x;
    }
    inline void rever(int x){swap(ch[x][0],ch[x][1]);rev[x]^=1;}
    inline void dnn(int x){if(rev[x])rever(ch[x][0]),rever(ch[x][1]),rev[x]=0;}
    inline void psh(int x){if(!isrt(x))psh(fa[x]);dnn(x);}
    inline void splay(int x){psh(x);for(int y=fa[x];!isrt(x);rot(x),y=fa[x])if(!isrt(y))wh(x)^wh(y)?rot(x):rot(y);}
    inline void access(int x){for(int y=0;x;x=fa[y=x])splay(x),ch[x][1]=y;}
    inline void mkrt(int x){access(x);splay(x);rever(x);}
    inline int fndrt(int x){access(x);splay(x);while(ch[x][0])x=ch[x][0];splay(x);return x;} 
    inline void split(int x,int y){mkrt(x);access(y);splay(y);}
    inline void link(int x,int y){mkrt(x);fa[x]=y;}
    inline void cut(int x,int y){split(x,y);fa[x]=ch[y][0]=0;}
    inline bool chk()
    {
    	int len=0;
    	for(int d=0;d<4;d++)
    	{
    		int x=px[R+1]+dx[d],y=py[R+1]+dy[d];
    		if(!x||x==n+1||!y||y==m+1||f[x][y]<L||f[x][y]>R)continue;
    		ps[++len]=fndrt(f[x][y]);
    	}
    	sort(ps+1,ps+len+1);
    	for(int i=1;i<len;i++)if(ps[i]==ps[i+1])return 0;
    	return 1;
    }
    inline st operator+(st a,st b){return (st){min(a.v,b.v),(a.v<=b.v?a.s:0)+(b.v<=a.v?b.s:0)};}
    inline void cov(int x,int v){T[x].v+=v;tg[x]+=v;}
    inline void dn(int x){if(tg[x])cov(x<<1,tg[x]),cov(x<<1|1,tg[x]),tg[x]=0;}
    void build(int x,int l,int r)
    {
    	if(l==r){T[x]=(st){0,1};return;}
    	int mid=l+r>>1;build(x<<1,l,mid);build(x<<1|1,mid+1,r);T[x]=T[x<<1]+T[x<<1|1];
    }
    void upd(int x,int l,int r,int tl,int tr,int v)
    {
    	if(tl<=l&&r<=tr){cov(x,v);return;}
    	int mid=l+r>>1;dn(x);
    	if(tl<=mid)upd(x<<1,l,mid,tl,tr,v);
    	if(tr>mid)upd(x<<1|1,mid+1,r,tl,tr,v);
    	T[x]=T[x<<1]+T[x<<1|1];
    }
    st qry(int x,int l,int r,int tl,int tr)
    {
    	if(tl<=l&&r<=tr)return T[x];
    	int mid=l+r>>1;dn(x);
    	if(tr<=mid)return qry(x<<1,l,mid,tl,tr);
    	if(tl>mid)return qry(x<<1|1,mid+1,r,tl,tr);
    	return qry(x<<1,l,mid,tl,tr)+qry(x<<1|1,mid+1,r,tl,tr);
    }
    void check_main()
    {
    	scanf("%d%d",&n,&m);tot=n*m;
    	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&f[i][j]),px[f[i][j]]=i,py[f[i][j]]=j;
    	build(1,1,tot);upd(1,1,tot,1,1,1);
    	while(R<tot)
    	{
    		for(;L<R&&!chk();L++)for(int d=0;d<4;d++)
    		{
    			int x=px[L]+dx[d],y=py[L]+dy[d];
    			if(!x||x==n+1||!y||y==m+1||f[x][y]<L||f[x][y]>R)continue;
    			cut(L,f[x][y]);
    		}
    		R++;upd(1,1,tot,L,R,1);
    		for(int d=0;d<4;d++)
    		{
    			int x=px[R]+dx[d],y=py[R]+dy[d];
    			if(!x||x==n+1||!y||y==m+1||f[x][y]<L||f[x][y]>R)continue;
    			link(R,f[x][y]);upd(1,1,tot,L,f[x][y],-1);
    		}
            left[R] = L;
    		st res=qry(1,1,tot,L,R);ans+=res.v==1?res.s:0;
    	}
    }
};
 
const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 200005;
const int M = 1001;

int n, m, temp[N], mat[M][M], mleft[N];

pii pos[N], adj[4] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};

struct Node {
    int l, r;
    int lzy;
    pii mnm;
} node[N << 2];

struct Splay {
    int flip;
    int parent;
    int child[2];
} nodes[N];

int side(int pnode, int node) {
    return nodes[pnode].child[0] == node? 0: (nodes[pnode].child[1] == node? 1: -1);
}

void attach(int pnode, int node, int dir) {

    if (pnode && dir != -1) {
        nodes[pnode].child[dir] = node;
    }
    if (node) {
        nodes[node].parent = pnode;
    }
}

void rotate(int node) {

    int pnode = nodes[node].parent, gnode = nodes[pnode].parent;
    int pdir = side(pnode, node), gdir = side(gnode, pnode);

    attach(gnode, node, gdir);
    attach(pnode, nodes[node].child[!pdir], pdir);
    attach(node, pnode, !pdir);
}

void reverse(int node) {

    swap(nodes[node].child[0], nodes[node].child[1]);
    nodes[node].flip ^= 1;
}

void push(int node) {

    if (nodes[node].flip) {
        nodes[node].flip = 0;
        
        reverse(nodes[node].child[0]);
        reverse(nodes[node].child[1]);
    }
}

void splay(int node) {

    static int ind = 0;

    int tnode = node;
    temp[++ind] = tnode;

    while (side(nodes[tnode].parent, tnode) != -1) {

        tnode = nodes[tnode].parent;
        temp[++ind] = tnode;
    }
    while (ind) {
        push(temp[ind--]);
    }
    while (side(nodes[node].parent, node) != -1) {

        int pnode = nodes[node].parent, gnode = nodes[pnode].parent;

        if (side(gnode, pnode) != -1) {
            side(pnode, node) == side(gnode, pnode)? rotate(pnode): rotate(node);
        }
        rotate(node);
    }
}

void access(int node) {

    int current = 0, parent = node;
    while (parent) {

        splay(parent);
        attach(parent, current, 1);

        current = parent;
        parent = nodes[parent].parent;
    }
    splay(node);
}

void reroot(int node) {
    access(node);
    reverse(node);
}

void link(int u, int v) {

    reroot(u);
    access(v);

    nodes[u].parent = v;
}

void cut(int u, int v) {

    reroot(u);
    access(v);

    nodes[v].child[0] = 0;
    nodes[u].parent = 0;
}

int root(int node) {

    access(node);
    while (nodes[node].child[0]) {
        node = nodes[node].child[0];
    }
    splay(node);
    return node;
}

pii operator + (pii a, pii b) {

    if (a.xx == b.xx) {
        return {a.xx, a.yy + b.yy};
    } else {
        return min(a, b);
    }
}

void lazy(int id) {

    int lzy = node[id].lzy;

    if (node[id].l != node[id].r) {
        node[l(id)].lzy += lzy;
        node[r(id)].lzy += lzy;
    }
    node[id].mnm.xx += lzy;
    node[id].lzy = 0;
}

void merge(int id, int l, int r) {

    lazy(l), lazy(r);
    node[id].mnm = node[l].mnm + node[r].mnm;
}

void build(int id, int l, int r) {

    node[id].l = l;
    node[id].r = r;
    node[id].mnm.xx = 0;
    node[id].mnm.yy = r - l + 1;

    if (l == r) {
        return;
    }
    int q = l + r >> 1;
    build(l(id), l, q);
    build(r(id), q + 1, r);
}

void update(int id, int l, int r, int x) {

    lazy(id);
    if (node[id].l == l && node[id].r == r) {
        node[id].lzy += x;
        return;
    }
    int q = node[id].l + node[id].r >> 1;

    if (l <= q)
        update(l(id), l, min(q, r), x);
    if (r > q)
        update(r(id), max(q + 1, l), r, x);

    merge(id, l(id), r(id));
}

int query(int id, int l, int r) {

    lazy(id);
    if (node[id].l == l && node[id].r == r) {
        return node[id].mnm.xx == 1? node[id].mnm.yy: 0;
    }
    int q = node[id].l + node[id].r >> 1, res = 0;

    if (l <= q)
        res += query(l(id), l, min(q, r));
    if (r > q)
        res += query(r(id), max(q + 1, l), r);

    return res;
}

void input()
{
    n = Check::n;
    m = Check::m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            
            mat[i][j] = Check::f[i][j];
            pos[mat[i][j]] = {i, j};
        }
    }
}

void calc_mleft(int i) {
        
    mleft[i] = mleft[i - 1];

    int x = pos[i].xx;
    int y = pos[i].yy;

    bool cycle = true;

    while (cycle) {

        vec <int> links;
        cycle = false;

        for (pii p: adj) {

            int xa = x + p.xx;
            int ya = y + p.yy;
            if (xa == 0 || ya == 0 || xa > n || ya > m) {
                continue;
            }
            if (mat[xa][ya] < i && mat[xa][ya] >= mleft[i]) {

                if (root(mat[xa][ya]) == root(i)) {

                    for (pii p: adj) {

                        int xb = pos[mleft[i]].xx + p.xx;
                        int yb = pos[mleft[i]].yy + p.yy;
                        if (xb == 0 || yb == 0 || xb > n || yb > m) {
                            continue;
                        }
                        if (root(mleft[i]) == root(mat[xb][yb])) {
                            cut(mleft[i], mat[xb][yb]);
                        }
                    }
                    mleft[i]++;
                    cycle = true;
                    break;

                } else {
                    link(mat[xa][ya], i);
                    links.pb(mat[xa][ya]);
                }
            }
        }
        for (int vert: links) {
            cut(i, vert);
        }
    }
}

void solve() {

    build(0, 1, n * m);

    ll ans = 0;

    mleft[0] = 1;
    for (int i = 1; i <= n * m; ++i) {

        calc_mleft(i);
        update(0, 1, i, 1);

        int x = pos[i].xx;
        int y = pos[i].yy;
        for (pii p: adj) {

            int xa = x + p.xx;
            int ya = y + p.yy;
            if (xa == 0 || ya == 0 || xa > n || ya > m) {
                continue;
            }
            if (mat[xa][ya] < i && mat[xa][ya] >= mleft[i]) {
                update(0, 1, mat[xa][ya], -1);
                link(mat[xa][ya], i);
            }
        }
        ans += query(0, mleft[i], i);
    }
    for (int i = 2; i <= n * m; ++i) {
        if (Check::left[i] != mleft[i]) {
            cout << i << ": " << Check::left[i] << " " << mleft[i] << "\n";
        }
    }
    cout << ans << "\n";
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    Check::check_main();

    input();
    solve();
}

/*
2 3
2 1 3
4 5 6
*/