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.util.*;

public class D {
	private static final String DIRECTIONS = "><^v";
	private static final int[] X = { 0, 0, -1, 1 };
	private static final int[] Y = { 1, -1, 0, 0 };

	private static int n;
	private static int m;
	private static char[][] board = new char[2000][];
	private static boolean[][] visited = new boolean[2000][2000];
	private static int[][][] distance = new int[2][2000][2000];
	
	private static final int MAX_STACK_SIZE = 4000000;
	private static int[] buffer = new int[MAX_STACK_SIZE];

	private static class IntStack{
		private int top = -1;
		IntStack(){
			
		}
		void push(int value){
			buffer[++top] = value;
		}
		int pop(){
			return buffer[top--];
		}
		int peek(){
			return buffer[top];
		}
		boolean isEmpty(){
			return top == -1;
		}
	}
	
	private static boolean dfs(int a, int b) {
		final IntStack s = new IntStack();
		visited[a][b] = true;
		s.push(a*m + b);
		while (true){
			final int top = s.peek();
			final int i = top/m;
			final int j = top%m;
			final int d = DIRECTIONS.indexOf(board[i][j]);
			final int ni = i + X[d];
			final int nj = j + Y[d];
			if (board[ni][nj] == '#') {
				distance[0][i][j] = i*m + j;
				distance[1][i][j] = 1;
				break;
			}else if (visited[ni][nj]){
				if (distance[1][ni][nj] == 0){
					return false;
				}
				distance[0][i][j] = distance[0][ni][nj];
				distance[1][i][j] = distance[1][ni][nj] + 1;
				break;
			}else{
				visited[ni][nj] = true;
				s.push(ni*m + nj);
			}
		}
		Integer prev = s.pop();
		while (!s.isEmpty()){
			final int v = s.pop();
			final int i = v/m;
			final int j = v%m;
			final int pi = prev/m;
			final int pj = prev%m;
			distance[0][i][j] = distance[0][pi][pj];
			distance[1][i][j] = distance[1][pi][pj] + 1;
			prev = v;
		}
		return true;
	}

	public static void main(String[] args) {
		final Scanner s = new Scanner(System.in);
		n = s.nextInt();
		m = s.nextInt();
		for (int i = 0; i < n; ++i) {
			board[i] = s.next().toCharArray();
		}
		boolean loopexists = false;
		for (int i = 1; i < n - 1; ++i) {
			for (int j = 1; j < m - 1; ++j) {
				if (!visited[i][j] && board[i][j] != '#') {
					if (!dfs(i, j)) {
						loopexists = true;
						break;
					}
				}
			}
		}
		if (loopexists) {
			System.out.println(-1);
		} else {
			int [][] max = new int[2000][2000];
			for (int i = 1; i < n - 1; ++i){
				for (int j = 1; j < m - 1; ++j){
					if (distance[1][i][j] != 0){
						final int ni = distance[0][i][j]/m;
						final int nj = distance[0][i][j]%m;
						final int d = distance[1][i][j];
						max[ni][nj] = Math.max(max[ni][nj], d);
					}
				}
			}
			int max1 = 0;
			int max2 = 0;
			for (int i = 1; i < n - 1; ++i){
				for (int j = 1; j < m - 1; ++j){
					if (max[i][j] > max1){
						max2 = max1;
						max1 = max[i][j];
					}else if (max[i][j] > max2){
						max2 = max[i][j];
					}
				}
			}
			if (max2 == 0){
				if (max1 <= 1){
					System.out.println(0);
				}else{
					System.out.println(2*max1 - 1);
				}
			}else{
				if (max1 == max2){
					System.out.println(2*max1);
				}else{
					System.out.println(2*max1 - 1);
				}
			}
		}
		s.close();
	}
}