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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;

public class D {
    
    static int sptr;
    static byte[] stack;
    static void path(String[] in, int[][] p2l, int[][] p2r, boolean[][] v2p, int i0, int j0) {
        
        int i=i0, j=j0;
        while(true) {
            if(v2p[i][j]) {
                p2l[i][j]=-1;
                break;
            } else if(p2l[i][j]>0) {
                break;
            }
            v2p[i][j]=true;
            
            if(in[i].charAt(j)=='>') {
                
                if(in[i].charAt(j+1)=='#') {
                    p2r[i][j]=(i<<16)+j;
                    p2l[i][j]=1;
                    break;
                } else {
                    stack[sptr++]=0;
                    j++; //path(in, p2l, p2r, v2p, i, j+1);
                }
            } else if(in[i].charAt(j)=='<') {
                
                if(in[i].charAt(j-1)=='#') {
                    p2r[i][j]=(i<<16)+j;
                    p2l[i][j]=1;
                    break;
                } else {
                    stack[sptr++]=1;
                    j--; //path(in, p2l, p2r, v2p, i, j-1);
                }
            } else if(in[i].charAt(j)=='^') {
                
                if(in[i-1].charAt(j)=='#') {
                    p2r[i][j]=(i<<16)+j;
                    p2l[i][j]=1;
                    break;
                } else {
                    stack[sptr++]=2;
                    i--; //path(in, p2l, p2r, v2p, i-1, j);
                }
            } else if(in[i].charAt(j)=='v') {
                
                if(in[i+1].charAt(j)=='#') {
                    p2r[i][j]=(i<<16)+j;
                    p2l[i][j]=1;
                    break;
                } else {
                    stack[sptr++]=3;
                    i++; //path(in, p2l, p2r, v2p, i+1, j);
                }
            }
        }
        
        v2p[i0][j0]=false;
        while(i!=i0||j!=j0) {
            v2p[i][j]=false;
            byte state=stack[--sptr];
            if(state==0) {
                j--;
                p2r[i][j]=p2r[i][j+1];

                if(p2l[i][j+1]>=0) {
                    p2l[i][j]=p2l[i][j+1]+1;
                } else {
                    p2l[i][j]=-1;
                }
            } else if(state==1) {
                j++;
                p2r[i][j]=p2r[i][j-1];
                
                if(p2l[i][j-1]>=0) {
                    p2l[i][j]=p2l[i][j-1]+1;
                } else {
                    p2l[i][j]=-1;
                }
            } else if(state==2) {
                i++;
                p2r[i][j]=p2r[i-1][j];
                
                if(p2l[i-1][j]>=0) {
                    p2l[i][j]=p2l[i-1][j]+1;
                } else {
                    p2l[i][j]=-1;
                }
            } else if(state==3) {
                i--;
                p2r[i][j]=p2r[i+1][j];
                if(p2l[i+1][j]>=0) {
                    p2l[i][j]=p2l[i+1][j]+1;
                } else {
                    p2l[i][j]=-1;
                }
            }
        }
    }
    
    static int compute(String[] in) {
        int n=in.length, m=in[0].length();
        stack=new byte[n*m];
        
        int[][] p2l=new int[n][m], p2r=new int[n][m];
        boolean[][] v2p=new boolean[n][m];
        
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                if(in[i].charAt(j)!='#'&&p2l[i][j]==0) {
                    path(in,p2l,p2r,v2p,i,j);
                }
            }
        }
        
        int max=0;
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                if(p2l[i][j]<0) {
                    return -1;
                } else if(p2l[i][j]>max) {
                    max=p2l[i][j];
                }
            }
        }
        
        HashSet<Integer> hs=new HashSet<Integer>();
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                if(p2l[i][j]==max) {
                    hs.add(p2r[i][j]);
                    if(hs.size()>1) {
                        return max<<1;
                    }
                }
            }
        }
        
        return max>0? (max<<1)-1: 0;
    }

    
    
    
    
    
    
    static void run_stream(InputStream ins) throws IOException {
        BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
        String l=r.readLine();
        int n=Integer.parseInt(l.substring(0, l.indexOf(' ')));
        
        String[] in=new String[n];
        for (int i=0; i<n; i++) {
            in[i]=r.readLine();
        }
        
        System.out.println(compute(in));
    }

    public static void main(String[] args) throws IOException {
        run_stream(System.in);
    }

}