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 N = 2005;
char M[N][N];
int dp[N][N];
int bio[N][N];
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
const char alfa[4] = {'<', '>', 'v', '^'};
int n, m;
inline void load() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", M[i]);
}
int cycle = -1;
int px, py;
int SOL;
inline void update(int &maks, int &maks2, int ans) {
  if (ans > maks) {
    maks2 = maks;
    maks = ans;
  } else if (ans > maks2)
    maks2 = ans;
}
int dfs(int x, int y, char c) {
  if (x < 0 || x >= n || y < 0 || y >= m) return 0;
  if (M[x][y] != c) return 0;
  if (M[x][y] == '#') return 0;
  if (dp[x][y] != -1) return dp[x][y];
  if (cycle != -1) return 0;
  if (bio[x][y] == 2) {
    cycle = 0;
    return 0;
  }
  int ref = 0;
  int maks = 0, maks2 = 0;
  for (int i = 0; i < 4; i++) {
    int ans = dfs(x + dx[i], y + dy[i], alfa[i]);
    update(maks, maks2, ans + 1);
    ref = max(ref, ans + 1);
  }
  dp[x][y] = ref;
  return ref;
}
void rek(int x, int y) {
  if (M[x][y] == '#') return;
  if (bio[x][y] == 1) return;
  if (bio[x][y] == 2) {
    cycle = 0;
    return;
  }
  if (cycle == 0) return;
  bio[x][y] = 2;
  if (M[x][y] == alfa[0]) rek(x, y - 1);
  if (M[x][y] == alfa[1]) rek(x, y + 1);
  if (M[x][y] == alfa[2]) rek(x + 1, y);
  if (M[x][y] == alfa[3]) rek(x - 1, y);
  bio[x][y] = 1;
}
inline bool ciklus() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (M[i][j] != '#') rek(i, j);
    }
  }
  if (cycle == 0) return true;
  return false;
}
int main(void) {
  load();
  cycle = -1;
  memset(dp, -1, sizeof(dp));
  int nezz = 0, nezz2 = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (M[i][j] == '#') {
        px = i, py = j;
        for (int k = 0; k < 4; k++) {
          int ans = dfs(i + dx[k], j + dy[k], alfa[k]);
          if (ans > nezz)
            nezz2 = nezz, nezz = ans;
          else if (ans > nezz2)
            nezz2 = ans;
        }
        if (nezz == nezz2)
          SOL = max(SOL, nezz + nezz);
        else
          SOL = max(SOL, nezz + nezz - 1);
      }
    }
  }
  if (ciklus())
    printf("-1\n");
  else
    printf("%d\n", SOL);
  return 0;
}