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

using namespace std;

#define all(x) x.begin(), x.end()
#define ll long long

string mas[1001];
ll k = 0, k1 = 0;

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> mas[i];
		//sort(all(mas[i]));
	}
	ll now = 0, ans = -1;

	for (int x = 0; x < n; x++)
		for (int y = x + 1; y < m; y++) {
			now = 0;
			for (int i = 0; i < n - 1; i++)
				for (int j = 0; j < m - 1; j++) {
					if ((mas[(i + x) % n][(j + y) % m] == mas[(i + 1 + x) % n][(j + y) % m])
						&& (mas[(i + 1 + x) % n][(j + y) % m] == mas[(i + x) % n][(j + y + 1) % m])
						&& (mas[(i + x) % n][(j + y + 1) % m] == mas[(i + 1 + x) % n][(j + y + 1) % m]))
						now++;
				}
			ans = max(now, ans);
		}


	cout << ans;
	return 0;
}