// Arup Guha
// 5/8/2018
// Solution to USACO 2015 US Open Bronze Problem: Moocryption

import java.util.*;
import java.io.*;

public class moocrypt {

	final public static int[] DX = {-1,-1,-1,0,0,1,1,1};
	final public static int[] DY = {-1,0,1,-1,1,-1,0,1};

	public static int r;
	public static int c;
	public static char[][] grid;

	public static void main(String[] args) throws Exception {

		// Read in data.
		BufferedReader stdin = new BufferedReader(new FileReader("moocrypt.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		r = Integer.parseInt(tok.nextToken());
		c = Integer.parseInt(tok.nextToken());
		grid = new char[r][];
		for (int i=0; i<r; i++)
			grid[i] = stdin.readLine().trim().toCharArray();

		// Store all mappings of strings of the form XYY here.
		HashMap<String,Integer> map = new HashMap<String,Integer>();

		// Try each starting spot.
		for (int i=0; i<r; i++) {
			for (int j=0; j<c; j++) {

				// Each direction.
				for (int k=0; k<DX.length; k++) {
					int eX = i + 2*DX[k];
					int eY = j + 2*DY[k];
					if (!inbounds(eX,eY)) continue;

					// Check all the mold rules.
					if (grid[i+DX[k]][j+DY[k]] != grid[i+2*DX[k]][j+2*DY[k]]) continue;
					if (grid[i+DX[k]][j+DY[k]] == grid[i][j]) continue;

					// This rule is silly, that you can't encrypt to yourself...
					if (grid[i][j] == 'M') continue;
					if (grid[i+DX[k]][j+DY[k]] == 'O') continue;

					// This is ugly...but with copy paste it's quick =)
					String tmp = "" + grid[i][j] + grid[i+DX[k]][j+DY[k]] + grid[i+2*DX[k]][j+2*DY[k]];

					// Put this in our map.
					if (map.containsKey(tmp))
						map.put(tmp, map.get(tmp)+1);
					else
						map.put(tmp, 1);
				}
			}
		}

		// Find the most frequent pattern.
		int res = 0;
		for (String s : map.keySet())
			res = Math.max(res, map.get(s));

		// Write result.
		PrintWriter out = new PrintWriter(new FileWriter("moocrypt.out"));
		out.println(res);
		out.close();
		stdin.close();
	}



	public static boolean inbounds(int x, int y) {
		return x >= 0 && x < r && y >= 0 && y < c;
	}
}