//#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream cin("video.in");
ofstream cout("video.out");

const int MAXN = int(1e3);
int n, m;
char a[MAXN][MAXN], b[MAXN][MAXN];

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			cin >> a[i][j];
	int ans = 0;
	for (int i_add = 0; i_add < n; ++i_add)
		for (int j_add = 0; j_add < m; ++j_add) {
			int s = 0;
			for (int i = 0; i < n; ++i)
				for (int j = 0; j < m; ++j)
					b[i][j] = a[(i + i_add) % n][(j + j_add) % m];
			for (int i = 0; i + 1 < n; ++i)
				for (int j = 0; j + 1 < m; ++j) {
					char x = b[i][j];
					char r = b[i][j + 1];
					char d = b[i + 1][j];
					char rd = b[i + 1][j + 1];
					s += (x == r) && (x == d) && (x == rd);
				}
			ans = max(ans, s);
		}
	cout << ans << endl;
	cin >> n;
	return 0;
}