// ~~ icebear love attttttt ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "robot"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2000 + 5;
int n, m, c[N][N], f[N][N];
char a[N][N];
ii nxt[N][N];
void init(void) {
cin >> n >> m;
FOR(i, 1, n) FOR(j, 1, m) {
cin >> a[i][j];
int x = i, y = j;
if (a[i][j] == 'L') y--;
if (a[i][j] == 'R') y++;
if (a[i][j] == 'U') x--;
if (a[i][j] == 'D') x++;
nxt[i][j] = mp(x, y);
}
}
void process(void) {
int ans = 0;
stack<ii> st;
FOR(i, 1, n) FOR(j, 1, m) if (c[i][j] == 0) {
f[i][j] = 1;
int x = i, y = j;
int ep = 0;
bool isCycle = false;
while(true) {
c[x][y] = 1;
st.push(mp(x, y));
int u, v; tie(u, v) = nxt[x][y];
if (u <= 0 || u > n || v <= 0 || v > m) break;
if (c[u][v] == 0) {
f[u][v] = f[x][y] + 1;
x = u; y = v;
}
if (c[u][v] == 1) {
isCycle = true;
break;
}
if (c[u][v] == 2) {
f[x][y] += (ep = f[u][v]);
break;
}
}
tie(x, y) = st.top();
ans = max(ans, f[x][y]);
if (isCycle) ep = f[x][y];
while(!st.empty()) {
tie(x, y) = st.top(); st.pop();
c[x][y] = 2;
f[x][y] = (isCycle ? ep : ++ep);
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Ly8gfn4gaWNlYmVhciBsb3ZlIGF0dHR0dHR0IH5+CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgcGFpcjxpbnQsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxpbnQsIGlpPiBpaWk7Cgp0ZW1wbGF0ZTxjbGFzcyBUPgogICAgYm9vbCBtaW5pbWl6ZShUICZhLCBjb25zdCBUICZiKSB7CiAgICAgICAgaWYgKGEgPiBiKSByZXR1cm4gYSA9IGIsIHRydWU7CiAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgfQogICAgCnRlbXBsYXRlPGNsYXNzIFQ+CiAgICBib29sIG1heGltaXplKFQgJmEsIGNvbnN0IFQgJmIpIHsKICAgICAgICBpZiAoYSA8IGIpIHJldHVybiBhID0gYiwgdHJ1ZTsKICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9CiAgICAKI2RlZmluZSBGT1IoaSxhLGIpIGZvcihpbnQgaT0oYSk7IGk8PShiKTsgKytpKQojZGVmaW5lIEZPUlIoaSxhLGIpIGZvcihpbnQgaT0oYSk7IGk+PShiKTsgLS1pKQojZGVmaW5lIFJFUChpLCBuKSBmb3IoaW50IGk9MDsgaTwobik7ICsraSkKI2RlZmluZSBSRUQoaSwgbikgZm9yKGludCBpPShuKS0xOyBpPj0wOyAtLWkpCiNkZWZpbmUgTUFTSyhpKSAoMUxMIDw8IChpKSkKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBmaSBmaXJzdAojZGVmaW5lIHNlIHNlY29uZAojZGVmaW5lIGFsbCh4KSB4LmJlZ2luKCksIHguZW5kKCkKI2RlZmluZSB0YXNrICJyb2JvdCIKCmNvbnN0IGludCBNT0QgPSAxZTkgKyA3Owpjb25zdCBpbnQgaW5mID0gMWU5ICsgMjcwOTIwMDg7CmNvbnN0IGxsIElORiA9IDFlMTggKyAyNzA5MjAwODsKY29uc3QgaW50IE4gPSAyMDAwICsgNTsKaW50IG4sIG0sIGNbTl1bTl0sIGZbTl1bTl07CmNoYXIgYVtOXVtOXTsKaWkgbnh0W05dW05dOwoKdm9pZCBpbml0KHZvaWQpIHsKICAgIGNpbiA+PiBuID4+IG07CiAgICBGT1IoaSwgMSwgbikgRk9SKGosIDEsIG0pIHsKICAgICAgICBjaW4gPj4gYVtpXVtqXTsKICAgICAgICBpbnQgeCA9IGksIHkgPSBqOwogICAgICAgIGlmIChhW2ldW2pdID09ICdMJykgeS0tOwogICAgICAgIGlmIChhW2ldW2pdID09ICdSJykgeSsrOwogICAgICAgIGlmIChhW2ldW2pdID09ICdVJykgeC0tOwogICAgICAgIGlmIChhW2ldW2pdID09ICdEJykgeCsrOwogICAgICAgIG54dFtpXVtqXSA9IG1wKHgsIHkpOwogICAgfQp9Cgp2b2lkIHByb2Nlc3Modm9pZCkgewogICAgaW50IGFucyA9IDA7CiAgICBzdGFjazxpaT4gc3Q7CiAgICBGT1IoaSwgMSwgbikgRk9SKGosIDEsIG0pIGlmIChjW2ldW2pdID09IDApIHsKICAgICAgICBmW2ldW2pdID0gMTsKICAgICAgICBpbnQgeCA9IGksIHkgPSBqOwogICAgICAgIGludCBlcCA9IDA7CiAgICAgICAgYm9vbCBpc0N5Y2xlID0gZmFsc2U7CiAgICAgICAgd2hpbGUodHJ1ZSkgewogICAgICAgICAgICBjW3hdW3ldID0gMTsKICAgICAgICAgICAgc3QucHVzaChtcCh4LCB5KSk7CiAgICAgICAgICAgIGludCB1LCB2OyB0aWUodSwgdikgPSBueHRbeF1beV07CiAgICAgICAgICAgIGlmICh1IDw9IDAgfHwgdSA+IG4gfHwgdiA8PSAwIHx8IHYgPiBtKSBicmVhazsKICAgICAgICAgICAgaWYgKGNbdV1bdl0gPT0gMCkgewogICAgICAgICAgICAgICAgZlt1XVt2XSA9IGZbeF1beV0gKyAxOwogICAgICAgICAgICAgICAgeCA9IHU7IHkgPSB2OwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChjW3VdW3ZdID09IDEpIHsKICAgICAgICAgICAgICAgIGlzQ3ljbGUgPSB0cnVlOwogICAgICAgICAgICAgICAgYnJlYWs7ICAgIAogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChjW3VdW3ZdID09IDIpIHsKICAgICAgICAgICAgICAgIGZbeF1beV0gKz0gKGVwID0gZlt1XVt2XSk7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgICAgICB0aWUoeCwgeSkgPSBzdC50b3AoKTsKICAgICAgICBhbnMgPSBtYXgoYW5zLCBmW3hdW3ldKTsKICAgICAgICBpZiAoaXNDeWNsZSkgZXAgPSBmW3hdW3ldOwogICAgICAgIAogICAgICAgIHdoaWxlKCFzdC5lbXB0eSgpKSB7CiAgICAgICAgICAgIHRpZSh4LCB5KSA9IHN0LnRvcCgpOyBzdC5wb3AoKTsKICAgICAgICAgICAgY1t4XVt5XSA9IDI7CiAgICAgICAgICAgIGZbeF1beV0gPSAoaXNDeWNsZSA/IGVwIDogKytlcCk7CiAgICAgICAgfSAgICAgICAgCiAgICB9CiAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwp9CgppbnQgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIGlmIChmb3Blbih0YXNrIi5pbnAiLCAiciIpKSB7CiAgICAgICAgZnJlb3Blbih0YXNrIi5pbnAiLCAiciIsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKHRhc2siLm91dCIsICJ3Iiwgc3Rkb3V0KTsKICAgIH0KICAgIGludCB0YyA9IDE7Ci8vICAgY2luID4+IHRjOwogICAgd2hpbGUodGMtLSkgewogICAgICAgIGluaXQoKTsKICAgICAgICBwcm9jZXNzKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQo=