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.IOException;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.util.ArrayDeque;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Agostinho Junior
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
	OutputWriter out;
	InputReader in;
	public void solve(int testNumber, InputReader in, OutputWriter out) {
		this.in = in; this.out = out;
		int n = in.readInt();
		int m = in.readInt();
		char[][] board = new char[n][];
		for (int i = 0; i < n; i++) {
			board[i] = in.readLine().toCharArray();
		}
		out.println(getAns(board, n, m));
	}
	private int getAns(char[][] board, int n, int m) {
		ArrayList<Point> roots = new ArrayList<Point>();
		for (int i = -1; i <= n; i++) {
			for (int j = -1; j <= m; j++) {
				if (i < 0 || n <= i || j < 0 || m <= j || board[i][j] == '#') {
					for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) if (Math.abs(dx) != Math.abs(dy)) {
						int x = i + dx;
						int y = j + dy;
						if (0 <= x && x < n && 0 <= y && y < m) {
							char expected;
							if (dx == -1) {
								expected = 'v';
							} else if (dx == 1) {
								expected = '^';
							} else if (dy == -1) {
								expected = '>';
							} else {
								expected = '<';
							}
							if (board[x][y] == expected) {
								roots.add(new Point(x, y));
							}
						}
					}
				}
			}
		}
		int[][] cost = new int[n][m];
		int small = 0;
		int big = 0;
		for (Point root: roots) {
			ArrayDeque<Point> queue = new ArrayDeque<Point>();
			queue.push(root);
			int curMax = 0;
			cost[root.x][root.y] = 1;
			while (!queue.isEmpty()) {
				Point now = queue.pop();
				int x = now.x;
				int y = now.y;
				for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) if (Math.abs(dx) != Math.abs(dy)) {
					int nx = x + dx;
					int ny = y + dy;
					if (0 <= nx && nx < n && 0 <= ny && ny < m) {
						char expected;
						if (dx == -1) {
							expected = 'v';
						} else if (dx == 1) {
							expected = '^';
						} else if (dy == -1) {
							expected = '>';
						} else {
							expected = '<';
						}
						if (board[nx][ny] == expected) {
							cost[nx][ny] = cost[x][y] + 1;
							curMax = Math.max(curMax, cost[nx][ny]);
							queue.push(new Point(nx, ny));
						}
					}
				}
			}
			if (curMax >= big) {
				small = big;
				big = curMax;
			} else if (curMax > small) {
				small = curMax;
			}
		}
		for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (cost[i][j] == 0 && board[i][j] != '#') {
			ArrayDeque<Point> queue = new ArrayDeque<Point>();
			queue.push(new Point(i, j));
			cost[i][j] = 1;
			while (!queue.isEmpty()) {
				Point now = queue.pop();
				int x = now.x;
				int y = now.y;
				for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) if (Math.abs(dx) != Math.abs(dy)) {
					int nx = x + dx;
					int ny = y + dy;
					if (0 <= nx && nx < n && 0 <= ny && ny < m) {
						char expected;
						if (dx == -1) {
							expected = 'v';
						} else if (dx == 1) {
							expected = '^';
						} else if (dy == -1) {
							expected = '>';
						} else {
							expected = '<';
						}
						if (board[nx][ny] == expected) {
							if (0 < cost[nx][ny]) {
								return -1;
							}
							cost[nx][ny] = cost[x][y] + 1;
							queue.push(new Point(nx, ny));
						}
					}
				}
			}
		}
		if (small == big) {
			return 2 * big;
		}
		return big + big - 1;
	}
}

class OutputWriter {
	private PrintWriter output;
	public OutputWriter(OutputStream out) {
		output = new PrintWriter(out);
	}
	public void println(Object o) {
		output.println(o);
	}
	public void close() {
		output.close();
	}
}

class InputReader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");
	public InputReader(InputStream in) {
		input = new BufferedReader(new InputStreamReader(in));
	}
	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
	public int readInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}
	public String readLine() {
		String s = "";
		try {
			s = input.readLine();
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return s;
	}
}