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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class CF382D
{
	static int n, m;
	static char[][] grid;
	static int visited[][], vid;
	static int ans[][];
	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner();
		n = sc.nextInt();
		m = sc.nextInt();
		grid = new char[n][m];
		for (int i = 0; i < n; i++)
			grid[i] = sc.next().toCharArray();
		visited = new int[n][m];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
			{
				if(visited[i][j] == 0)
				{
					vid++;
					if(cycle(i, j))
					{
						System.out.println(-1);
						return;
					}
				}
			}
		vid = 3;
		
		ans = new int[n][m];
		for (int i = 0; i < n; i++)
			Arrays.fill(ans[i], -1);
//		for (int i = 0; i < n; i++)
//			for (int j = 0; j < m; j++)
//				if(ans[i][j] == -1)
//					calc();
		
		calc();
		int[] max = new int[3];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
			{
				max[0] = ans[i][j];
				Arrays.sort(max);
			}
		
		if(max[1] != max[2])
			System.out.println(max[1] + max[2]);
		else
		{
			int res = max[1] + max[2];
			if(res == 0)
			{
				System.out.println(res);
				return;
			}
			int cnt = 0;
			for (int i = 0; i < n; i++)
				for (int j = 0; j < m; j++)
					if(visited[i][j] != vid && ans[i][j] == max[2])
						if(ok(i, j))
							cnt++;
			if(cnt == 1)
				System.out.println(res-1);
			else
				System.out.println(res);
		}
	}
	
	private static boolean ok(int i, int j)
	{
		if(grid[i][j] == '#')
			return true;
		visited[i][j] = vid;
		int childi = i, childj = j;
		if(grid[i][j] == 'v')
			childi = i+1;
		if(grid[i][j] == '>')
			childj = j+1;
		if(grid[i][j] == '<')
			childj = j-1;
		if(grid[i][j] == '^')
			childi = i-1;
		if(visited[childi][childj] == vid)
			return false;
		return ok(childi, childj);
	}

//	private static void calc(int i, int j)
//	{
//		if(grid[i][j] == '#')
//		{
//			ans[i][j] = 0;
//			return;
//		}
//		int childi = i, childj = j;
//		if(grid[i][j] == 'v')
//			childi = i+1;
//		if(grid[i][j] == '>')
//			childj = j+1;
//		if(grid[i][j] == '<')
//			childj = j-1;
//		if(grid[i][j] == '^')
//			childi = i-1;
//		calc(childi, childj);
//		ans[i][j] = ans[childi][childj] + 1;
//	}

	private static void calc()
	{
		Queue<Pair> q = new LinkedList<Pair>();
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				if(grid[i][j] == '#')
					q.add(new Pair(i, j, 0));
		
		while(!q.isEmpty())
		{
			Pair f = q.poll();
			int i = f.x;
			int j = f.y;
			int cost = f.cost;
			ans[i][j] = cost;
			if(valid(i-1, j) && grid[i-1][j] == 'v')
				q.add(new Pair(i-1, j, cost+1));
			if(valid(i, j-1) && grid[i][j-1] == '>')
				q.add(new Pair(i, j-1, cost+1));
			if(valid(i, j+1) && grid[i][j+1] == '<')
				q.add(new Pair(i, j+1, cost+1));
			if(valid(i+1, j) && grid[i+1][j] == '^')
				q.add(new Pair(i+1, j, cost+1));
			
		}
	}
	private static boolean cycle(int i, int j)
	{
		while(true)
		{
			if(grid[i][j] == '#')
			{
				visited[i][j] = vid;
				return false;
			}
			if(visited[i][j] == vid)
				return true;
			visited[i][j] = vid;
			int childi = i, childj = j;
			if(grid[i][j] == 'v')
				childi = i+1;
			if(grid[i][j] == '>')
				childj = j+1;
			if(grid[i][j] == '<')
				childj = j-1;
			if(grid[i][j] == '^')
				childi = i-1;
			i = childi;
			j = childj;
		}
	}

	static boolean valid(int x, int y)
	{
		if(x >= 0 && y >= 0 && x < n && y < m)
			return true;
		return false;
	}
	static class Pair
	{
		int x,y,cost;
		public Pair(int x, int y, int cost)
		{
			this.x = x;
			this.y = y;
			this.cost = cost;
		}
	}
	static class Scanner
	{
		StringTokenizer st;BufferedReader br;
		public Scanner() throws FileNotFoundException{
			br = new BufferedReader(new InputStreamReader(System.in));
//			br = new BufferedReader(new FileReader(new File("tc.out")));
			}
		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws NumberFormatException, IOException{return Integer.parseInt(next());}
	}
}