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 di[] = {-1, 1, 0, 0};
const int dj[] = {0, 0, -1, 1};
const char dc[] = {'^', 'v', '<', '>'};
const int MN = 2011;
int m, n, cur;
char a[MN][MN];
int id[MN][MN];
int next_[MN * MN], to[MN * MN], head[MN * MN];
int visited[MN * MN], vao[MN * MN], f1[MN * MN];
bool dfs1(int u, int fu) {
  visited[u] = 1;
  for (int p = head[u]; p >= 0; p = next_[p]) {
    int v = to[p];
    if (!visited[v]) {
      if (dfs1(v, u)) return true;
    } else if (visited[v] == 1)
      return true;
  }
  visited[u] = 2;
  return false;
}
void dfs2(int u) {
  f1[u] = visited[u];
  for (int p = head[u]; p >= 0; p = next_[p]) {
    int v = to[p];
    if (!visited[v]) {
      visited[v] = visited[u] + 1;
      dfs2(v);
    }
    if (f1[v] > f1[u]) f1[u] = f1[v];
  }
}
bool hasCycle() {
  memset(visited, 0, sizeof visited);
  for (int i = (1), _b = (cur); i <= _b; i++)
    if (!visited[i]) {
      if (dfs1(i, -1)) return true;
    }
  return false;
}
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(NULL);
  cout << (fixed) << setprecision(6);
  while (cin >> m >> n) {
    for (int i = (1), _b = (m); i <= _b; i++)
      for (int j = (1), _b = (n); j <= _b; j++) cin >> a[i][j];
    cur = 0;
    for (int i = (1), _b = (m); i <= _b; i++)
      for (int j = (1), _b = (n); j <= _b; j++)
        if (a[i][j] != '#') {
          id[i][j] = ++cur;
        }
    if (cur < 2) {
      cout << 0 << endl;
      continue;
    }
    memset(vao, 0, sizeof vao);
    memset(head, -1, sizeof head);
    memset(next_, -1, sizeof next_);
    memset(to, 0, sizeof to);
    int nEdges = 0;
    for (int i = (1), _b = (m); i <= _b; i++)
      for (int j = (1), _b = (n); j <= _b; j++)
        if (a[i][j] != '#') {
          for (int dir = 0, _a = (4); dir < _a; dir++)
            if (dc[dir] == a[i][j]) {
              int ii = i + di[dir], jj = j + dj[dir];
              if (a[ii][jj] != '#') {
                int u = id[i][j], v = id[ii][jj];
                swap(u, v);
                next_[nEdges] = head[u];
                to[nEdges] = v;
                head[u] = nEdges++;
                vao[v]++;
              }
            }
        }
    if (hasCycle()) {
      cout << -1 << endl;
      continue;
    }
    memset(visited, 0, sizeof visited);
    int ln1 = 0, ln2 = 0, res = 0;
    for (int i = (1), _b = (cur); i <= _b; i++)
      if (!vao[i]) {
        visited[i] = 1;
        dfs2(i);
        res = max(res, f1[i] + f1[i] - 1);
        if (f1[i] > ln1) {
          ln2 = ln1;
          ln1 = f1[i];
        } else if (f1[i] > ln2) {
          ln2 = f1[i];
        }
        res = max(res, ln1 + ln2);
      }
    cout << res << endl;
  }
  return 0;
}