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

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

char mm[MAXN][MAXN];

int px[MAXN][MAXN];
int py[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(const int &x, const 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()
{
	int val;
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m - 1; j++)
		{
			val = good(i, j);
			px[i][j] = val;
			py[i][j] = val;
			cnt += val;
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 1; j < m; j++)
		{
			px[i][j] += px[i][j - 1];
		}
	}
	for (int i = 1; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			py[i][j] += py[i - 1][j];
		}
	}
}

int getSumPX(int id, int l, int r)
{
	if (l == 0)
		return px[id][r];
	else
		return px[id][r] - px[id][l - 1];
}

int getSumPY(int id, int l, int r)
{
	if (l == 0)
		return py[r][id];
	else
		return py[r][id] - py[l - 1][id];
}

inline void right(const int &x, const 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++;
	}*/
	if (0 <= n - x - 2)
	{
		cnt -= getSumPY(m - y - 2, 0, n - x - 2);
		cnt += getSumPY(m - y - 1, 0, n - x - 2);
	}
	if (n - x <= n - 1)
	{
		cnt -= getSumPY(m - y - 2, n - x, n - 1);
		cnt += getSumPY(m - y - 1, n - x, n - 1);
	}

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

inline void down(const int &x, const 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++;
	}*/
	if (0 <= m - y - 2)
	{
		cnt -= getSumPX(n - x - 2, 0, m - y - 2);
		cnt += getSumPX(n - x - 1, 0, m - y - 2);
	}
	if (m - y <= m - 1)
	{
		cnt -= getSumPY(n - x - 2, m - y, m - 1);
		cnt += getSumPY(n - x - 1, m - y, m - 1);
	}
	/*for (int i = m - y; i < m; i++)
	{
		if (good(n - x - 2, i))
			cnt--;
		if (good(n - x - 1, i))
			cnt++;
	}*/
}


int main()
{
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	input();
	INIT();
	int ans = cnt, lcnt;
	for (int i = 0; i < n - 1; 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;
}