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


public class D {	
	BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter out;
    
	public void solve() throws IOException {				
		int N = nextInt();
		int M = nextInt();
		char[][] board = new char[N][];
		for (int i = 0; i < N; i++) {
			board[i] = reader.readLine().toCharArray();
		}
		
		for (int i = 1; i < N-1; i++) {
			for (int j = 1; j < M-1; j++) {
				if (board[i][j] == '>' && board[i][j+1] == '<') {
					out.println(-1); return;
				}
				if (board[i][j] == '^' && board[i-1][j] == 'v') {
					out.println(-1); return;
				}
			}
		}
		
		int[][] moves = new int[N][M];
		for (int i = 1; i < N-1; i++) {
			for (int j = 1; j < M-1; j++) {
				if (board[i][j] == '#') continue;
				
				boolean isStart = true;
				if (board[i-1][j] == 'v') isStart = false;
				if (board[i+1][j] == '^') isStart = false;
				if (board[i][j-1] == '>') isStart = false;
				if (board[i][j+1] == '<') isStart = false;
				
				if (isStart) {
					int mv = 0;
					int curI = i;
					int curY = j;
					while (board[curI][curY] != '#') {
						mv++;
						switch (board[curI][curY]) {
						case 'v':
							curI++;
							break;
						case '^':
							curI--;
							break;
						case '<':
							curY--;
							break;
						case '>':
							curY++;
							break;
						}			
						
						if (curI == i && curY == j) {
							out.println(-1); return;
						}
					}
					
					curI = i;
					curY = j;
					while (board[curI][curY] != '#') {
						moves[curI][curY] = Math.max(moves[curI][curY], mv);
						mv--;
						switch (board[curI][curY]) {
						case 'v':
							curI++;
							break;
						case '^':
							curI--;
							break;
						case '<':
							curY--;
							break;
						case '>':
							curY++;
							break;
						}			
					}				
				}				
			}
		}
		
		int largestI = -1;
		int largestY = -1;
		int largestI2 = -1;
		int largestY2 = -1;
		
		int largest = 0;
		int largest2 = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				if (moves[i][j] > 0) {
					if (moves[i][j] > largest) {
						largest2 = largest;
						largest = moves[i][j];
						largestI2 = largestI;
						largestY2 = largestY;
						largestI = i;
						largestY = j;
					} else if(moves[i][j] > largest2) {
						largest2 = moves[i][j];
						largestI2 = i;
						largestY2 = j;
					}
				}
			}
		}
		
		if (largest > 0 && largest2 > 0) {
			int curI = largestI;
			int curY = largestY;
			int curI2 = largestI2;
			int curY2 = largestY2;
			
			while (board[curI2][curY2] != '#') {
				switch (board[curI][curY]) {
				case 'v':
					curI++;
					break;
				case '^':
					curI--;
					break;
				case '<':
					curY--;
					break;
				case '>':
					curY++;
					break;
				}
				
				switch (board[curI2][curY2]) {
				case 'v':
					curI2++;
					break;
				case '^':
					curI2--;
					break;
				case '<':
					curY2--;
					break;
				case '>':
					curY2++;
					break;
				}			
				
				if (curY == curY2 && curI == curI2 && board[curI][curY] != '#') {
					largest2--;
					break;
				}
			}				
		}
		out.println( largest + largest2);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new D().run();
	}
	
	public void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
            out = new PrintWriter(System.out);
            solve();
            reader.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

}