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.*;
import java.util.Map.Entry;
import java.io.*;
import java.awt.Point;
import java.math.BigInteger;

import static java.lang.Math.*;

public class CodeforcesD implements Runnable {

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        if (ONLINE_JUDGE) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new CodeforcesD(), "", 128 * (1L << 20)).start();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    void debug(Object... objects) {
        if (DEBUG) {
            for (Object o : objects) {
                System.err.println(o.toString());
            }
        }
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            time();
            memory();
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    boolean DEBUG = false;

    int INF = 1000 * 1000 * 1000;

    int n, m;

    boolean valid(int x, int y) {
        if (x >= 0 && x < n && y >= 0 && y < m)
            return true;
        return false;
    }
    
    class Point {
        int x, y, color;

        public Point(int x, int y, int color) {
            super();
            this.x = x;
            this.y = y;
            this.color = color;
        }
    }

    int[][] steps = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };

    void solve() throws IOException {

        n = readInt();
        m = readInt();

        char[][] c = new char[n][m];

        for (int i = 0; i < n; i++) {
            char[] s = readString().toCharArray();
            for (int j = 0; j < m; j++)
                c[i][j] = s[j];
        }

        ArrayDeque<Point> q = new ArrayDeque<Point>();
        int[][] d = new int[n][m];

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                d[i][j] = -INF;
        
        int color = 1;
        int[][] col = new int[n][m];

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                if (i==1 && j==3) {
                    i=1;
                }
                if (c[i][j] == '#') {
                    for (int t = 0; t < 4; t++) {
                        int toX = i + steps[t][0];
                        int toY = j + steps[t][1];
                        if (!valid(toX, toY))
                            continue;
                        if (c[toX][toY] == '<' && t == 0) {
                            d[toX][toY] = 1;
                            d[i][j]=0;
                            col[toX][toY] = color;
                            q.add(new Point(toX, toY, color++));
                        }
                        if (c[toX][toY] == '>' && t == 1) {
                            d[toX][toY] = 1;
                            d[i][j]=0;
                            col[toX][toY] = color;
                            q.add(new Point(toX, toY, color++));
                        }
                        if (c[toX][toY] == '^' && t == 2) {
                            d[toX][toY] = 1;
                            d[i][j]=0;
                            col[toX][toY] = color;
                            q.add(new Point(toX, toY, color++));
                        }
                        if (c[toX][toY] == 'v' && t == 3) {
                            d[toX][toY] = 1;
                            d[i][j]=0;
                            col[toX][toY] = color;
                            q.add(new Point(toX, toY, color++));
                        }
                    }
                }
            }

        while (!q.isEmpty()) {
            Point v = q.pollFirst();
            int x = v.x;
            int y = v.y;
            for (int t = 0; t < 4; t++) {
                int toX = x + steps[t][0];
                int toY = y + steps[t][1];
                color = v.color;
                if (!valid(toX, toY))
                    continue;
                if (c[toX][toY] == '<' && t == 0 && d[toX][toY] < 1 + d[x][y]) {
                    d[toX][toY] = 1 + d[x][y];
                    q.add(new Point(toX, toY, v.color));
                    col[toX][toY] = color;
                }
                if (c[toX][toY] == '>' && t == 1 && d[toX][toY] < 1 + d[x][y]) {
                    d[toX][toY] = 1 + d[x][y];
                    q.add(new Point(toX, toY, v.color));
                    col[toX][toY] = color;
                }
                if (c[toX][toY] == '^' && t == 2 && d[toX][toY] < 1 + d[x][y]) {
                    d[toX][toY] = 1 + d[x][y];
                    q.add(new Point(toX, toY, v.color));
                    col[toX][toY] = color;
                }
                if (c[toX][toY] == 'v' && t == 3 && d[toX][toY] < 1 + d[x][y]) {
                    d[toX][toY] = 1 + d[x][y];
                    q.add(new Point(toX, toY, v.color));
                    col[toX][toY] = color;
                }
            }
        }
        
        for (int i=0; i<n; i++)
            for (int j=0; j<m; j++)
                if (c[i][j]!='#' && d[i][j]==-INF) {
                    out.println(-1);
                    return;
                }
        
        int max = -INF;
        
        int fromX = 0, fromY = 0, fromColor=-1;
        
        for (int i=0; i<n; i++) 
            for (int j=0; j<m; j++) {
                if (max<d[i][j]) {
                    max = d[i][j];
                    fromX = i;
                    fromY = j;
                    fromColor = col[i][j];
                }
            }
        
        if (max<0) {
            out.println(0);
            return;
        }
        
        int fromX1 = 0, fromY1 = 0, fromColor1 = -1;
        int max1 = -INF;
        
        for (int i=0; i<n; i++) 
            for (int j=0; j<m; j++) {
                if (max1<d[i][j] && (i!=fromX || j!=fromY) && col[i][j]!=fromColor) {
                    max1 = d[i][j];
                    fromX1 = i;
                    fromY1 = j;
                }
            }

        if (max1==-INF) max1 = max-1;
        max1 = max(max1, max-1);
        out.println(max+max1);
        
    }
}