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 bool read(T &n) {
  T x = 0, tmp = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
  if (c == EOF) return false;
  if (c == '-') c = getchar(), tmp = -1;
  while (c >= '0' && c <= '9') x *= 10, x += (c - '0'), c = getchar();
  n = x * tmp;
  return true;
}
template <class T>
inline void write(T n) {
  if (n < 0) {
    putchar('-');
    n = -n;
  }
  int len = 0, data[20];
  while (n) {
    data[len++] = n % 10;
    n /= 10;
  }
  if (!len) data[len++] = 0;
  while (len--) putchar(data[len] + 48);
}
char Map[2010][2010];
const int MAXN = 2000 * 2000 + 10;
int Top_Edge = -1;
struct N {
  int v, next;
} Edge[MAXN];
int head[MAXN];
void link(int u, int v) {
  Edge[++Top_Edge].v = v;
  Edge[Top_Edge].next = head[u];
  head[u] = Top_Edge;
}
bool id[MAXN], od[MAXN], mv[MAXN];
int Ans = 0, MaxDepth = -1;
int dfs(int s, int h) {
  mv[s] = true;
  int TD, MD = 1, ans = 0;
  for (int p = head[s]; p != -1; p = Edge[p].next) {
    if (mv[Edge[p].v] == false) {
      TD = dfs(Edge[p].v, h + 1);
      if (TD + 1 > MD) {
        MD = TD + 1;
        ans = 1;
      } else if (TD + 1 == MD)
        ans++;
    } else
      return -1;
  }
  while (h == 1 && ans--)
    if (MD > MaxDepth) {
      MaxDepth = MD;
      Ans = 1;
    } else if (MD == MaxDepth)
      Ans++;
  return MD;
}
int main() {
  int top, n, m, i, j, ans = 0;
  read(n), read(m);
  for (i = 1; i <= n; ++i) gets(Map[i] + 1);
  top = n * m;
  memset(id, false, sizeof(bool) * (top + 2));
  memset(od, false, sizeof(bool) * (top + 2));
  memset(mv, false, sizeof(bool) * (top + 2));
  memset(head, -1, sizeof(int) * (top + 2));
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= m; ++j) {
      if (Map[i][j] == '>') {
        ans++;
        link((i - 1) * m + j + 1, (i - 1) * m + j);
        od[(i - 1) * m + j + 1] = true;
        id[(i - 1) * m + j] = true;
      } else if (Map[i][j] == '<') {
        ans++;
        link((i - 1) * m + j - 1, (i - 1) * m + j);
        od[(i - 1) * m + j - 1] = true;
        id[(i - 1) * m + j] = true;
      } else if (Map[i][j] == '^') {
        ans++;
        link((i - 2) * m + j, (i - 1) * m + j);
        od[(i - 2) * m + j] = true;
        id[(i - 1) * m + j] = true;
      } else if (Map[i][j] == 'v') {
        ans++;
        link(i * m + j, (i - 1) * m + j);
        od[i * m + j] = true;
        id[(i - 1) * m + j] = true;
      }
    }
  }
  for (i = 1; i <= top; ++i) {
    if (od[i] && id[i] == false) {
      dfs(i, 1);
      ans++;
    }
  }
  for (i = 1; i <= top; ++i)
    if (mv[i]) ans--;
  if (ans != 0)
    printf("-1\n");
  else {
    if (Ans >= 2)
      printf("%d\n", MaxDepth * 2 - 2);
    else if (Ans == 0)
      printf("%d\n", 0);
    else
      printf("%d\n", MaxDepth * 2 - 3);
  }
  return 0;
}