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:
// why checking each twice???
import java.util.*;
import java.io.*;
import java.awt.*;
public class Main {
    static BufferedReader reader 
		= new BufferedReader(new InputStreamReader(System.in));       
    static StringBuilder out = new StringBuilder();
    static Point[][] graph = new Point[2005][2005];
    static boolean[][] hasBeenTo = new boolean[2005][2005];
    static int maxLength = 0;
    static int cnt = 0;

    public static void main(String[] args){
        int n, m;
        int[] inp = nextIntArray();
        n = inp[0]; m = inp[1];
        for (int row = 1; row <= n; row ++)
        {
            char[] ary = nextLine().toCharArray();
            for (int col = 1; col <= m; col ++)
            {
                switch (ary[col - 1])
                {
                    case ('^'):
                        graph[row][col] = new Point(row - 1, col);
                        break;
                    case ('<'):
                        graph[row][col] = new Point(row, col - 1);
                        break;
                    case ('>'):
                        graph[row][col] = new Point(row, col + 1);
                        break;
                    case ('v'):
                        graph[row][col] = new Point(row + 1, col);
                        break;
                    default:
                        graph[row][col] = new Point(-1, -1);
                }
            }
        }
        for (int r = 1; r <= n; r++)
            for (int c = 1; c <= m; c++)
            {
                int x = graph[r][c].x;
                int y = graph[r][c].y;
                if (x == -1)
                    hasBeenTo[r][c] = true;
                else if (graph[x][y].x == -1)
                {
                    hasBeenTo[x][y] = true;
                    help(r, c);
                }
            }

        boolean hasSeenEveryCell = true;
        for (int r = 1; r <= n; r++)
            for (int c = 1; c <= m; c++)
                hasSeenEveryCell &= hasBeenTo[r][c];

        if (! hasSeenEveryCell)
            out.append(-1);
        else if (maxLength == 0)
            out.append(0);
        else if (cnt == 1)
            out.append(maxLength * 2 - 1);
        else
            out.append(maxLength * 2);
        System.out.println(out);
    }
    // update the maxLength and cnt
    // by searching the path from r, c
    static void dfs(int r, int c)
    {
        hasBeenTo[r][c] = true;
        int[] d1 = {-1, 0, 1, 0};
        int[] d2 = {0, 1, 0, -1};
        for (int i = 0; i < 4; i++)
        {
            int dx = d1[i];
            int dy = d2[i];
            if (graph[r + dx][c + dy] != null && 
                graph[r + dx][c + dy].x == r &&
                graph[r + dx][c + dy].y == c)
            {
                // System.out.println("dfs(" + r + ", " + c + ") help(" + (r + dx) + ", " + (c + dy) + ")");
                help(r + dx, c + dy);
            }
        }
    }
    // update the maxLength and cnt
    // by searching the path from r, c
    static void help(int r, int c)
    {

        if (graph[r][c] == null || graph[r][c].x == -1)
            return;
        int len = 1;
        HashSet<Point> points = new HashSet<Point> ();
        points.add(new Point (r, c));
        while (true)
        {
            HashSet<Point> next = new HashSet<Point>();
            for (Point cur : points)
            {
                hasBeenTo[cur.x][cur.y] = true;
                // check 4 adjacent points to cur
                int x = cur.x;
                int y = cur.y;
                int[] d1 = {-1, 0, 1, 0};
                int[] d2 = {0, 1, 0, -1};
                for (int i = 0; i < 4; i++)
                {
                    int dx = d1[i];
                    int dy = d2[i];
                    if (graph[x + dx][y + dy] == null)
                        continue;
                    if (graph[x + dx][y + dy].x == -1)
                        hasBeenTo[x + dx][y + dy] = true;
                    else if (graph[x + dx][y + dy].x == x &&
                             graph[x + dx][y + dy].y == y)
                             // add this point to next
                             next.add(new Point(x + dx, y + dy));
                }

            }
            // check size and exit if necessary
            if (next.size() >= 1)
            {
                len ++;
                points = next;
            }
            else
                break;
        }
        // compare with max length
        if (len > maxLength)
        {
            maxLength = len;
            cnt = 1;
        }
        else if (len == maxLength)
            cnt ++;
        // System.out.println(len + " = (" + r + ", " + c + ")");
    }
    

    // the followings are methods to take care of inputs.
    static int nextInt(){
        return Integer.parseInt(nextLine());
    }
    static long nextLong(){
        return Long.parseLong(nextLine());
    }
    static int[] nextIntArray(){
        String[] inp = nextLine().split("\\s+");
        int[] ary = new int[inp.length]; for (int i = 0; i < ary.length; i++){
            ary[i] = Integer.parseInt(inp[i]);
        }
        return ary;
    }
    static int[] nextIntArrayFrom1(){
        String[] inp = nextLine().split("\\s+");
        int[] ary = new int[inp.length + 1];
        for (int i = 0; i < inp.length; i++){
            ary[i+1] = Integer.parseInt(inp[i]);
        }
        return ary;
    }
    static long[] nextLongArray(){
        String[] inp = nextLine().split("\\s+");
        long[] ary = new long[inp.length];
        for (int i = 0; i < inp.length; i++){
            ary[i] = Long.parseLong(inp[i]);
        }
        return ary;
    }
    static long[] nextLongArrayFrom1(){
        String[] inp = nextLine().split("\\s+");
        long[] ary = new long[inp.length + 1];
        for (int i = 0; i < inp.length; i++){
            ary[i+1] = Long.parseLong(inp[i]);
        }
        return ary;
    }
	static String nextLine(){
		try { 
			return reader.readLine().trim();
		} catch (Exception e){}
		return null;
	}
}