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;
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<string> deck(n);
  string tmp;
  getline(cin, tmp);
  for (int r = 0; r < n; ++r) {
    getline(cin, deck[r]);
  }
  int row = 0, column = 0;
  int cur_comp = 1;
  int max_length = 0;
  int longest_comp = -1;
  vector<int> visited(n * m, 0);
  vector<int> lp(n * m, 0);
  while (1) {
    bool unvisited_found = false;
    int r = row;
    int c = column;
    for (; c < m; ++c) {
      if (deck[row][c] != '#' && !visited[row * m + c]) {
        unvisited_found = true;
        break;
      }
    }
    if (!unvisited_found) {
      c = 0;
      ++r;
      for (; r < n; ++r) {
        for (c = 0; c < m; ++c) {
          if (deck[r][c] != '#' && !visited[r * m + c]) {
            unvisited_found = true;
            break;
          }
        }
        if (unvisited_found) {
          break;
        }
      }
    }
    if (!unvisited_found) {
      break;
    }
    column = (c + 1) % m;
    row = r;
    if (!column) {
      ++row;
    }
    int index = r * m + c;
    int long_path = 0;
    int actual_component = cur_comp;
    visited[index] = cur_comp;
    stack<int> S;
    while (deck[r][c] != '#') {
      S.push(index);
      if (deck[r][c] == '<') {
        --c;
      } else if (deck[r][c] == '>') {
        ++c;
      } else if (deck[r][c] == '^') {
        --r;
      } else if (deck[r][c] == 'v') {
        ++r;
      }
      index = r * m + c;
      if (visited[index]) {
        if (visited[index] == cur_comp) {
          cout << -1 << endl;
          return 0;
        }
        long_path = lp[index];
        if (deck[r][c] != '#') {
          actual_component = visited[index];
        }
        break;
      } else {
        visited[index] = cur_comp;
      }
    }
    while (!S.empty()) {
      index = S.top();
      S.pop();
      ++long_path;
      lp[index] = long_path;
      visited[index] = actual_component;
    }
    if (long_path > max_length) {
      max_length = long_path;
      longest_comp = actual_component;
    }
    if (actual_component == cur_comp) {
      ++cur_comp;
    }
  }
  int res = 2 * max_length;
  bool res_ok = false;
  int not_blocked_count = 0;
  for (int r = 0; r < n; ++r) {
    for (int c = 0; c < m; ++c) {
      not_blocked_count += int(deck[r][c] != '#');
      if (lp[r * m + c] == max_length && visited[r * m + c] != longest_comp) {
        res_ok = true;
      }
    }
  }
  if (!res_ok || not_blocked_count <= 1) {
    --res;
  }
  cout << max(res, 0) << endl;
  return 0;
}