#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int N = 1e3;

char c[N][N];
int val[N][N];
int cntx[N];
int cnty[N];

const int cnt = 4;
const int dx[cnt] = { 0, 0, 1, 1 };
const int dy[cnt] = { 0, 1, 0, 1 };

int main()
{
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			scanf(" %c", &c[i][j]);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
		{
			val[i][j] = 1;
			char q = c[i][j];
			int x = i, y = j;
			for (int z = 0; z < cnt; ++z)
			{
				x = (i + dx[z]);
				y = (j + dy[z]);
				if (x == n)
					x = 0;
				if (y == m)
					y = 0;
				if (c[x][y] != q)
				{
					val[i][j] = 0;
					break;
				}
			}
		}

	int sum = 0;
	//for (int i = 0; i < n; ++i, cout << endl)
	//	for (int j = 0; j < m; ++j)
	//		cout << val[i][j];
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			if (val[i][j] == 1)
				++cntx[i],
				++cnty[j],
				++sum;
	int result = 0;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			result = max(result, sum - cntx[i] - cnty[j] + val[i][j]);

	cout << result;
	return 0;
}
