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

const int MAXN = (int)3e2 + 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, cnt;
char kek[4];

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();
	}
}

inline bool good(int x, int y)
{
	int nx, ny;
	for (int k = 0; k < 4; k++)
	{
		nx = (x + dx[k]) % n;
		ny = (y + dy[k]) % m;
		kek[k] = mm[nx][ny];
	}
	return kek[0] == kek[1] && kek[1] == kek[2] && kek[2] == kek[3];
}

void INIT()
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m - 1; j++)
		{
			if (good(i, j))
				cnt++;
		}
	}
}

void right(int x, int y)
{
	for (int i = 0; i < n - x - 1; i++)
	{
		if (good(i, m - y - 2))
			cnt--;
		if (good(i, m - y - 1))
			cnt++;
	}
	for (int i = n - x; i < n; i++)
	{
		if (good(i, m - y - 2))
			cnt--;
		if (good(i, m - y - 1))
			cnt++;
	}
}

void down(int x, int y)
{
	for (int i = 0; i < m - y - 1; i++)
	{
		if (good(n - x - 2, i))
			cnt--;
		if (good(n - x - 1, i))
			cnt++;
	}
	for (int i = m - y; i < m; i++)
	{
		if (good(n - x - 2, i))
			cnt--;
		if (good(n - x - 1, i))
			cnt++;
	}
}

int main()
{
	// xxxvideo
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	input();
	INIT();
	int ans = 0, lcnt;
	for (int i = 0; i < n; i++)
	{
		lcnt = cnt;
		for (int j = 0; j < m - 1; j++)
		{
			right(i, j);
			ans = max(ans, cnt);
		}
		cnt = lcnt;
		down(i, 0);
		ans = max(ans, cnt);
	}
	printf("%d", ans);
	return 0;
}