#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int n, m;

char a[1013][1013];
bool f[1013][1013];

bool g(int x, int y) {
	if (a[x][y] == a[x][y + 1] && a[x][y] == a[x + 1][y] && a[x][y] == a[x + 1][y + 1]) {
		return 1;
	}
	return 0;
}
int outx[1013], outy[1013], sum;
int main () {
	//freopen("1.txt", "r", stdin);
	freopen("video.in", "r", stdout);
	freopen("video.out", "w", stdout);
	
	cin >> n >> m;

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

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

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

	a[n][m] = a[0][0];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			f[i][j] = g(i, j);
			sum += f[i][j];
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			outx[j] += f[i][j];
			outy[i] += f[i][j];
		}
	}

	int out = 10000013;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			out = min(out, outx[j] + outy[i] - f[i][j]);
		}
	}

	cout << sum- out;
	return 0;
}