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

using namespace std;

int n, m, maxy, b[5000][5000], sum1[5000], sum[5000][5000];
char a[5000][5000], c[5000][5000];

int f(int a, int b) {
	if (a != b)
		return a + 1;
	else
		return 1;
}

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

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 >> a[i][j];
		}
	check();
	for (int i = 1; i <= n * 3; i++) {
		for (int j = 1; j <= m * 3; j++)
			sum1[j] += b[i][j];
		for (int j = 1; j <= m * 3; j++)
			sum[i][j] = sum[i][j - 1] + sum1[j];
	}
	for (int i = n - 1; i <= 3 * n; i++)
		for (int j = m - 1; j <= 3 * m; j++)
			maxy = max(maxy, sum[i][j] - sum[i][j - m + 1] - sum[i - n + 1][j] + sum[i - n + 1][j - m + 1]);
	cout << maxy;
	return 0;
}