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;
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.push_back(x);
  return v;
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\n';
  err(++it, args...);
}
const int MOD = 1000000007;
template <class T, class U>
inline T tmin(T a, U b) {
  return (a < b) ? a : b;
}
template <class T, class U>
inline T tmax(T a, U b) {
  return (a > b) ? a : b;
}
template <class T, class U>
inline void amax(T &a, U b) {
  if (b > a) a = b;
}
template <class T, class U>
inline void amin(T &a, U b) {
  if (b < a) a = b;
}
template <class T>
inline T tabs(T a) {
  return (a > 0) ? a : -a;
}
template <class T>
T gcd(T a, T b) {
  while (b != 0) {
    T c = a;
    a = b;
    b = c % b;
  }
  return a;
}
const int N = 2005;
int n, m, vis[N][N];
char b[N][N];
bool found;
vector<int> G[N * N];
vector<int> dst;
inline bool inside(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
void dfs(int x, int y) {
  vis[x][y] = 0;
  if (b[x][y] == '>' && inside(x, y + 1) && b[x][y + 1] != '#') {
    if (vis[x][y + 1] == -1)
      dfs(x, y + 1);
    else {
      vis[x][y] = 1;
      if (vis[x][y + 1] == 0) found = 1;
    }
  }
  if (b[x][y] == '<' && inside(x, y - 1) && b[x][y - 1] != '#') {
    if (vis[x][y - 1] == -1)
      dfs(x, y - 1);
    else {
      vis[x][y] = 1;
      if (vis[x][y - 1] == 0) found = 1;
    }
  }
  if (b[x][y] == 'v' && inside(x + 1, y) && b[x + 1][y] != '#') {
    if (vis[x + 1][y] == -1)
      dfs(x + 1, y);
    else {
      vis[x][y] = 1;
      if (vis[x + 1][y] == 0) found = 1;
    }
  }
  if (b[x][y] == '^' && inside(x - 1, y) && b[x - 1][y] != '#') {
    if (vis[x - 1][y] == -1)
      dfs(x - 1, y);
    else {
      vis[x][y] = 1;
      if (vis[x - 1][y] == 0) {
        found = 1;
      }
    }
  }
  vis[x][y] = 1;
}
void go(int v, int d) {
  for (int i = (0); i < (G[v].size()); ++i) {
    go(G[v][i], d + 1);
  }
  if (G[v].size() == 0) {
    dst.push_back(d);
  }
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = (0); i < (n); ++i) cin >> b[i];
  found = 0;
  memset(vis, -1, sizeof(vis));
  for (int i = (1); i < (n - 1); ++i)
    for (int j = (1); j < (m - 1); ++j) {
      if (vis[i][j] == -1 && b[i][j] != '#') {
        dfs(i, j);
      }
    }
  if (found) {
    printf("-1\n");
    return 0;
  }
  int ret = 0;
  vector<int> root;
  for (int i = (1); i < (n - 1); ++i)
    for (int j = (1); j < (m - 1); ++j) {
      if (b[i][j] == '>' && inside(i, j + 1)) {
        if (b[i][j + 1] == '#')
          root.push_back(N * i + j);
        else {
          G[N * i + j + 1].push_back(N * i + j);
        }
      }
      if (b[i][j] == '<' && inside(i, j + 1)) {
        if (b[i][j - 1] == '#')
          root.push_back(N * i + j);
        else {
          G[N * i + j - 1].push_back(N * i + j);
        }
      }
      if (b[i][j] == 'v' && inside(i + 1, j)) {
        if (b[i + 1][j] == '#')
          root.push_back(N * i + j);
        else {
          G[N * (i + 1) + j].push_back(N * i + j);
        }
      }
      if (b[i][j] == '^' && inside(i - 1, j)) {
        if (b[i - 1][j] == '#')
          root.push_back(N * i + j);
        else {
          G[N * (i - 1) + j].push_back(N * i + j);
        }
      }
    }
  vector<int> maxi;
  for (int i = (0); i < (root.size()); ++i) {
    dst.clear();
    go(root[i], 1);
    sort(dst.begin(), dst.end());
    reverse(dst.begin(), dst.end());
    amax(ret, 2 * dst[0] - 1);
    maxi.push_back(dst[0]);
  }
  if (maxi.size()) {
    sort(maxi.begin(), maxi.end());
    reverse(maxi.begin(), maxi.end());
    if (maxi.size() > 1) {
      amax(ret, maxi[0] + maxi[1]);
    }
  }
  printf("%d\n", ret);
  return 0;
}