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 board[2100][2100];
int mark[2100][2100];
class Graph {
  int V;
  list<int> *adj;
  bool isCyclicUtil(int v, bool visited[], bool *rs);

 public:
  Graph(int V);
  void addEdge(int v, int w);
  bool isCyclic();
};
Graph::Graph(int V) {
  this->V = V;
  adj = new list<int>[V];
}
void Graph::addEdge(int v, int w) { adj[v].push_back(w); }
bool Graph::isCyclicUtil(int v, bool visited[], bool *recStack) {
  if (visited[v] == false) {
    visited[v] = true;
    recStack[v] = true;
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i) {
      if (!visited[*i] && isCyclicUtil(*i, visited, recStack))
        return true;
      else if (recStack[*i])
        return true;
    }
  }
  recStack[v] = false;
  return false;
}
bool Graph::isCyclic() {
  bool *visited = new bool[V];
  bool *recStack = new bool[V];
  for (int i = 0; i < V; i++) {
    visited[i] = false;
    recStack[i] = false;
  }
  for (int i = 0; i < V; i++)
    if (isCyclicUtil(i, visited, recStack)) return true;
  return false;
}
int move(int i, int j) {
  int i0 = i, j0 = j;
  if (board[i][j] == '#') return 0;
  int di = 0, dj = 0;
  if (mark[i][j] != 0) {
    return mark[i][j];
  }
  di = 0;
  dj = 0;
  if (board[i][j] == 'v') di = 1;
  if (board[i][j] == '^') di = -1;
  if (board[i][j] == '>') dj = 1;
  if (board[i][j] == '<') dj = -1;
  i += di;
  j += dj;
  if (mark[i][j] != 0) {
    mark[i0][j0] = mark[i][j] + 1;
    return mark[i][j] + 1;
  } else {
    int d = move(i, j);
    mark[i0][j0] = d + 1;
    return d + 1;
  }
}
bool check(int i1, int j1, int i2, int j2) {
  if (board[i1][j1] == '#' || board[i1][j2] == '#') return false;
  int di1 = 0, dj1 = 0, di2 = 0, dj2 = 0;
  while (board[i1][j1] != '#' || board[i2][j2] != '#') {
    di1 = 0;
    dj1 = 0, di2 = 0, dj2 = 0;
    if (board[i1][j1] == 'v') di1 = 1;
    if (board[i1][j1] == '^') di1 = -1;
    if (board[i1][j1] == '>') dj1 = 1;
    if (board[i1][j1] == '<') dj1 = -1;
    i1 += di1;
    j1 += dj1;
    if (board[i2][j2] == 'v') di2 = 1;
    if (board[i2][j2] == '^') di2 = -1;
    if (board[i2][j2] == '>') dj2 = 1;
    if (board[i2][j2] == '<') dj2 = -1;
    i2 += di2;
    j2 += dj2;
    if (i1 == i2 && j1 == j2)
      if (board[i1][j1] != '#') return false;
  }
  return true;
}
Graph g(2000 * 2000);
int main() {
  int n, m, max = 0, val = 0, maxi[2000], maxj[2000], num = 0, cnt = 0;
  bool f = false;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> board[i][j];
      if (board[i][j] == 'v' && cnt < 10000) {
        g.addEdge(m * i + j, m * (i + 1) + j);
        cnt++;
      }
      if (board[i][j] == '^' && cnt < 10000) {
        g.addEdge(m * i + j, m * (i - 1) + j);
        cnt++;
      }
      if (board[i][j] == '>' && cnt < 10000) {
        g.addEdge(m * i + j, m * i + j + 1);
        cnt++;
      }
      if (board[i][j] == '<' && cnt < 10000) {
        g.addEdge(m * i + j, m * i + j - 1);
        cnt++;
      }
    }
  }
  if (g.isCyclic()) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      val = move(i, j);
      if (val == max && !f && max != 0) {
        maxj[num] = j;
        maxi[num] = i;
        num++;
      }
      if (val > max) {
        max = val;
        maxi[0] = i;
        maxj[0] = j;
        num = 1;
        f = false;
      }
    }
  }
  if (max == 0) {
    cout << 0;
    return 0;
  }
  if (num > 1) f = check(maxi[0], maxj[0], maxi[1], maxj[1]);
  if (num > 2) {
    for (int i = 2; i < num; i++) {
      if (!f) {
        f = check(maxi[1], maxj[1], maxi[i], maxj[i]);
      }
    }
  }
  if (f)
    cout << max * 2;
  else
    cout << max * 2 - 1;
}