#include <iostream>
#include <algorithm>
using namespace std;

const int maxN = 1000;

char a[maxN][maxN];

void shiftleft(int n, int m)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = m - 1; j > 0; j--)
		{
			swap(a[i][j], a[i][j - 1]);
		}
	}
}

void shiftright(int n, int m)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m - 1; j++)
		{
			swap(a[i][j], a[i][j + 1]);
		}
	}
}

void shiftup(int n, int m)
{
	for (int i = n - 1; i > 0; i--)
	{
		for (int j = 0; j < m; j++)
		{
			swap(a[i][j], a[i - 1][j]);
		}
	}
}

void shiftdown(int n, int m)
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m; j++)
		{
			swap(a[i][j], a[i + 1][j]);
		}
	}
}

int count(int n, int m)
{
	int res = 0;
	for (int i = 1; i < n; i++)
	{
		for (int j = 1; j < m; 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++;
			}
		}
	}
	return res;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	int n, m;
	scanf("%d%d", &n, &m);

	for (int i = 0; i < n; i++)
	{
		scanf("%s", a[i]);
	}

	int res = 0;
	res = max(res, count(n, m));

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			shiftleft(n, m);
			res = max(res, count(n, m));
		}
		shiftdown(n, m);
		res = max(res, count(n, m));
	}
	printf("%d", res);
	
    return 0;
}

