//穷举
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] str1 = in.nextLine().split(",");
int m = Integer.parseInt(str1[0]);
int n = Integer.parseInt(str1[1]);
String[][] s = new String[m][n];
for (int i = 0; i < m; i++) {
String[] s1 = in.nextLine().split(",");
for (int j = 0; j < n; j++) {
s[i][j] = s1[j];
}
}
List<Integer> list = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j].equals("M")) {
getResult(s, i, j, list);
}
}
}
Collections.sort(list);
System.out.println(list.get(list.size() - 1));
}
public static void getResult(String[][] s, int i, int j, List<Integer> list) {
int len = 1;
int a = 0, b = 0;
int m = s.length, n = s[0].length;
if (j < n) { // 从左往右
a = i;
b = j;
while (b < n - 1 && s[a][++b].equals("M")) {
len++;
}
list.add(len);
len = 1;
}
if (i < m) { // 从上往下
a = i;
b = j;
while (a < m - 1 && s[++a][b].equals("M")) {
len++;
}
list.add(len);
len = 1;
}
if (i < m && j < n) { // 对角线
a = i;
b = j;
while ((a < m - 1 && b < n - 1) && s[++a][++b].equals("M")) {
len++;
}
list.add(len);
len = 1;
}
if (i >= 0 && j < n) { // 从左往右
a = i;
b = j;
while (( a > 0 && b < n - 1) && s[--a][++b].equals("M")) {
len++;
}
list.add(len);
}
}