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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProblemD {
	static final long MOD = 1000000007;

	static int[] dx = new int[128];
	static int[] dy = new int[128];
	
	static int[] DX = {1, 0, -1, 0};
	static int[] DY = {0, 1, 0, -1};

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out);
		dx['<'] = -1;
		dx['>'] = 1;
		dy['^'] = -1;
		dy['v'] = 1;
		String[] nm = in.readLine().split(" ");
		int n = Integer.valueOf(nm[0]);
		int m = Integer.valueOf(nm[1]);
		char[][] map = new char[n][];
		for (int i = 0; i < n; i++) {
			map[i] = in.readLine().toCharArray();
		}
		
		int[][] indeg = new int[n][m];
		int[] ord = toposort(map, indeg);
		if (ord == null) {
			out.println(-1);
			out.flush();
			return;
		}

		int[] dp = new int[n*m];
		int[] start = new int[n*m];
		Arrays.fill(dp, -1);
		Arrays.fill(start, -1);
		for (int i = 0 ; i < n ; i++) {
			for (int j = 0 ; j < m ; j++) {
				if (indeg[i][j] == 0) {
					dp[i*m+j] = 0;
					start[i*m+j] = i*m+j;
				}
			}
		}
		
		for (int i = 0 ; i < n*m ; i++) {
			int d = ord[i];
			if (dp[d] >= 0) {
				int x = d % m;
				int y = d / m;
				boolean first = (start[d] == d);
				for (int di = 0 ; di < 4 ; di++) {
					int tx = x+DX[di];
					int ty = y+DY[di];
					if (tx < 0 || ty < 0 || tx >= m || ty >= n || map[ty][tx] == '#') {
						continue;
					}
					int by = ty + dy[map[ty][tx]];
					int bx = tx + dx[map[ty][tx]];
					if (by == y && bx == x) {
						int tid = ty*m+tx;
						if (dp[tid] < dp[d]+1) {
							int to = first ? (start[d] * 4 + di) : start[d];
							dp[tid] = dp[d]+1;
							start[tid] = to;
						}
					}
				}
			}
		}
		
		out.println(solve(dp, start));
		out.flush();
	}

	private static int solve(int[] dp, int[] start) {
		int max = 0;
		int bestStart = -1;
		int best = 0;
		for (int i = 0 ; i < dp.length ; i++) {
			if (dp[i] >= 1) {
				max = Math.max(max, dp[i] * 2 - 1);
			}
			if (best < dp[i]) {
				best = dp[i];
				bestStart = start[i];
			}
		}
		
		int bestOther = 0;
		for (int i = 0 ; i < dp.length ; i++) {
			if (start[i] != bestStart && bestOther < dp[i]) {
				bestOther = dp[i];
			}
		}
		max = Math.max(max, best + bestOther);
		return max;
	}

	static int[] toposort(char[][] map, int[][] indeg) {
		int n = map.length;
		int m = map[0].length;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (map[i][j] == '#') {
					continue;
				}
				indeg[i][j]++;
			}
		}
		
		int[] res = new int[n*m];
		int idx = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (indeg[i][j] == 0) {
					res[idx++] = i*m+j;
				}
			}
		}
		for (int i = 0; i < idx; i++) {
			int v = res[i];
			int x = v % m;
			int y = v / m;
			for (int d = 0 ; d < 4 ; d++) {
				int tx = x+DX[d];
				int ty = y+DY[d];
				if (tx < 0 || ty < 0 || tx >= m || ty >= n || map[ty][tx] == '#') {
					continue;
				}
				int by = ty + dy[map[ty][tx]];
				int bx = tx + dx[map[ty][tx]];
				if (by == y && bx == x) {
					indeg[ty][tx]--;
					if (indeg[ty][tx] == 0) {
						res[idx++] = ty*m+tx;
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0 ; j < m ; j++) {
				if (indeg[i][j] >= 1) {
					return null;
				}
			}
		}
		return res;
	}

	public static void debug(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}

}