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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
char a[2010][2010];
int vis[2010][2010];
int memo[2010][2010], m, n;
pair<int, int> endw[2010][2010];
int dfs(int x, int y) {
  if (memo[x][y] != -1) return memo[x][y];
  int i, j;
  if (a[x][y] == '#') return memo[x][y] = 0;
  vis[x][y] = -1;
  if (a[x][y] == '^') i = x - 1;
  if (a[x][y] == 'v') i = x + 1;
  if (a[x][y] == '<') j = y - 1;
  if (a[x][y] == '>') j = y + 1;
  if (a[x][y] == '^' || a[x][y] == 'v')
    j = y;
  else
    i = x;
  if (vis[i][j] == 0 || vis[i][j] == 1) {
    int d = dfs(i, j);
    vis[x][y] = 1;
    if (d == -2) return memo[x][y] = -2;
    if (d == 0)
      endw[x][y] = make_pair(x, y);
    else
      endw[x][y] = endw[i][j];
    return memo[x][y] = d + 1;
  } else {
    vis[x][y] = 1;
    return memo[x][y] = -2;
  }
}
int main() {
  memset(memo, -1, sizeof(memo));
  memset(endw, 0, sizeof(endw));
  memset(vis, 0, sizeof(vis));
  cin >> n >> m;
  for (int i = (int)(1), _n = (int)(n + 1); i < _n; i++)
    for (int j = (int)(1), _n = (int)(m + 1); j < _n; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#') {
        vis[i][j] = 1;
        endw[i][j] = make_pair(i, j);
      }
    }
  int len = 0, len2 = 0, x = 1, y = 1;
  for (int i = (int)(1), _n = (int)(n + 1); i < _n; i++)
    for (int j = (int)(1), _n = (int)(m + 1); j < _n; j++)
      if (vis[i][j] == 0) {
        int d = dfs(i, j);
        if (d == -2) {
          cout << -1;
          return 0;
        } else if (d > len) {
          len = d;
          x = i;
          y = j;
        } else if (d == len && endw[i][j] != endw[x][y])
          len2 = d;
      }
  if (len2 == len)
    cout << 2 * len;
  else
    cout << 2 * len - 1;
  return 0;
}