#include <bits/stdc++.h>

using namespace std;

inline int getAns(vector<string> & a) {
	int ans = 0;

	for (int i = 1; i < (int)a.size(); i++) {
		for (int j = 1; j < (int)a[i].size(); j++) {
			if (a[i][j] == a[i - 1][j] && a[i - 1][j] == a[i][j - 1] && a[i][j - 1] == a[i - 1][j - 1]) {
				ans++;
			}
		}
	}

	return ans;
}

vector<int> ch;

inline int getMaxJ(vector<string> & a, int ans) {
	int nowAns = ans;

	for (int i = 0; i < (int)a[0].size(); i++) {
		nowAns += ch[i];

		ans = max(ans, nowAns);
	}

	return ans;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cout.tie(nullptr);

	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);

	int n, m;
	cin >> n >> m;

	vector<string> a(n);
	ch.resize(m, 0);

	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	for (int i = 0; i < (int)a[0].size(); i++) {
		int pr = (i + m - 1) % m, nx = (i + 1) % m;

		for (int j = 1; j < n; j++) {
			if (a[j][i] == a[j][pr] && a[j][pr] == a[j - 1][pr] && a[j - 1][pr] == a[j - 1][i]) {
				ch[i]++;
			}

			if (a[j][i] == a[j][nx] && a[j][nx] == a[j - 1][nx] && a[j - 1][nx] == a[j - 1][i]) {
				ch[i]--;
			}
		}
	}

	int ans = getAns(a), nowAns = ans;

	for (int i = 0; i < n; i++) {
		int pr = n - 1, now = 0, nx = 1;

		for (int i = 1; i < m; i++) {
			if (a[now][i] == a[now][i - 1] && a[now][i - 1] == a[pr][i] && a[pr][i] == a[pr][i - 1]) {
				nowAns++;
			}
			if (a[now][i] == a[now][i - 1] && a[now][i - 1] == a[nx][i] && a[nx][i] == a[nx][i - 1]) {
				nowAns--;
			}
		}

		for (int i = 1; i < n; i++) {
			a[i].swap(a[i - 1]);
		}

		// ch.assign(m, 0);

		for (int i = 0; i < (int)a[0].size(); i++) {
			int pr = (i + m - 1) % m, nx = (i + 1) % m;

			for (int j = 1; ; j = n - 1) {
				if (a[j][i] == a[j][pr] && a[j][pr] == a[j - 1][pr] && a[j - 1][pr] == a[j - 1][i]) {
					ch[i]--;
				}

				if (a[j][i] == a[j][nx] && a[j][nx] == a[j - 1][nx] && a[j - 1][nx] == a[j - 1][i]) {
					ch[i]++;
				}

				if (j == n - 1) break;
			}
		}

		ans = max(getMaxJ(a, nowAns), ans);
	}

	cout << ans << "\n";

	return 0;
}
