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 lsone(int n) { return (n & -n); }
void mult(long long int a[25][25], long long int b[25][25],
          long long int c[25][25], int m, int n, int p) {
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= p; j++) {
      c[i][j] = 0;
      for (int k = 1; k <= n; k++) {
        c[i][j] += (a[i][k] * b[k][j]) % 1000000007;
        c[i][j] %= 1000000007;
      }
    }
  }
}
void mat_pow(long long int a[25][25], long long int c[25][25], int n,
             long long int p) {
  if (p == 0) {
    for (int i = 1; i <= n; i++) c[i][i] = 1;
  } else if (p == 1) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) c[i][j] = a[i][j];
    }
  } else {
    long long int d[25][25];
    mat_pow(a, d, n, p / 2);
    if (p % 2) {
      long long int e[25][25];
      mult(d, d, e, n, n, n);
      mult(e, a, c, n, n, n);
    } else {
      mult(d, d, c, n, n, n);
    }
  }
}
long long int pow1(long long int a, long long int b) {
  if (b == 0)
    return 1ll;
  else if (b == 1)
    return a;
  else {
    long long int x = pow1(a, b / 2);
    x *= x;
    x %= 1000000007;
    if (b % 2) {
      x *= a;
      x %= 1000000007;
    }
    return x;
  }
}
int n, m, moves[2100][2100], vis[2100][2100], cyc = 0;
pair<int, pair<int, int> > dest[2100][2100];
char grid[2100][2100];
vector<pair<int, pair<int, int> > > ans;
void dfs(int x, int y) {
  vector<pair<int, int> > v;
  int a = x, b = y;
  while (grid[x][y] != '#' and moves[x][y] == 0) {
    if (vis[x][y]) {
      cyc = 1;
      return;
    }
    v.push_back(make_pair(x, y));
    vis[x][y] = 1;
    if (grid[x][y] == '^')
      x--;
    else if (grid[x][y] == 'v')
      x++;
    else if (grid[x][y] == '<')
      y--;
    else
      y++;
  }
  for (int i = v.size() - 1; i >= 0; i--) {
    x = v[i].first, y = v[i].second;
    if (grid[x][y] == '^') {
      if (grid[x - 1][y] != '#') {
        moves[x][y] = 1 + moves[x - 1][y];
        dest[x][y] = dest[x - 1][y];
      } else {
        moves[x][y] = 1;
        dest[x][y] = make_pair(1, make_pair(x - 1, y));
      }
    } else if (grid[x][y] == 'v') {
      if (grid[x + 1][y] != '#') {
        moves[x][y] = 1 + moves[x + 1][y];
        dest[x][y] = dest[x + 1][y];
      } else {
        moves[x][y] = 1;
        dest[x][y] = make_pair(2, make_pair(x + 1, y));
      }
    } else if (grid[x][y] == '<') {
      if (grid[x][y - 1] != '#') {
        moves[x][y] = 1 + moves[x][y - 1];
        dest[x][y] = dest[x][y - 1];
      } else {
        moves[x][y] = 1;
        dest[x][y] = make_pair(3, make_pair(x, y - 1));
      }
    } else {
      if (grid[x][y + 1] != '#') {
        moves[x][y] = 1 + moves[x][y + 1];
        dest[x][y] = dest[x][y + 1];
      } else {
        moves[x][y] = 1;
        dest[x][y] = make_pair(4, make_pair(x, y + 1));
      }
    }
    ans.push_back(make_pair(moves[x][y], make_pair(x, y)));
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) cin >> grid[i][j];
  }
  for (int i = 1; i <= n and !cyc; i++) {
    for (int j = 1; j <= m; j++) {
      if (grid[i][j] != '#' and moves[i][j] == 0) {
        dfs(i, j);
        if (cyc) break;
      }
    }
  }
  if (cyc) {
    cout << -1 << "\n";
    return 0;
  }
  if (ans.size() <= 1) {
    cout << 0 << "\n";
    return 0;
  }
  sort(ans.begin(), ans.end());
  int l = ans.size();
  long long int ans1 = ans[l - 1].first, flag = 0;
  pair<int, pair<int, int> > temp =
      dest[ans[l - 1].second.first][ans[l - 1].second.second];
  for (int i = l - 2; i >= 0; i--) {
    int x = ans[i].second.first, y = ans[i].second.second;
    if (dest[x][y] != temp or (moves[x][y] != ans1)) {
      ans1 += ans[i].first;
      flag = 1;
      break;
    }
  }
  if (!flag) {
    cout << 0 << "\n";
  } else
    cout << ans1 << "\n";
  return 0;
}