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.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Vadim Semenov
 */
public class TaskD implements Runnable {
    private final InputReader in = new InputReader(System.in);
    private final PrintWriter out = new PrintWriter(System.out);
    
    //_______________________________>__v__<__^__
    private static final int[] dx = {0, 1, 0, -1};
    private static final int[] dy = {1, 0, -1, 0};

    private int rows, columns;
    private int[][] table, maxPath, end;
    private boolean haveCycle;
    
    private void solve() {
        rows = in.nextInt();
        columns = in.nextInt();
        table = new int[rows][columns];
        maxPath = new int[rows][columns];
        end = new int[rows][columns];
        for (int[] row : maxPath) Arrays.fill(row, -1);
        for (int i = 0; i < rows; i++) {
            String row = in.next();
            for (int j = 0; j < columns; j++) {
                switch (row.charAt(j)) {
                    case '#' : table[i][j] = -1; break;
                    case '>' : table[i][j] = 0; break;
                    case 'v' : table[i][j] = 1; break;
                    case '<' : table[i][j] = 2; break;
                    case '^' : table[i][j] = 3; break;
                    default: throw new RuntimeException("Illegal character");
                }
            }
        }
        int maximum = -1;
        for (int i = rows - 1; i >= 0; i--) {
            for (int j = columns - 1; j >= 0; j--) {
                if (maxPath[i][j] == -1) {
                    maximum = Math.max(maximum, dfs(i, j));
                }
                if (haveCycle) {
                    out.println(-1);
                    return;
                }
            }
        }
        List<Integer> candidates = new ArrayList<>();
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < columns; j++)
                if (maximum == maxPath[i][j])
                    candidates.add(hash(i, j));
        int answer = 2 * maximum - 1;
        for (int i = 1; i < candidates.size(); i++) {
            int[] cell = decode(candidates.get(i));
            int x = cell[0]; int y = cell[1];
            cell = decode(candidates.get(i - 1));
            int xx = cell[0]; int yy = cell[1];
            if (end[x][y] != end[xx][yy]) answer = 2 * maximum;
        }
        out.println(Math.max(0, answer));
    }

    private int dfs(int x, int y) {
        if (maxPath[x][y] == -2) {
            haveCycle = true;
            return Integer.MIN_VALUE;
        }
        if (maxPath[x][y] != -1) return maxPath[x][y];
        maxPath[x][y] = -2;
        if (table[x][y] == -1) {
            end[x][y] = hash(x, y);
            return maxPath[x][y] = 0;
        }
        int length = dfs(x + dx[table[x][y]], y + dy[table[x][y]]);
        end[x][y] = end[x + dx[table[x][y]]][y + dy[table[x][y]]];
        if (length == 0) end[x][y] = hash(x, y);
        return maxPath[x][y] = 1 + length;
    }

    private int hash(int x, int y) {
        return x * columns + y;
    }

    private int[] decode(int hash) {
        return new int[]{hash / columns, hash % columns};
    }
    
    @Override
    public void run() {
        solve();
        out.close();        
    }
    
    public static void main(String[] args) {
        new Thread(null, new TaskD(), "Stack extension", 200 * 1000 * 1000).start();
    }
    
}

class InputReader {
    private static BufferedReader bufferedReader;
    private static StringTokenizer stringTokenizer;

    public InputReader(InputStream inputStream) {
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    }

    public String next() {
        while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
            try {
                stringTokenizer = new StringTokenizer(bufferedReader.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringTokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}