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;
const int maxn = 2050;
char fo[10] = "#^>v<";
int n, m, ma[maxn][maxn], len[maxn][maxn] = {0}, s, z = 0, ans;
int vis[maxn][maxn] = {0};
int dx[5] = {0, -1, 0, 1, 0}, dy[5] = {0, 0, 1, 0, -1};
struct Len {
  int id, l;
} l0[maxn * maxn];
bool cmp(const Len &xx, const Len &yy) { return xx.l > yy.l; }
void dfs(int x, int y, int k) {
  if (vis[x][y] == k) {
    z = 1;
    return;
  }
  if (!ma[x][y] || vis[x][y]) return;
  vis[x][y] = k;
  dfs(x + dx[ma[x][y]], y + dy[ma[x][y]], k);
  len[x][y] = len[x + dx[ma[x][y]]][y + dy[ma[x][y]]] + 1;
}
int main() {
  int i, j, k;
  char ci;
  scanf("%d%d\n", &n, &m);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%c", &ci);
      for (k = 0; k < 5; k++)
        if (ci == fo[k]) ma[i][j] = k;
    }
    scanf("\n");
  }
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      if (!len[i][j]) dfs(i, j, i * m + j + 1);
      l0[i * m + j].id = i * m + j;
      l0[i * m + j].l = len[i][j];
    }
  if (z)
    ans = -1;
  else {
    sort(l0, l0 + n * m, cmp);
    if (!l0[0].l)
      ans = 0;
    else if (l0[0].l != l0[1].l)
      ans = l0[0].l * 2 - 1;
    else {
      for (s = 1; s < n * m && l0[s].l == l0[0].l; s++)
        ;
      memset(vis, 0, sizeof(vis));
      int x = l0[0].id / m, y = l0[0].id % m;
      for (i = 0; i < l0[0].l; i++, k = ma[x][y], x += dx[k], y += dy[k])
        vis[x][y] = 1;
      for (i = 1; i < s; i++) {
        z = 0;
        x = l0[i].id / m, y = l0[i].id % m;
        for (j = 0; j < l0[0].l; j++, k = ma[x][y], x += dx[k], y += dy[k])
          if (vis[x][y]) {
            z = 1;
            break;
          }
        if (!z) break;
      }
      ans = z ? l0[0].l * 2 - 1 : l0[0].l * 2;
    }
  }
  printf("%d\n", ans);
  return 0;
}