Problem: Ksenia has a chessboard of size n × m. Each cell of the chessboard contains one of the characters: "<", ">", "^", "v", "#". The cells that contain character "#" are blocked. We know that all chessboard cells that touch the border are blocked.

Ksenia is playing with two pawns on this chessboard. Initially, she puts the pawns on the chessboard. One cell of the chessboard can contain two pawns if and only if the cell is blocked. In other cases two pawns can not stand in one cell. The game begins when Ksenia put pawns on the board. In one move, Ksenia moves each pawn to a side adjacent cell in the direction of arrows painted on the cell on which the corresponding pawn sits (if the pawn sits on "#", it does not move). Assume that Ksenia moves pawns simultaneously (see the second test case). 

Of course, Ksenia plays for points. How can one calculate the points per game? Very simply! Let's count how many movements the first pawn made and how many movements the second pawn made, sum these two numbers — it will be the resulting score of the game. 

Ksenia wonders: what is the maximum number of points she can earn (for that, she should place the pawns optimally well early in the game). Help her and find that number. 

Input

The first line contains two integers, n and m (1 ≤ n, m ≤ 2000) — the sizes of the board. Each of the following n lines contains m characters – the board's description. Each character is one of the characters: "<", ">", "^", "v", "#".

It is guaranteed that the border cells of the table are blocked cells (with character "#").

Output

If Ksenia can get infinitely many points, print -1. Otherwise, print the maximum number of points she can get.

Examples

Input

1 1
#


Output

0


Input

3 4
####
#&gt;^{}#
####


Output

3

Input

3 4
####
#&gt;&lt;#
####


Output

-1

Input

7 5
#####
##v##
##v##
#####
##^{}##
##^{}##
#####


Output

4

Input

7 5
#####
##v##
##v##
##&lt;##
##^{}##
##^{}##
#####

Output

5

Solution (in JAVA):

Solution:
import static java.util.Arrays.*;

import java.util.*;

public class D {

	private final static boolean autoflush = false;

	public D () {
		int N = sc.nextInt();
		int M = sc.nextInt();
		char [][] A = sc.nextChars(N);

		int [][] F = new int [N][M];
		for (int [] f : F) fill(f, -1);

		int [] Q = new int [N*M];
		int fi = 0, la = 0;

		for (int i : rep(N))
			for (int j : rep(M))
				if (A[i][j] == '#') {
					F[i][j] = 0;
					Q[la++] = M*i + j;
				}

		while (fi < la) {
			int q = Q[fi++];
			int i = q/M, j = q%M;

			int x, y;

			x = i-1; y = j;
			if (x >= 0 && F[x][y] == -1 && A[x][y] == 'v') {
				F[x][y] = F[i][j] + 1;
				Q[la++] = M*x + y;
			}

			x = i+1; y = j;
			if (x < N && F[x][y] == -1 && A[x][y] == '^') {
				F[x][y] = F[i][j] + 1;
				Q[la++] = M*x + y;
			}

			x = i; y = j-1;
			if (y >= 0 && F[x][y] == -1 && A[x][y] == '>') {
				F[x][y] = F[i][j] + 1;
				Q[la++] = M*x + y;
			}

			x = i; y = j+1;
			if (y < M && F[x][y] == -1 && A[x][y] == '<') {
				F[x][y] = F[i][j] + 1;
				Q[la++] = M*x + y;
			}
		}

		int Z = 0;
		for (int i : rep(N))
			for (int j : rep(M))
				if (F[i][j] == -1)
					exit(-1);
				else
					Z = max(Z, F[i][j]);

		fi = 0; la = 0;
		int res = 2*Z;
		for (int i : rep(N))
			for (int j : rep(M))
				if (F[i][j] == Z)
					Q[la++] = M*i + j;

		boolean [][] S = new boolean [N][M];
		while (la - fi > 1) {
			int q = Q[fi++];
			int i = q/M, j = q%M;

			if (A[i][j] == 'v') ++i;
			else if (A[i][j] == '^') --i;
			else if (A[i][j] == '>') ++j;
			else if (A[i][j] == '<') --j;
			else continue;

			if (!S[i][j]) {
				Q[la++] = M*i + j;
				S[i][j] = true;
			}
		}

		int q = Q[fi];
		int i = q/M, j = q%M;
		if (F[i][j] > 0)
			--res;

		exit(res);
	}

	///////////////////////////////////////////////////////////////////////////
	private static int [] rep(int N) { return rep(0, N); }
	private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
	private static <T extends Comparable<T>> T max(T x, T y) { return x.compareTo(y) > 0 ? x : y; }
	////////////////////////////////////////////////////////////////////////////////////
	private final static MyScanner sc = new MyScanner();
	private static class MyScanner {
		public String next() { newLine(); return line[index++]; }
		public int nextInt() { return Integer.parseInt(next()); }
		public char [] nextChars() { return next ().toCharArray (); }
		public char [][] nextChars(int N) {
			char [][] res = new char [N][];
			for (int i = 0; i < N; ++i)
				res[i] = nextChars();
			return res;
		}
		//////////////////////////////////////////////
		private boolean eol() { return index == line.length; }
		private String readLine() {
			try {
				return r.readLine();
			} catch (Exception e) {
				throw new Error (e);
			}
		}
		private final java.io.BufferedReader r;
		private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
		private MyScanner (java.io.BufferedReader r) {
			try {
				this.r = r;
				while (!r.ready())
					Thread.sleep(1);
				start();
			} catch (Exception e) {
				throw new Error(e);
			}
		}
		private String [] line;
		private int index;
		private void newLine() {
			if (line == null || eol()) {
				line = readLine().split(" ");
				index = 0;
			}
		}
	}
	private static void print (Object o, Object... a) { printDelim(" ", o, a); }
	private static void printDelim (String delim, Object o, Object... a) { pw.println(build(delim, o, a)); }
	private static void exit (Object o, Object... a) { print(o, a); exit(); }
	private static void exit() {
		pw.close();
		System.out.flush();
		System.err.println("------------------");
		System.err.println("Time: " + ((millis() - t) / 1000.0));
		System.exit(0);
	}
	////////////////////////////////////////////////////////////////////////////////////
	private static String build (String delim, Object o, Object... a) {
		StringBuilder b = new StringBuilder();
		append(b, o, delim);
		for (Object p : a)
			append(b, p, delim);
		return b.substring(delim.length());
	}
	private static void append(StringBuilder b, Object o, String delim) {
		if (o.getClass().isArray()) {
			int L = java.lang.reflect.Array.getLength(o);
			for (int i = 0; i < L; ++i)
				append(b, java.lang.reflect.Array.get(o, i), delim);
		} else if (o instanceof Iterable<?>)
			for (Object p : (Iterable<?>)o)
				append(b, p, delim);
		else
			b.append(delim).append(o);
	}
	////////////////////////////////////////////////////////////////////////////////////
	private static void start() { t = millis(); }
	private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out, autoflush);
	private static long t;
	private static long millis() { return System.currentTimeMillis(); }
	public static void main (String[] args) { new D(); exit(); }
}