#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];

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(mm[nx][ny]);
	}
	return lol[0] == lol[1] && lol[1] == lol[2] && lol[2] == lol[3];
}

int solve(int sx, int sy)
{
	int x = sx;
	int y = sy;
	int ans = 0;
	for (int i = 0; i < n - 1; i++)
	{
		x = (x + 1) % n;
		y = sy;
		for (int j = 0; j < m - 1; j++)
		{
			y = (y + 1) % m;
			if (good(x, y))
				ans++;
		}
	}
	return ans;
}

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