#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("video.in");
ofstream out("video.out");
int main()
{
	int n, m;
	in >> n >> m;
	vector<vector<char>> a(n, vector<char>(m));
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			in >> a[i][j];
	vector<vector<int>> b(n, vector<int>(m, false));
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			if (a[i][j] == a[(i + 1) % n][j] && a[i][j] == a[i][(j + 1) % m] && a[i][j] == a[(i + 1) % n][(j + 1) % m])
				b[i][j] = true;
	vector<int> s1(n, 0), s2(m, 0);
	int s = 0;
	for (int i = 0; i < n; ++i)
		for  (int j = 0; j < m; ++j)
			s1[i] += b[i][j];
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			s2[j] += b[i][j];
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			s += b[i][j];
	int maxf = 0;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			maxf = max(maxf, s - s1[i] - s2[j] + b[i][j]);
	out << maxf;
}