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;
const int MAXN = 2e3 + 123;
int r, s;
char grid[MAXN][MAXN];
int longest[MAXN][MAXN];
bool next(int x, int y, int& nx, int& ny) {
  nx = x;
  ny = y;
  if (grid[x][y] == 'v') {
    ++nx;
    return true;
  }
  if (grid[x][y] == '^') {
    --nx;
    return true;
  }
  if (grid[x][y] == '<') {
    --ny;
    return true;
  }
  if (grid[x][y] == '>') {
    ++ny;
    return true;
  }
  return false;
}
int solve(int x, int y) {
  int orig_x = x, orig_y = y;
  struct Pt {
    int x, y;
  };
  static vector<Pt> V;
  V.clear();
  for (; longest[x][y] == 0 && grid[x][y] != '#';) {
    longest[x][y] = -1;
    V.push_back({x, y});
    int nx, ny;
    next(x, y, nx, ny);
    x = nx;
    y = ny;
  }
  if (longest[x][y] == -1) return -1;
  int step = longest[x][y] + 1;
  for (int i = V.size() - 1; i >= 0; --i, ++step) {
    longest[V[i].x][V[i].y] = step;
  }
  return longest[orig_x][orig_y];
}
int simulate(int x1, int y1, int x2, int y2) {
  int step = 0;
  while (true) {
    int nx1, ny1;
    int move1 = next(x1, y1, nx1, ny1);
    int nx2, ny2;
    int move2 = next(x2, y2, nx2, ny2);
    if (grid[nx1][ny1] != '#' && nx1 == nx2 && ny1 == ny2) {
      assert(move1 && move2);
      --step;
      x1 = nx1;
      y1 = ny1;
    } else {
      step += move1 + move2;
      x1 = nx1;
      y1 = ny1;
      x2 = nx2;
      y2 = ny2;
    }
    if (move1 == 0 && move2 == 0) break;
  }
  return step;
}
int main(void) {
  scanf("%d %d", &r, &s);
  for (int i = 0; i < (r); ++i) scanf("%s", grid[i]);
  memset(longest, 0, sizeof longest);
  struct S {
    int x, y, c;
  };
  vector<S> V;
  for (int i = 0; i < (r); ++i)
    for (int j = 0; j < (s); ++j) {
      if (grid[i][j] != '#') {
        if (longest[i][j] == 0) {
          if (solve(i, j) == -1) {
            puts("-1");
            return 0;
          }
        }
        V.push_back({i, j, longest[i][j]});
      }
    }
  int best = 0;
  sort(V.begin(), V.end(), [](const S& a, const S& b) { return a.c > b.c; });
  if (V.size() > 4) V.resize(4);
  int vsz = V.size();
  for (int a = 0; a < vsz; ++a)
    for (int b = a + 1; b < vsz; ++b) {
      best = max(best, simulate(V[a].x, V[a].y, V[b].x, V[b].y));
    }
  printf("%d\n", best);
  return 0;
}