#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#include<iostream>
#include<fstream>
#include<cmath>
#include<ctime>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>

using namespace std;

typedef long long ll;
#define forn(i, n) for (int i = 0; i < n; ++i)
#define mk make_pair
#define psh push_back
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>

const int INF = 1e9 + 7;
const int MAXN = 1e3 + 7;

char a[MAXN][MAXN];

int main()
{
	/*freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);*/
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	int n, m;
	cin >> n >> m;
	multiset<int> s;
	forn(i, n)
		forn(j, m)
			cin >> a[i][j];
	ll ans = 0;
	for (int j = 0; j < m; ++j)
	{
		int lans = 0;
		int j1 = j;
		int j2 = (j + 1) % m;
		for (int i = 0; i < n; ++i)
		{
			int i1 = i;
			int i2 = (i + 1) % n;
			if (a[i1][j1] == a[i1][j2] && a[i1][j1] == a[i2][j1] && a[i1][j1] == a[i2][j2])
				lans++;
		}
		lans = min(lans, n - 1);
		s.insert(lans);
	}
	s.erase(s.find(*s.begin()));
	for (auto it = s.begin(); it != s.end(); ++it)
		ans += *it;
	cout << ans;
	return 0;
}