#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <algorithm>


typedef long long ll;


using namespace std;


#define TASK "video"


char q[3005][3005];


int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	freopen(TASK".in", "r", stdin);
	freopen(TASK".out", "w", stdout);
#endif // _DEBUG

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> q[i][j];
		}
	}
	for (int i = 0; i < 3 * n; i++) {
		for (int j = 0; j < 3 * m; j++) {
			q[i][j] = q[i % n][j % m];
		}
	}
	int ans = 0;
	for (int sti = 0; sti < 2*n + 1; sti++) {
		for (int stj = 0; stj < 2*m + 1; stj++) {
			int tans = 0;
			for (int i = sti; i < n + sti - 1; i++) {
				for (int j = stj; j < m + stj - 1; j++) {
					if (q[i][j] == q[i][j + 1] &&
						q[i][j] == q[i + 1][j] &&
						q[i][j] == q[i + 1][j + 1]) {
						tans++;
					}
				}
			}
			ans = max(ans, tans);
		}
	}
	cout << ans;
}