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>
#pragma GCC diagnostic ignored "-Wwrite-strings"
using namespace std;
template <template <typename, typename> class ContainerT, typename ValueT>
ostream &operator<<(ostream &out,
                    const ContainerT<ValueT, std::allocator<ValueT> > &A) {
  for (__typeof((A).begin()) i = (A).begin(); i != (A).end(); ++i)
    out << *i << " ";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &A) {
  out << A.first << "_" << A.second;
  return out;
}
template <class T>
void mini(T &a, const T &b) {
  if (b < a) a = b;
}
template <class T>
void maxi(T &a, const T &b) {
  if (b > a) a = b;
}
int N, M;
vector<vector<int> > board;
vector<vector<int> > life;
const pair<int, int> diff[4] = {make_pair(0, -1), make_pair(-1, 0),
                                make_pair(0, 1), make_pair(1, 0)};
int visited[2001][2001];
int comp;
bool dfs(int x, int y) {
  if (visited[x][y] == comp) return true;
  visited[x][y] = comp;
  int dir = board[x][y];
  if (dir != 4) {
    if (dfs(x + diff[dir].first, y + diff[dir].second))
      return true;
    else
      return false;
  }
  return false;
}
bool hasCycle() {
  comp = 0;
  memset(visited, 0, sizeof(visited));
  for (int i = 0; i < (N); ++i)
    for (int j = 0; j < (M); ++j) {
      if (!visited[i][j]) {
        comp++;
        if (dfs(i, j)) return true;
      }
    }
  return false;
}
int calcLife(int x, int y) {
  if (life[x][y] != -1) return life[x][y];
  life[x][y] = 0;
  int dir = board[x][y];
  if (dir != 4)
    maxi(life[x][y], 1 + calcLife(x + diff[dir].first, y + diff[dir].second));
  return life[x][y];
}
int getLife(pair<int, int> a) { return life[a.first][a.second]; }
bool race(pair<int, int> a, pair<int, int> b) {
  while (true) {
    if (board[a.first][a.second] == 4 and board[b.first][b.second] == 4)
      return true;
    if (a.first == b.first and a.second == b.second) return false;
    if (board[a.first][a.second] != 4) {
      int dir = board[a.first][a.second];
      a.first += diff[dir].first;
      a.second += diff[dir].second;
    }
    if (board[b.first][b.second] != 4) {
      int dir = board[b.first][b.second];
      b.first += diff[dir].first;
      b.second += diff[dir].second;
    }
  }
}
long long solve() {
  cin >> N >> M;
  board = vector<vector<int> >(N, vector<int>(M));
  life = vector<vector<int> >(N, vector<int>(M, -1));
  for (int i = 0; i < (N); ++i) {
    string temp;
    cin >> temp;
    for (int j = 0; j < (M); ++j) {
      if (temp[j] == '<') board[i][j] = 0;
      if (temp[j] == '^') board[i][j] = 1;
      if (temp[j] == '>') board[i][j] = 2;
      if (temp[j] == 'v') board[i][j] = 3;
      if (temp[j] == '#') board[i][j] = 4;
    }
  }
  if (hasCycle()) return -1;
  for (int i = 0; i < (N); ++i)
    for (int j = 0; j < (M); ++j) calcLife(i, j);
  pair<int, int> max1, max2;
  for (int i = 0; i < (N); ++i)
    for (int j = 0; j < (M); ++j)
      if (life[max1.first][max1.second] < life[i][j]) max1 = make_pair(i, j);
  for (int i = 0; i < (N); ++i)
    for (int j = 0; j < (M); ++j)
      if (not(i == max1.first and j == max1.second) and
          life[max2.first][max2.second] < life[i][j])
        max2 = make_pair(i, j);
  assert(getLife(max1) >= getLife(max2));
  if (getLife(max1) == getLife(max2)) {
    if (race(max1, max2)) return getLife(max1) * 2;
  }
  return getLife(max1) * 2 - 1;
}
int main() {
  cout << solve() << endl;
  return 0;
}