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 brd[2002][2002];
bool vis[2002][2002];
int qval[2002][2002];
int ends[2002][2002];
int dp[2002][2002];
pair<pair<int, int>, pair<int, int> > res[2002][2002];
int n, m;
int theend(int ci, int cj, int pi, int pj, pair<int, int>& e1,
           pair<int, int>& e2) {
  int& ans = dp[ci][cj];
  if (ans >= 0) {
    e1 = res[ci][cj].first;
    e2 = res[ci][cj].second;
    return ans;
  }
  int cci = ci, ccj = cj;
  ans = 0;
  while (brd[ci][cj] != '#') {
    if (brd[ci][cj] == '<') {
      pi = ci;
      pj = cj;
      cj--;
    } else if (brd[ci][cj] == '>') {
      pi = ci;
      pj = cj;
      cj++;
    } else if (brd[ci][cj] == '^') {
      pi = ci;
      pj = cj;
      ci--;
    } else if (brd[ci][cj] == 'v') {
      pi = ci;
      pj = cj;
      ci++;
    }
    ans++;
  }
  e1 = pair<int, int>(ci, cj);
  e2 = pair<int, int>(pi, pj);
  res[cci][ccj] = pair<pair<int, int>, pair<int, int> >(e1, e2);
  return ans;
}
bool degen() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (brd[i][j] != '#') return false;
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", brd[i]);
  if (degen()) {
    cout << "0\n";
    return 0;
  }
  bool hascycle = false;
  int qv = 0;
  for (int si = 0; si < n; si++)
    for (int sj = 0; sj < m; sj++) {
      if (vis[si][sj] || brd[si][sj] == '#') continue;
      qv++;
      int ci = si;
      int cj = sj;
      while (brd[ci][cj] != '#') {
        if (qval[ci][cj] == qv) {
          hascycle = true;
          break;
        }
        if (vis[si][sj]) break;
        qval[ci][cj] = qv;
        vis[si][sj] = true;
        if (brd[ci][cj] == '<') cj--;
        if (brd[ci][cj] == '>') cj++;
        if (brd[ci][cj] == '^') ci--;
        if (brd[ci][cj] == 'v') ci++;
      }
    }
  if (hascycle) {
    cout << "-1\n";
    return 0;
  }
  memset(qval, 0, sizeof(qval));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (brd[i][j] != '#') {
        if (brd[i][j] == '<') qval[i][j - 1]++;
        if (brd[i][j] == '>') qval[i][j + 1]++;
        if (brd[i][j] == '^') qval[i - 1][j]++;
        if (brd[i][j] == 'v') qval[i + 1][j]++;
      }
  memset(dp, -1, sizeof(dp));
  int mval = -1, mcnt = 0;
  set<pair<pair<int, int>, pair<int, int> > > ends;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (brd[i][j] != '#' && qval[i][j] == 0) {
        pair<int, int> x, y;
        int v = theend(i, j, i, j, x, y);
        if (v == mval) {
          mcnt++;
          ends.insert(pair<pair<int, int>, pair<int, int> >(x, y));
        } else if (v > mval) {
          mval = v;
          mcnt = 1;
          ends.clear();
          ends.insert(pair<pair<int, int>, pair<int, int> >(x, y));
        }
      }
  if (mcnt == 1 || ends.size() == 1) {
    cout << 2 * mval - 1 << endl;
  } else {
    cout << 2 * mval << endl;
  }
}