#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

int n, m, a[2000][2000], h[2000], l[2000], sumi = 0;
char g[2000][2000];

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> g[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (g[i][j] == g[(i + 1) % n][j] && g[i][j] == g[(i + 1) % n][(j + 1) % m] && g[i][j] == g[i][(j + 1) % m]) {
				sumi++;
				a[i][j]++;
				h[i]++;
				l[j]++;
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			ans = max(ans, sumi - h[i] - l[j] + a[i][j]);
		}
	}
	cout << ans;
}