10 10
.L.....R..
.........U
..D....D..
.R......L.
....L.....
.........D
......U...
D....R....
...U......
........L.
 5 5
DD..D
.....
LU...
U...U
.UL.U
 1 1
U
 5 5
UD..L
RRD..
..RDD
UUL..
DDLRR
 19 23
U..U..R........L...D...
.D.....UL.U.U........DR
..D.U..D....L......R...
D.......RL.L....D.....R
U..D.....U...U.L..D...U
...........L...........
U....U....D.U......LRR.
......RL...U...L.......
DLU..U......UU.U.U.D...
...........D....U......
U...DL......LL.....R...
...U..DD..........LD...
.R.........D.....UU...U
....D......R..........D
.L..........R.....L....
.R........L.LL....R....
..R...D........R....D..
.LR.R..LDU.DDRL......U.
...LL.....D.....DD.....
 11 29
URRUUDDLURLRDDLLRLDUDR.UDLLD.
LRRUDLUDLDULUDL.DURDRR.RRLLRR
D.DUDDURLRDDULDUDDUDDURRDLD.L
.RULURURRDLRRRLUUUU.RDLDLRRRL
.RLDDU.UUULUURURRLLUU.RLDRDUD
LRL.URRRDDURUURUDRRDLDDULRDRL
RLULDU.LRDUDRLDLRDDDDRUUDRDLL
DLUDURRDDUDLLDLLUURLDRUURRLUL
L.RRURRDDRRURRRDDUDRRDULR.LUR
LDLRULURRUULRDUUULLU.LLU.LLLL
DUURDULDDUDLRUUDRUULURLD.RL.U
 2 2
RD
UL
 1 10
LLLLLRRRRR
 3 3
...
...
..R
 17 13
...LD.UR.RUU.
LDLULDUDRRDRL
DD.RRLRUR.UD.
RU...D...LD..
.UR.R...URUUD
.RURD.RLR.D.R
.UUU.RU...UR.
UUUU.R.......
RL.LRU.UD.DUD
LD.DRURRLRDRL
.DR..UULUDDR.
LRLURRUUDDLUR
.LRDDLLR.L.DU
DRUDLLD.LRRU.
..DL.UDL.DUL.
U.DL..D..U.RD
RR.R...RL.L..

2 2
 4 1
 1 1
 8 3
 9 1
 94 1
 4 4
 5 2
 1 1
 24 2

#include <bits/stdc++.h>
char a[5000][5000];
int b[5000][5000] = {0};
int n;
int m;
bool u[5001][5001] = {0};
int count(int r, int c) {
  int i;
  if (r < 0 || c < 0 || r >= n || c >= m) return 0;
  int t = 0, x = -1;
  if (b[r][c] == 'D') {
    for (i = r + 1; i < n; i++) {
      if (!u[i][c] && a[i][c] != '.') {
        u[i][c] = 1;
        t = count(i, c);
        if (x < t) x = t;
      }
    }
  }
  if (b[r][c] == 'U')
    for (i = r - 1; i > 0; i--) {
      if (!u[i][c] && a[i][c] != '.') {
        u[i][c] = 1;
        t = count(i, c);
        if (x < t) x = t;
      }
    }
  if (b[r][c] == 'L')
    for (i = c - 1; i > 0; i--) {
      if (!u[r][i] && a[r][i] != '.') {
        u[r][i] = 1;
        t = count(r, i);
        if (x < t) x = t;
      }
    }
  if (b[r][c] == 'R')
    for (i = c + 1; i < m; i++) {
      if (!u[r][i] && a[r][i] != '.') {
        u[r][i] = 1;
        t = count(r, i);
        if (x < t) x = t;
      }
    }
  return x + 1;
}
int main() {
  int i, j, max = -1, z;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) scanf("%s", a[i]);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      memset(u, 0, sizeof(u));
      z = count(i, j);
      if (max < z) max = z;
    }
  printf("%d\n", max);
  return 0;
}
