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.*;
import java.math.BigInteger;
import java.util.*;

import static java.lang.Double.parseDouble;
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;

public class Main {
    public static void main(String[] args) throws IOException {
        new Solver().run();
    }
}

class Solver {
    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;

    void run() throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        //in = new BufferedReader(new FileReader("people.in"));
        //PrintWriter out = new PrintWriter(new File("people.out"));

        solve();

        out.close();
    }

    int[][] used, dp;
    String[] a;
    int[][] dest;
    int n, m, cur;

    public void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        Q = new int[n * m * 2];
        used = new int[n][m];
        a = new String[n];
        dp = new int[n][m];
        dest = new int[n][m];
        for (int i = 0; i < n; ++i) {
            a[i] = next();
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (used[i][j] == 0) {
                    if (bfs(i, j)) {
                        out.println(-1);
                        return;
                    }
                }
            }
        }
        int maxi = -1;
        int maxDest = -1;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                maxi = Math.max(maxi, dp[i][j]);
                if (dp[i][j] == maxi) {
                    maxDest = dest[i][j];
                }
            }
        }
        boolean flag = false;
        int cnt = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (maxi == dp[i][j]) {
                    ++cnt;
                }
                if (maxi == dp[i][j] && maxDest != dest[i][j]) {
                    flag = true;
                }
            }
        }
        int ans = 2 * maxi;
        if ((cnt == 1 || !flag) && maxi != 0) {
            --ans;
        }
        out.println(ans);
    }

    int ptr = 0;
    int[] Q;

    boolean bfs(int x, int y) {
        ++ptr;
        int r = 0;
        while (true) {
            used[x][y] = ptr;
            Q[r++] = x;
            Q[r++] = y;
            char c = a[x].charAt(y);
            int tx = x, ty = y;
            if (c == 'v') {
                ++tx;
            } else if (c == '^') {
                --tx;
            } else if (c == '>') {
                ++ty;
            } else if (c == '<') {
                --ty;
            }
            if (c != '#') {
                if (used[tx][ty] == ptr) {
                    return true;
                }
                if (used[tx][ty] == 0) {
                    x = tx;
                    y = ty;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        while (r != 0) {
            x = Q[r - 2];
            y = Q[r - 1];
            r -= 2;
            char c = a[x].charAt(y);
            int tx = x, ty = y;
            if (c == 'v') {
                ++tx;
            } else if (c == '^') {
                --tx;
            } else if (c == '>') {
                ++ty;
            } else if (c == '<') {
                --ty;
            }
            if (c != '#') {
                dp[x][y] = dp[tx][ty] + 1;
                dest[x][y] = dest[tx][ty];
                if (dest[x][y] >= n * m) {
                    dest[x][y] = x * m + y;
                }
            } else {
                dest[x][y] = n * m + cur++;
            }
        }
        return false;
    }

    // stack overflow
    boolean dfs(int x, int y) {
        used[x][y] = 1;
        char c = a[x].charAt(y);
        int tx = x, ty = y;
        if (c == 'v') {
            ++tx;
        } else if (c == '^') {
            --tx;
        } else if (c == '>') {
            ++ty;
        } else if (c == '<') {
            --ty;
        }
        if (c != '#') {
            if (used[tx][ty] == 1) {
                return true;
            }
            if (used[tx][ty] == 0) {
                if (dfs(tx, ty)) {
                    return true;
                }
            }
            dp[x][y] = dp[tx][ty] + 1;
            dest[x][y] = dest[tx][ty];
            if (dest[x][y] >= n * m) {
                dest[x][y] = x * m + y;
            }
        } else {
            dest[x][y] = n * m + cur++;
        }
        used[x][y] = 2;
        return false;
    }

    String next() throws IOException {
        if (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(next());
    }

    int nextInt() throws IOException {
        return parseInt(next());
    }

    long nextLong() throws IOException {
        return parseLong(next());
    }

    double nextDouble() throws IOException {
        return parseDouble(next());
    }
}