#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>
#include<unordered_map>
#include<unordered_set>

using namespace std;

typedef long long ll;
#define forn(i, n) for (int i = 0; i < n; ++i)
#define forq(i, q, n) for (int i = q; 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>
#define vii vector<pii>
#define vpll vector<pll>
#define x first
#define y second

const int MOD = 1e9 + 7;
const int MAXN = 1e3 + 7;
const ll INf = 1e15 + 7;

char a[MAXN][MAXN];
ll ans[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)
			//scanf("%c", &a[i][j]);
			cin >> a[i][j];
	forq(k, -5, 6)
	{
		s.clear();
		for (int j = 0; j < m; ++j)
			{
				int lans = 0;
				int j1 = j;
				int j2 = (j + 1) % m;
				for (int i = k; i < n + k - 1; ++i)
				{
					int i1 = (i + n) % n;
					int i2 = (i + 1 + n) % 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.begin());
		for (auto it = s.begin(); it != s.end(); ++it)
			ans[k] += *it;
	}
	ll mx = 0;
	/*forn(i, n)
		cout << ans[i] << " ";
	cout << "\n";*/
	forn(i, n)
		mx = max(mx, ans[i]);
	cout << mx;
	return 0;
}