#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>

using namespace std;

const int mn = 1010;
int n, m, k;
int a[mn][mn], cnt[mn], cnt2[mn];
char c;

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> c;
			a[i][j] = c - '1';
		}
	}
	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];
	k = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (a[i][j] == a[i + 1][j] && a[i][j] == a[i][j + 1] && a[i][j] == a[i + 1][j + 1]) {
				k++;
				cnt[i]++;
				cnt2[j]++;
			}
		}
	}
	int ma1 = 0, ma2 = 0;
	for (int i = 0; i < n; i++) {
		if (cnt[ma1] > cnt[i]) {
			ma1 = i;
		}
	}
	k -= cnt[ma1];
	ma2 = 0;
	for (int i = 0; i < m; i++) {
		if (cnt2[ma2] > cnt2[i]) {
			ma2 = i;
		}
	}
	if (a[ma1][ma2] == a[ma1 + 1][ma2] && a[ma1][ma2] == a[ma1][ma2 + 1] && a[ma1][ma2] == a[ma1 + 1][ma2 + 1]) {
		k++;
	}
	cout << k << "\n";
}