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>
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class F2 {

	static int R, C;
	static int b[][];
	static Pair ps[];
	static int dr[] = new int[] { -1, 1, 0, 0 };
	static int dc[] = new int[] { 0, 0, -1, 1 };
	
	static Node nodes[][];
	
	static int pL,pR;
	
	public static void main(String[] args) {
		FS in = new FS();
		R = in.nextInt();
		C = in.nextInt();
		b = new int[R][C];
		for (int i = 0; i < R; i++)
			for (int j = 0; j < C; j++)
				b[i][j] = in.nextInt() - 1;

		ps = new Pair[R * C];
		for (int i = 0; i < R; i++)
			for (int j = 0; j < C; j++)
				ps[b[i][j]] = new Pair(i, j);

		nodes = new Node[R][C];
		for(int i = 0; i < R; i++)
			for(int j = 0; j < C; j++)
				nodes[i][j] = new Node(-1);
		
		long res = 0;
		
		pR = -1;
		int nComps = 0;
		ST st = new ST(0, R*C);
		for(pL = 0; pL < R*C; pL++) {
			// add until we are about to make a non-tree
			while(true) {
				boolean good = pR+1 < R*C;
				if(good) {
					Pair p = ps[pR+1];
					Pair a, b;
					for(int k1 = 0; k1 < 4; k1++)
						for(int k2 = k1+1; k2 < 4; k2++)
							if((a = p.to(k1)).v() && (b = p.to(k2)).v() && connected(a.n(), b.n())) good = false;
							
				}
				if(good) {
					Pair p = ps[++pR];
					Pair a;
					nComps++; // add my guy
					for(int k = 0; k < 4; k++) {
						if((a = p.to(k)).v()) {
							Node n1 = p.n(), n2 = a.n();
							if(!connected(n1, n2)) {
								link(n1,n2);
								nComps--;
							}
						}
					}
					st.add(pR, pR, nComps);
				}
				else break;
			}
			
			int x[] = st.q(pL, pR);
			int nOnes = (x[0] == 1 ? x[1] : 0);
			
			res += nOnes;
			
			// remove this guy
			// get min first
			int first = pR+1;
			for(int k = 0; k < 4; k++) {
				Pair a = ps[pL].to(k);
				if(a.v()) first = min(first, b[a.r][a.c]);
			}
			st.add(pL, first-1, -1);
			
			nComps--;
			for(int k = 0; k < 4; k++) {
				Pair a = ps[pL].to(k);
				if(!a.v()) continue;
				// Guarenteed they are connected at this stage
				cut(ps[pL].n(), a.n());
				nComps++;
				if(b[a.r][a.c] != first) st.add(b[a.r][a.c], pR, 1);
			}
			
//			System.out.println(l+" "+r+" nComps = "+nComps+" nOnes = "+nOnes);
		}
		
		
		System.out.println(res);
	}
	
	static class ST {
		ST left, right;
		int l, r, m, mc, del;
		public ST(int ll, int rr) {
			l=ll;
			r=rr;
			m = del = 0;
			mc = (r-l+1);
			if(l == r) return;
			int mid = (l+r)>>1;
			left = new ST(l, mid);
			right = new ST(mid+1, r);
		}
		
		void set(int v) {
			del += v;
			m += v;
		}
		void prop() {
			left.set(del);
			right.set(del);
			del = 0;
		}
		void update() {
			m = min(left.m, right.m);
			mc = 0;
			if(left.m == m) mc += left.mc;
			if(right.m == m) mc += right.mc;
		}
		void add(int ll, int rr, int v) {
			if(ll <= l && r <= rr) {
				set(v); return;
			}
			if(l > rr || r < ll) return;
			prop();
			left.add(ll, rr, v);
			right.add(ll, rr, v);
			update();
		}
		
		int[] q(int ll, int rr) {
			if(ll <= l && r <= rr) return new int[] {m, mc};
			if(l > rr || r < ll) return null;
			prop();
			int x[] = left.q(ll, rr);
			int y[] = right.q(ll, rr);
			if(x == null) return y;
			else if(y == null) return x;
			else if(x[0] < y[0]) return x;
			else if(x[0] > y[0]) return y;
			else return new int[] {x[0], x[1] + y[1]};
		}
	}
	static int min(int a, int b) { return a < b ? a : b;}

	static class Pair {
		int r, c;

		public Pair(int rr, int cc) {
			r = rr;
			c = cc;
		}
		
		public Node n() {
			return nodes[r][c];
		}
		
		public Pair to(int k) {
			return new Pair (r+dr[k], c+dc[k]);
		}
		
		public boolean v() {
			return r >= 0 && r < R && c >= 0 && c < C && b[r][c] >= pL && b[r][c] <= pR;
		}
	}


	public static class Node {
		int nodeValue, subTreeValue, delta, size;
		// delta affects nodeValue, subTreeValue, left.delta and right.delta
		boolean revert;
		Node left, right, parent;

		Node(int value) {
			subTreeValue = nodeValue = value;
			size = 1;
		}

		// tests whether x is a root of a splay tree
		boolean isRoot() {
			return parent == null || (parent.left != this && parent.right != this);
		}

		void push() {
			if (revert) {
				revert = false;
				Node t = left;
				left = right;
				right = t;
				if (left != null)
					left.revert = !left.revert;
				if (right != null)
					right.revert = !right.revert;
			}
		}
	}

	
	static void connect(Node ch, Node p, Boolean isLeftChild) {
		if (ch != null)
			ch.parent = p;
		if (isLeftChild != null) {
			if (isLeftChild)
				p.left = ch;
			else
				p.right = ch;
		}
	}

	static void rotate(Node x) {
		Node p = x.parent, g = p.parent;
		boolean isRootP = p.isRoot(), leftChildX = (x == p.left);

		// create 3 edges: (x.r(l),p), (p,x), (x,g)
		connect(leftChildX ? x.right : x.left, p, leftChildX);
		connect(p, x, !leftChildX);
		connect(x, g, isRootP ? null : p == g.left);
	}

	static void splay(Node x) {
		while (!x.isRoot()) {
			Node p = x.parent, g = p.parent;
			if (!p.isRoot())
				g.push();
			p.push();
			x.push();
			if (!p.isRoot())
				rotate((x == p.left) == (p == g.left) ? p : x);
			rotate(x);
		}
		x.push();
	}

	// makes node x the root of the virtual tree, and also x becomes the leftmost
	// node in its splay tree
	static Node expose(Node x) {
		Node last = null;
		for (Node y = x; y != null; y = y.parent) {
			splay(y);
			y.left = last;
			last = y;
		}
		splay(x);
		return last;
	}

	public static void makeRoot(Node x) {
		expose(x);
		x.revert = !x.revert;
	}

	public static boolean connected(Node x, Node y) {
		if (x == y)
			return true;
		expose(x);
		expose(y);
		return x.parent != null;
	}

	public static void link(Node x, Node y) {
		makeRoot(x);
		x.parent = y;
	}

	public static void cut(Node x, Node y) {
		makeRoot(x);
		expose(y);
		y.right.parent = null;
		y.right = null;
	}


	static class FS {
		BufferedReader br;
		StringTokenizer st;

		public FS() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					throw null;
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
	}

}