#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define MAXN 1005

int mas[MAXN * 2][MAXN * 2];
int sumw[MAXN * 2][MAXN];
int sumans[MAXN][MAXN];

int main()
{
	int n, m;
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			char c;
			scanf("%c", &c);
			if (c == '\n' || c == '\r')
				j--;
			if (c == '1')
				mas[i][j] = 1;
			if (c == '2')
				mas[i][j] = 2;
		}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			mas[i + n][j] = mas[i][j];
			mas[i + n][j + m] = mas[i][j];
			mas[i][j + m] = mas[i][j];
		}
	for (int i = 0; i < 2 * n - 1; i++)
	{
		for (int j = 0; j < m - 1; j++)
		if (mas[i][j + 0] == mas[i][j + 1] && mas[i][j + 0] == mas[i + 1][j + 0] && mas[i][j + 0] == mas[i + 1][j + 1])
			sumw[i][0]++;
	}
	for (int i = 0; i < 2 * n - 1; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			sumw[i][j] = sumw[i][j - 1];
			if (mas[i][j - 1] == mas[i][j] && mas[i][j - 1] == mas[i + 1][j - 1] && mas[i][j - 1] == mas[i + 1][j])
				sumw[i][j]--;
			if (mas[i][j + m - 2] == mas[i][j + m - 1] && mas[i][j + m - 2] == mas[i + 1][j + m - 2] && mas[i][j + m - 2] == mas[i + 1][j + m - 1])
				sumw[i][j]++;
		}
	}
	for (int j = 0; j <= m; j++)
		for (int i = 0; i < n - 1; i++)
			sumans[0][j] += sumw[i][j];
	for (int j = 0; j < m; j++)
	{
		for (int i = 1; i < n; i++)
			sumans[i][j] += sumans[i - 1][j] + sumw[i + n - 2][j] - sumw[i - 1][j];
	}
	int ans = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			ans = max(ans, sumans[i][j]);
	/*int ans = 0;
	for (int l = 0; l < n; l++)
		for (int r = 0; r < m; r++)
		{
			int curans = 0;
			for (int i = 0; i < n - 1; i++)
				for (int j = 0; j < m - 1; j++)
					if (mas[i + l][j + 0 + r] == mas[i + l][j + 1 + r] && mas[i + l][j + 0 + r] == mas[i + 1 + l][j + 0 + r] && mas[i + l][j + 0 + r] == mas[i + 1 + l][j + 1 + r])
						curans++;
			ans = max(ans, curans);
		}*/
	cout << ans;
	return 0;
}