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.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;

public class KseninaAndPawns_382D {
	static int[][] sol;
	static String s = "^v<>";
	static int[] dy = new int[]{0,0,-1,1};
	static int[] dx = new int[]{-1,1,0,0};
	static char[][] grid;
	static int sx[] , sy[];
	static int kk = 0;
	static void dfs(int x ,int y)
	{
		while(grid[x][y] != '#')
		{
			if(sol[x][y] != 0)
				break;
			sx[kk] = x;
			sy[kk] = y;
			kk++;
			int x2 = x + dx[dir[grid[x][y]]];
			int y2 = y + dy[dir[grid[x][y]]];
			x = x2;
			y = y2;
		}
		kk--;
		while(kk >= 0)
		{
			x = sx[kk];
			y = sy[kk];
			kk--;
			sol[x][y] = 1 + sol[x+dx[dir[grid[x][y]]]][ y + dy[dir[grid[x][y]]]];
		}
		kk++;
	}
	static int[][] vis;
	static int k;
	static boolean inf;
	static void check(int x ,int y)
	{
		Queue<Integer> qx = new LinkedList<Integer>();
		Queue<Integer> qy = new LinkedList<Integer>();
		qx.add(x);
		qy.add(y);
		while(!qx.isEmpty())
		{
			int xx = qx.poll();
			int yy = qy.poll();
			vis[xx][yy] = k;
			if(grid[xx][yy] == '#')
				continue ;
			if(vis[xx + dx[s.indexOf(grid[xx][yy])]][yy + dy[s.indexOf(grid[xx][yy])]] == k)
			{
				inf = true;
				return;
			}
			if(vis[xx + dx[s.indexOf(grid[xx][yy])]][yy + dy[s.indexOf(grid[xx][yy])]] == 0)
			{
				qx.add(xx + dx[s.indexOf(grid[xx][yy])]);
				qy.add(yy + dy[s.indexOf(grid[xx][yy])]);
			}
		}
	}
	static int[] dir = new int[1000];
	static void dfs3(int x, int y)
	{
		Queue<Integer> qx = new LinkedList<Integer>();
		Queue<Integer> qy = new LinkedList<Integer>();
		qx.add(x);
		qy.add(y);
		while(!qx.isEmpty())
		{
			int xx = qx.poll();
			int yy = qy.poll();
			if(grid[xx][yy] == '#')
				continue;
			int x2 = xx + dx[s.indexOf(grid[xx][yy])];
			int y2 = yy + dy[s.indexOf(grid[xx][yy])];
			grid[xx][yy] = '#';
			qx.add(x2);
			qy.add(y2);
		}
	}


	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		grid = new char[n][m];
		sx = new int[4100009];
		sy = new int[4100009];
		for(int i=0;i<n;i++)
			grid[i] = sc.next().toCharArray();
		long tt = System.currentTimeMillis();
		inf = false;
		dir['^'] = s.indexOf('^');
		dir['v'] = s.indexOf('v');
		dir['>'] = s.indexOf('>');
		dir['<'] = s.indexOf('<');
//		n = 2000;
//		m = 2000;
//		grid = new char[n][m];
//		for(int i=0;i<n;i++)
//			for(int j=0;j<m;j++)
//			{
//				if(i == 0 || i == n-1 || j == m-1 || j == 0)
//					grid[i][j] = '#';
//				else
//					if(i == 1 && j%2 == 0)
//						grid[i][j] = '>';
//					else
//						if(i == n-2 && j%2 == 1)
//							grid[i][j] = '>';
//						else
//							if(j%2 == 1)
//								grid[i][j] = 'v';
//							else
//								grid[i][j]= '^';
//			}

		k = 1;
		vis = new int[n][m];
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if(vis[i][j] == 0)
				{
					check(i,j);
					k++;
				}
		if(inf)
			System.out.println(-1);
		else
		{
			sol = new int[n][m];
			for(int i=0;i<n;i++)
				Arrays.fill(sol[i], 0);
			for(int i=0;i<n;i++)
				for(int j=0;j<m;j++)
					if(grid[i][j] != '#' && sol[i][j] == 0)
						dfs(i,j);
			int max = 0;
			int maxx = 0 , maxy = 0;
			for (int i = 0; i < grid.length; i++) {
				for (int j = 0; j < grid[i].length; j++) {
					if(max < sol[i][j])
					{
						max = Math.max(max, sol[i][j]);
						maxx = i;
						maxy = j;
					}
				}
			}
			dfs3(maxx, maxy);
			sol = new int[n][m];
			for(int i=0;i<n;i++)
				Arrays.fill(sol[i], 0);
			for(int i=0;i<n;i++)
				for(int j=0;j<m;j++)
					if(grid[i][j] != '#' && sol[i][j] == 0)
						dfs(i,j);
			int max2 = 0;
			for (int i = 0; i < grid.length; i++) {
				for (int j = 0; j < grid[i].length; j++) {
					max2 = Math.max(max2, sol[i][j]);
				}
			}
			System.out.println(Math.max(2*max-1 , max + max2));
		}
	}

	static class Scanner{
		StringTokenizer st;BufferedReader br;
		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}
		public String next() throws IOException 
		{while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());return st.nextToken();}
		public int nextInt() throws IOException {return Integer.parseInt(next());}
		public long nextLong() throws IOException {return Long.parseLong(next());}
		public String nextLine() throws IOException {return br.readLine();}
		public double nextDouble() throws IOException {return Double.parseDouble(next());}
		public boolean ready() throws IOException {return br.ready();}
	}

}