#include <iostream>

using namespace std;

const int SIZE = 2000;

char s[SIZE][SIZE];
int h[SIZE], v[SIZE];

int main()
{
#ifdef _CONSOLE
	freopen("input.txt", "r", stdin);
#else
	freopen("video.in", "r", stdin);
	freopen("vodeo.out", "w", stdout);
#endif

	int n, m;

	scanf("%d %d\n", &n, &m);

	for (int i = 0; i < n; i++)
		gets_s(s[i]);

	for (int i = 0; i < n; i++)
		s[i][m] = s[i][0];
	for (int i = 0; i < m + 1; i++)
		s[n][i] = s[0][i];

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (s[i][j] == s[i + 1][j] && s[i][j] == s[i][j + 1] && s[i][j] == s[i + 1][j + 1]) {
				h[i]++;
				v[j]++;
			}

	int sum = 0, min_v = 0, min_h = 0;

	for (int i = 0; i < n; i++) {
		sum += h[i];

		if (h[i] < h[min_h])
			min_h = i;
	}

	for (int i = 0; i < m; i++) {
		if (v[i] < v[min_v])
			min_v = i;
	}

	int i = min_h, j = min_v;
	cout << sum - v[j] - h[i] + (s[i][j] == s[i][j + 1] && s[i][j] == s[i + 1][j] && s[i][j] == s[i + 1][j + 1]) << endl;
}