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;
template <class T>
inline void read(T& x) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
  if (fu) x = -x;
};
char getc() {
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  return c;
}
const int N = 2010;
const int f1[] = {0, 0, 1, -1};
const int f2[] = {1, -1, 0, 0};
int gg(char c) {
  if (c == '<') return 0;
  if (c == '>') return 1;
  if (c == '^') return 2;
  if (c == 'v') return 3;
  return -1;
}
int n, m, i, j, k, l, p, t, x, y, x2, y2, g, head, tail, s0, n0, s1, n1;
char a[N][N];
bool have, oo;
int q[N * N][2], d[N][N], s[N][N];
bool v[N][N];
bool pd(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }
int main() {
  read(n);
  read(m);
  for (i = 1; i <= n; i++) scanf("%s", a[i] + 1);
  int cnt = 0;
  head = 1;
  tail = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (a[i][j] == '#') {
        ++tail;
        q[tail][0] = i;
        q[tail][1] = j;
        s[i][j] = -1;
        v[i][j] = 1;
      } else {
        cnt++;
      }
  for (; head <= tail; head++) {
    x = q[head][0];
    y = q[head][1];
    for (t = 0; t <= 3; t++) {
      x2 = x + f1[t];
      y2 = y + f2[t];
      if (pd(x2, y2) && gg(a[x2][y2]) == t) {
        if (v[x2][y2]) {
          printf("-1\n");
          return 0;
        }
        ++tail;
        have = 1;
        ++g;
        q[tail][0] = x2;
        q[tail][1] = y2;
        d[x2][y2] = d[x][y] + 1;
        if (a[x][y] == '#')
          s[x2][y2] = (x2 - 1) * m + y2;
        else
          s[x2][y2] = s[x][y];
      }
    }
  }
  if (g != cnt) {
    printf("-1\n");
    return 0;
  }
  int ans = 0;
  ans = s0 = d[q[tail][0]][q[tail][1]];
  n0 = s[q[tail][0]][q[tail][1]];
  for (i = tail - 1; i >= 1; i--) {
    ans =
        max(ans, s0 + d[q[i][0]][q[i][1]] -
                     (s[q[i][0]][q[i][1]] == n0 && d[q[i][0]][q[i][1]] == s0));
  }
  printf("%d\n", ans);
  scanf("\n");
  return 0;
}