const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
const [len, tate] = (await readline()).split(",").map(Number);
const matrix = [];
for (let i = 0; i < len; i++) {
matrix.push((await readline()).split(","));
}
const offset = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
];
let ans = 0;
for (let i = 0; i < len; i++) {
for (let j = 0; j < tate; j++) {
if (matrix[i][j] == "M") {
for (let [offsetX, offsetY] of offset) {
let x = i + offsetX;
let y = j + offsetY;
let num = 1;
while (
x >= 0 &&
x < len &&
y >= 0 &&
y < tate &&
matrix[x][y] == "M"
) {
num++;
x += offsetX;
y += offsetY;
}
ans = Math.max(ans, num);
}
}
}
}
console.log(ans);
})();