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.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Stack;
import java.util.StringTokenizer;


public class D_224 {
	static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st = new StringTokenizer("");
	
	static String readString() throws Exception {
		while(!st.hasMoreTokens())
			st = new StringTokenizer(stdin.readLine());
		return st.nextToken();
	}

	static int readInt() throws Exception {
		return Integer.parseInt(readString());
	}
	static char board[];
	static int n,m;
	static int id(int y, int x){
		return y*m+x;
	}
	public static void main(String[] args) throws Exception {
		n = readInt();
		m = readInt();
		board = new char[n*m];
		for(int i = 0; i < n; i++){
			String s = readString();
			for(int j = 0; j < m; j++){
				board[id(i,j)] = s.charAt(j);
			}
		}
		
		postorders = new ArrayList();
		 clock = 0;
		colors = new int[n*m];
		 color = 0;
		 ArrayDeque<Node> st = new ArrayDeque();
		for(int i = 0; i < m; i++){
			for(int j = 0; j < n; j++){
				if(colors[id(j,i)] == 0){
//					System.out.println("visiting ("+i+","+j+")");
					color++;
					st.push(new Node(i,j));
					
					while(!st.isEmpty()){
						Node node = st.peek();
						
						int x = node.id%m;
						int y = node.id/m;
//						System.out.println("peeket ("+x+","+y+")");
						if(colors[id(y,x)] == 0){
//							System.out.println("hadde ikke farge fra før");
							colors[id(y,x)] = color;
						} else {
//							System.out.println("var besøkt fra før");
							st.pop();
							postorders.add(new Postorder(x, y, clock++));
							continue;
						}
						int xx = -1;
						int yy = -1;
						if(board[id(y,x)] == '<'){
							xx = x-1;
							yy = y;
						} else if(board[id(y,x)] == '>'){
							xx = x+1;
							yy = y;
						} else if(board[id(y,x)] == '^'){
							xx = x;
							yy = y-1;
						} else if(board[id(y,x)] == 'v'){
							xx = x;
							yy = y+1;
						} 
						if(xx >= 0 && xx < m && yy >= 0 && yy < n){
//							System.out.println("checking nabo");
							if(colors[id(yy,xx)] == 0){
//								System.out.println("pusher");
								//dfs(xx, yy);
								//colors[id(yy,xx)] = color;
								st.push(new Node(xx,yy));
							} else if(colors[id(yy,xx)] == color){
								System.out.println(-1);
								System.exit(0);
							}
						}
						
					}
					//dfs(i,j);
				}
			}
		}
//		int aa = 0;
//		for(int i : colors){
//			System.out.print(i +" ");
//			if(++aa%m == 0 )System.out.println();
//		}
		colors = null;
		System.gc();
		Collections.sort(postorders);
//		for(Postorder p : postorders){
//			System.out.println("("+p.x+","+p.y+") = "+p.val);
//		}
		int dist[] = new int[n*m];
		int INF = -1000000000;
		Arrays.fill(dist, INF);
		for(int i = postorders.size()-1; i >= 0; i--){
			Postorder p = postorders.get(i);
			if(dist[p.id] < 0) dist[p.id] = 0;
			Node nabo = nabo(p.id%m, p.id/m);
			if(nabo.id < 0 || nabo.id >= n*m) continue;
			if(dist[nabo.id] < dist[p.id] + 1){
				dist[nabo.id] = dist[p.id] + 1;
			} else if(dist[nabo.id] == dist[p.id] + 1){
				
			}
		}
		int longest = INF;
		int x = -1;
		int y = -1;
		boolean two = false;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				if(board[id(i,j)] != '#') continue;
				if(dist[id(i,j)] > longest){
					longest = dist[id(i,j)];
					x = j;
					y = i;
					two = false;
				} else if(dist[id(i,j)] == longest){
					two = true;
				}
			}
		}
//		for(int[] i : dist){
//			System.out.println(Arrays.toString(i));
//		}
		int cnt = 0;
		for(int[] e : edges){
			if(y+e[0] >= 0 && x+e[1] >= 0 && y+e[0] < n && x+e[1] < m &&dist[id(y+e[0],x+e[1])] == longest-1)cnt++;
		}
		int ans = -1;
		if(two || cnt>= 2){
			ans = 2*longest;
		} else {
			ans = 2*longest-1;
		}
		if(ans > 0) System.out.println(ans);
		else System.out.println(0);

	}
	
	static int color;
	static ArrayList<Postorder> postorders;
	static int clock;
	static int colors[];
	static Node nabo(int x, int y){
		int xx = -1;
		int yy = -1;
		if(board[id(y,x)] == '<'){
			xx = x-1;
			yy = y;
		} else if(board[id(y,x)] == '>'){
			xx = x+1;
			yy = y;
		} else if(board[id(y,x)] == '^'){
			xx = x;
			yy = y-1;
		} else if(board[id(y,x)] == 'v'){
			xx = x;
			yy = y+1;
		} 
		return new Node(xx,yy);
	}
	private static void dfs(int i, int j) {

		Node node = new Node(i,j);
		int x = node.id%m;
		int y = node.id/m;
		colors[id(y,x)] = color;
		int xx = -1;
		int yy = -1;
		if(board[id(y,x)] == '<'){
			xx = x-1;
			yy = y;
		} else if(board[id(y,x)] == '>'){
			xx = x+1;
			yy = y;
		} else if(board[id(y,x)] == '^'){
			xx = x;
			yy = y-1;
		} else if(board[id(y,x)] == 'v'){
			xx = x;
			yy = y+1;
		} 
		if(xx >= 0 && xx < m && yy >= 0 && yy < n){
			if(colors[id(yy,xx)] == 0){
				dfs(xx, yy);
			} else if(colors[id(yy,xx)] == color){
				System.out.println(-1);
				System.exit(0);
			}
		}
		postorders.add(new Postorder(x, y, clock++));

		
	}
	static int edges[][] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
	
	static class Node{
		int id;
		Node(int xx, int yy){
			id = id(yy,xx);
		}
	}
	
	static class Postorder implements Comparable<Postorder>{
		int id;
		int val;
		Postorder(int xx, int yy, int post){
			id = id(yy,xx);
			val = post;
		}
		@Override
		public int compareTo(Postorder o) {
			// TODO Auto-generated method stub
			return Integer.compare(val, o.val);
		}
	}

}