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


typedef long long ll;


using namespace std;


#define TASK "video"


char q[1005][1005];


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 = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> q[i][j];
		}
	}
	for (int i = 1; i <= n; i++) {
		q[i][0] = q[i][m];
		q[i][m + 1] = q[i][1];
	}
	for (int j = 1; j <= m; j++) {
		q[0][j] = q[n][j];
		q[n + 1][j] = q[1][j];
	}
	q[0][0] = q[n][m];
	q[0][m + 1] = q[n][1];
	q[n + 1][0] = q[1][m];
	q[n + 1][m + 1] = q[1][1];
	int ans = 0;
	for (int sti = 0; sti < 3; sti++) {
		for (int stj = 0; stj < 3; 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;
}