#include <bits/stdc++.h>   using namespace std;   std::vector<string> v(2000);   vector<vector<int> > mark(2000);   set<pair<int, int> > ans;   int bfs(int x, int y) {     int m = x, n = y;     vector<pair<int, int> > q;     int i = 0;     while (v[x][y] !=  #  && mark[x][y] == -2) {       q.push_back(make_pair(x, y));       int Y = y, X = x;       Y -= (v[x][y] ==  < );       Y += (v[x][y] ==  > );       X -= (v[x][y] ==  ^ );       X += (v[x][y] ==  v );       mark[x][y] = -1;       x = X;       y = Y;     }     if (v[x][y] ==  # ) {       mark[x][y] = 0;       int cnt = 1;       for (i = q.size() - 1; i >= 0; i--) {         mark[q[i].first][q[i].second] = cnt + mark[x][y];         cnt++;       }     } else if (mark[x][y] == -1) {       for (i = 0; i < q.size(); i++) {         mark[q[i].first][q[i].second] = -1;       }     } else {       int cnt = 1;       for (i = q.size() - 1; i >= 0; i--) {         mark[q[i].first][q[i].second] = cnt + mark[x][y];         cnt++;       }     }     return mark[m][n];   }   void check() {     while (ans.size() != 1) {       vector<pair<int, int> > u;       set<pair<int, int> >::iterator it;       for (it = ans.begin(); it != ans.end(); it++) {         int x = (*it).first;         int y = (*it).second;         int X = x, Y = y;         Y -= (v[x][y] ==  < );         Y += (v[x][y] ==  > );         X -= (v[x][y] ==  ^ );         X += (v[x][y] ==  v );         if (v[X][Y] ==  # ) {           return;         }         u.push_back(make_pair(X, Y));       }       int i;       ans.clear();       for (i = 0; i < u.size(); i++) {         ans.insert(u[i]);       }     }   }   int main() {     int n, m;     cin >> n >> m;     int i, j;     for (i = 0; i < n; i++) {       cin >> v[i];     }     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         mark[i].push_back(-2);       }     }     int maxi = -100;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (mark[i][j] == -2) {           int a = bfs(i, j);           if (a == -1) {             cout <<  -1  << endl;             return 0;           }           if (maxi < a) {             ans.clear();             maxi = a;             ans.insert(make_pair(i, j));           } else if (maxi == a) {             ans.insert(make_pair(i, j));           }         }       }     }     mark.clear();     check();     if (ans.size() == 1) {       if (v[(*(ans.begin())).first][(*(ans.begin())).second] ==  # )         cout <<  0  << endl;       else         cout << 2 * maxi - 1 << endl;     } else {       cout << 2 * maxi << endl;     }     return 0;   }   