#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int n, m, maxy;
char a[2000][2000], b[2000][2000], c[2000][2000];

int check() {
	int ans = 0;
	for (int i = 1; i + 1 <= n; i++)
		for (int j = 1; j + 1 <= m; j++)
			if (a[i][j] == a[i + 1][j] && a[i][j] == a[i][j + 1] && a[i][j] == a[i + 1][j + 1])
				ans++;
	return ans;
}

int main() {
	//ifstream cin("in.txt");
	//ofstream cout("out.txt");
	ifstream cin("video.in");
	ofstream cout("video.out");
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> c[i][j];
	for (int x = 0; x <= 2; x++)
		for (int y = 0; y <= 2; y++) {
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= m; j++)
					b[i][j] = c[i][j];
			for (int i = 1; i <= n; i++) {
				for (int j = 1 + y; j <= m; j++)
					a[i][j] = b[i][j - y];
				for (int j = 1; j <= y; j++)
					a[i][j] = b[i][m + j - y];
			}
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= m; j++)
					b[i][j] = a[i][j];
			for (int i = 1; i <= m; i++) {
				for (int j = 1 + x; j <= n; j++)
					a[j][i] = b[j - x][i];
				for (int j = 1; j <= x; j++)
					a[j][i] = b[n + j - x][i];
			}
			maxy = max(maxy, check());
		}
	cout << maxy;
	return 0;
}