#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>

using namespace std;

#define SIZE 1228

int n, m;
char a[SIZE][SIZE];
int ln[SIZE], col[SIZE];
int good[SIZE][SIZE];

int getx(int x)
{
	if (x >= 0)
		return x;
	return n + x;
}

int gety(int y)
{
	if (y >= 0)
		return y;
	return m + y;
}

int main()
{
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int q = 0; q < m; q++)
			cin >> a[i][q];
	}
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		for (int q = 0; q < m; q++)
		{
			if (a[i][q] == a[getx(i - 1)][q] && a[getx(i - 1)][q] == a[i][gety(q - 1)] && a[i][gety(q - 1)] == a[getx(i - 1)][gety(q - 1)])
			{
				ln[i]++;
				col[q]++;
				good[i][q] = 1;
			}
		}
	}
	int scol = 0;
	for (int i = 0; i < m; i++)
		scol += col[i];
	for (int x = 0; x < n; x++)
	{
		for (int y = 0; y < m; y++)
			ans = max(ans, scol - col[y] - ln[x] + good[x][y]);
	}
	cout << ans << endl;
	return 0;
}