#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXN = (int)1e3 + 22;
const int dx[4] = { 0, 0, 1, 1 };
const int dy[4] = { 0, 1, 0, 1 };

char mm[MAXN][MAXN];
char go[MAXN][MAXN];

int n, m;

void input()
{
	scanf("%d %d\n", &n, &m);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			scanf("%c", &mm[i][j]);
		getchar();
	}
}

bool good(int x, int y)
{
	vector < char > lol;
	int nx, ny;
	for (int k = 0; k < 4; k++)
	{
		nx = (x + dx[k]) % n;
		ny = (y + dy[k]) % m;
		lol.push_back(go[nx][ny]);
	}
	return lol[0] == lol[1] && lol[1] == lol[2] && lol[2] == lol[3];
}

void INIT()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			go[i][j] = mm[i][j];
		}
	}
}

void up()
{
	vector < char > lol;
	for (int i = 0; i < m; i++)
		lol.push_back(go[0][i]);
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m; j++)
			go[i][j] = go[i + 1][j];
	}
	for (int i = 0; i < m; i++)
		go[n - 1][i] = lol[i];
}

void left()
{
	vector < char > lol;
	for (int i = 0; i < n; i++)
		lol.push_back(go[i][0]);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m - 1; j++)
			go[i][j] = go[i][j + 1];
	}
	for (int i = 0; i < n; i++)
		go[i][m - 1] = lol[i];
}

int solve(int sx, int sy)
{
	INIT();
	for (int i = 0; i < sx; i++)
		up();
	for (int i = 0; i < sy; i++)
		left();
	int ans = 0;
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m - 1; j++)
		{
			ans += good(i, j);
		}
	}
	return ans;
}

int main()
{
	// xxxvideo
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	input();
	int ans = 0;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
			ans = max(ans, solve(i, j));
	}
	printf("%d", ans);
	return 0;
}