#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <memory.h>
#include <string>
#define mp make_pair
using namespace std;

const int MAXN = 1002;

string s[MAXN];
int a[MAXN];
int b[MAXN];
int c[MAXN][MAXN];
int main()
{
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	int n, m;
	cin >> n >> m;
	int ans = n * m;
	int sm = 0;
	for (int i = 0; i < n; i++) cin >> s[i];
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			int i2 = (i + 1) % n, j2 = (j + 1) % n;
			if ((s[i][j] == s[i2][j2]) && (s[i][j] == s[i2][j]) && (s[i][j] == s[i][j2])) c[i][j] = 1;
			else c[i][j] = 0;
			sm += c[i][j], a[i] += c[i][j], b[j] += c[i][j];
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			ans = min(ans, a[i] + b[j] - c[i][j]);
		}
	}
	cout << sm - ans << endl;
	return 0;
}