#include <fstream>
#include <string>
#include <map>
#include <set>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;

#ifdef _DEBUG
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
ifstream cin("video.in");
ofstream cout("video.out");
#endif

int x, y;

char a[1000][1000];

void top() {
	for (int i = 1; i < x; i++) {
		for (int j = 0; j < y; j++) {
			swap(a[i][j], a[i - 1][j]);
		}
	}
}
void bot() {
	for (int i = x - 1; i >= 1; i--) {
		for (int j = 0; j < y; j++) {
			swap(a[i][j], a[i - 1][j]);
		}
	}
}
void lft() {
	for (int i = 0; i < x; i++) {
		for (int j = 1; j < y; j++) {
			swap(a[i][j], a[i][j - 1]);
		}
	}
}
void rgh() {
	for (int i = 0; i < x; i++) {
		for (int j = y - 1; j >= 1; j--) {
			swap(a[i][j], a[i][j - 1]);
		}
	}
}
int ans = 0;
void calc() {
	int res = 0;
	for (int i = 0; i < x - 1; i++) {
		for (int j = 0; j < y - 1; j++) {
			if (a[i][j] == a[i + 1][j + 1] && a[i][j] == a[i + 1][j] && a[i][j] == a[i][j + 1])
				res++;
		}
	}
	ans = max(ans, res);
}

int main() {
	cin >> x >> y;
	for (int i = 0; i < x; i++) {
		for (int j = 0; j < y; j++) {
			cin >> a[i][j];
		}
	}
	calc();
	top();
	calc();
	lft();
	calc();
	rgh();
	rgh();
	calc();
	lft();
	calc();
	bot();

	//END 1

	lft();
	calc();
	top();
	calc();
	bot();
	bot();
	calc();
	top();
	rgh();

	//END 2

	bot();
	calc();
	lft();
	calc();
	rgh();
	rgh();
	calc();
	lft();
	top();

	//END 3

	rgh();
	calc();
	top();
	calc();
	bot();
	bot();
	calc();


	cout << ans;
}