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.*;
import java.text.DecimalFormat;
import java.util.*;

public class Main {
	static int mod = (int)1e9+7;
	static int inf = (int)(2e9+1e8);
	static double eps = 1e-15;
	static int N = 2005;
	char[][] a = new char[N][N];
	boolean[][] vis = new boolean[N][N];
	int[][] d = new int[N][N] , num = new int[N][N];
	int n, m;
	String ch = "^><v";
	int bfs(int x, int y){
	    Queue<Integer>qx = new LinkedList<Integer>(), qy = new LinkedList<Integer>();
	    vis[x][y] = true;
	    qx.add(x); qy.add(y);
	    d[x][y] = 1;
	    int maxv = 0;
	    while(qx.size() > 0){
	        int tx = qx.poll(), ty = qy.poll();
	        for(int i = 0; i < 4; i ++){
	            int txx = tx + dx[i], tyy = ty + dy[i];
	            if(txx<=0 || txx>n || tyy<=0 || tyy>m) continue;
	            if(a[txx][tyy]!=ch.charAt(i)) continue;
	            if(vis[txx][tyy]) continue;
	            d[txx][tyy] = d[tx][ty] + 1;
	            qx.add(txx); qy.add(tyy);
	            vis[txx][tyy] = true;
	            maxv = Math.max(d[txx][tyy],maxv);
	        }
	    }
	    return maxv;
	}
    void work() throws Exception {
    	n = nextInt();
    	m = nextInt();
    	for(int i = 1; i <= n; i ++) {
    		String s = next();
    		for(int j = 0; j < m; j ++) {
    			a[i][j+1] = s.charAt(j);
    		}
    	}
    	
    
    	//
    	for(int i = 0; i < N; i ++) {
	    	Arrays.fill(d[i], 0);
			Arrays.fill(num[i], 0);
			Arrays.fill(vis[i], false);
    	}
    	//
    	for(int i = 1; i <= n; i ++) {
    		for(int j = 1; j <= m; j ++) {
    			if(a[i][j] == '#') {
    				vis[i][j] = true;
    		        num[i][j] = inf;
    		        for(int k = 0; k < 4; k ++){
    		            int x = i + dx[k], y = j + dy[k];
    		            if(a[x][y] != ch.charAt(k)) continue;
    		            if(vis[x][y]) continue;
    		            if(x<=0 || x>n || y<=0 || y>m) continue;
    		   //         System.out.println(i + " " + j + " " + k + " " + x + " " + y);
    		            
    		            int tmp = bfs(x,y);
    		            if(tmp>d[i][j]) {
    		                num[i][j] = 1;
    		                d[i][j] = tmp;
    		            } else if(tmp==d[i][j]) num[i][j]++;
    		        }
    			}
     		}
    	}
    	boolean flag = false;
        int ans = 0, cnt = 0;
        for(int i = 1; i <= n; i ++) {
    		for(int j = 1; j <= m; j ++) {
	            if(!vis[i][j]) {
	            	flag = true;
	            //	System.out.println(i + " " + j);
	            }
	            else if(a[i][j]=='#'){
	                if(d[i][j]==ans) cnt += num[i][j];
	                else if(d[i][j]>ans){
	                    ans = d[i][j];
	                    cnt = num[i][j];
	                }
	            }
    		}
        }
        if(flag) ans=-1;
        else {
        	ans = ans + ans;
        	if(cnt==1) ans--;
        }
        out.println(ans);	
    }  
    static PrintWriter out = new PrintWriter(System.out);
    Scanner cin = new Scanner(System.in);
  
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer str = null;
    private String next() throws Exception{
    	while (str == null || !str.hasMoreElements())
    	    str = new StringTokenizer(in.readLine());
    	return str.nextToken();
    } 
    private int nextInt() throws Exception{
    	return Integer.parseInt(next());
    }
    private long nextLong() throws Exception{
    	return Long.parseLong(next());
    }
    static int dx[] = {1, 0, 0, -1};
    static int dy[] = {0, -1, 1, 0};
    class pair implements Comparable<pair>{
    	int first, second;
    	pair(int x, int y) {
    		first = x;
    		second = y;
    	}
		public int compareTo(pair o) {
			// TODO auto-generated method stub
			if(first != o.first) return ((Integer)first).compareTo(o.first);
			else return ((Integer)second).compareTo(o.second); 
		}
    }
    int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;
    	int pos = r;
    	r--;
    	while (l <= r) {
	    	int mid = (l + r) >> 1;
	    	if (A[mid] <= val) {
	    		l = mid + 1;
	    	} else {
	    		pos = mid;
	    		r = mid - 1;
	    	}
    	}
    	return pos;
    }
    int lower_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;
		int pos = r;
		r--;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if (A[mid] < val) {
				l = mid + 1;
			} else {
				pos = mid;
				r = mid - 1;
			}
		}
		return pos;
	}
    DecimalFormat df=new DecimalFormat("0.0000000000");
    public static void main(String[] args) throws Exception{
        Main wo = new Main();
        wo.work();
        out.close();
    }
}