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>
int IntMaxVal = (int)1e20;
int IntMinVal = (int)-1e20;
long long LongMaxVal = (long long)1e20;
long long LongMinVal = (long long)-1e20;
template <typename T>
inline void minimize(T& a, T b) {
  a = std::min(a, b);
}
template <typename T>
inline void maximize(T& a, T b) {
  a = std::max(a, b);
}
using namespace std;
template <typename T>
struct argument_type;
template <typename T, typename U>
struct argument_type<T(U)> {};
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& s) {
  is >> s.first >> s.second;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (int i = 0; i < v.size(); i++) os << v[i] << ' ';
  os << '\n';
  return os;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& s, pair<T1, T2>& t) {
  s << t.first << ' ' << t.second;
  return s;
}
template <typename T>
vector<T> readVector(int n) {
  vector<T> res(n);
  for (int i = 0; i < n; i++) cin >> res[i];
  return res;
}
int n, m;
vector<string> f;
int ans1 = 0;
int ans2 = 0;
bool visited[2000 * 2000];
vector<int> order;
void dfs1(int v) {
  if (visited[v]) return;
  visited[v] = true;
  int y = v / m;
  int x = v % m;
  if (f[y][x] == '#')
    ;
  else if (f[y][x] == '>')
    dfs1(v + 1);
  else if (f[y][x] == '<')
    dfs1(v - 1);
  else if (f[y][x] == '^')
    dfs1(v - m);
  else
    dfs1(v + m);
  order.push_back(v);
}
bool dfs(int v) {
  visited[v] = true;
  if (v / m > 0 && f[v / m - 1][v % m] == 'v' && !visited[v - m]) return true;
  if (v / m < n - 1 && f[v / m + 1][v % m] == '^' && !visited[v + m])
    return true;
  if (v % m > 0 && f[v / m][v % m - 1] == '>' && !visited[v - 1]) return true;
  if (v % m < m - 1 && f[v / m][v % m + 1] == '<' && !visited[v + 1])
    return true;
  return false;
}
int main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  f.resize(n);
  for (int i = 0; i < n; ++i) cin >> f[i];
  order.reserve(n * m);
  for (int i = 0; i < n * m; ++i) dfs1(i);
  for (int i = 0; i < 2000 * 2000; ++i) visited[i] = false;
  for (int i = order.size() - 1; i >= 0; --i)
    if (!visited[order[i]]) {
      int v = order[i];
      if (dfs(v)) {
        cout << -1;
        return 0;
      }
    }
  vector<int> dist(n * m);
  vector<int> order2;
  order2.reserve(n * m);
  for (int i = 0; i < n * m; ++i) {
    int y = i / m;
    int x = i % m;
    if (f[y][x] == '#') continue;
    int dst = i;
    if (f[y][x] == '>')
      dst++;
    else if (f[y][x] == '<')
      dst--;
    else if (f[y][x] == '^')
      dst -= m;
    else
      dst += m;
    int dy = dst / m;
    int dx = dst % m;
    if (f[dy][dx] != '#') continue;
    order2.clear();
    order2.push_back(i);
    int val = 1;
    dist[i] = 1;
    for (int j = 0; j < order2.size(); ++j) {
      int v = order2[j];
      if (v / m > 0 && f[v / m - 1][v % m] == 'v') {
        int v2 = v - m;
        dist[v2] = 1 + dist[v];
        order2.push_back(v2);
        maximize(val, dist[v2]);
      }
      if (v / m < n - 1 && f[v / m + 1][v % m] == '^') {
        int v2 = v + m;
        dist[v2] = 1 + dist[v];
        order2.push_back(v2);
        maximize(val, dist[v2]);
      }
      if (v % m > 0 && f[v / m][v % m - 1] == '>') {
        int v2 = v - 1;
        dist[v2] = 1 + dist[v];
        order2.push_back(v2);
        maximize(val, dist[v2]);
      }
      if (v % m < m - 1 && f[v / m][v % m + 1] == '<') {
        int v2 = v + 1;
        dist[v2] = 1 + dist[v];
        order2.push_back(v2);
        maximize(val, dist[v2]);
      }
    }
    if (val > ans1) {
      ans2 = ans1;
      ans1 = val;
    } else if (val > ans2)
      ans2 = val;
    val--;
    if (val > ans1) {
      ans2 = ans1;
      ans1 = val;
    } else if (val > ans2)
      ans2 = val;
  }
  cout << ans1 + ans2;
}