// 5video.cpp : Defines the entry point for the console application.
//

#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

ifstream fin("video.in");
ofstream fout("video.out");

int n, m;
vector <vector <bool> > mon;
char c;
int ans = 0, res = 0, cur = 0;

bool el(int x, int y)
{
	return mon[(x + n) % n][(y + m) % m];
}

int max(int a, int b)
{
	if (a > b)
		return a;
	return b;
}

int main()
{
	fin >> n >> m;

	mon.clear();
	mon.resize(n);

	for (int i = 0; i < n; i++)
	{
		mon[i].clear();
		mon[i].resize(m);
		for (int j = 0; j < m; j++)
		{
			fin >> c;
			mon[i][j] = (c == '1');
		}
	}

	/*for (int i = 1; i < n - 2; i++)
	{
		for (int j = 1; j < m - 2; j++)
		{
			if (mon[i][j] == mon[i][j + 1] && mon[i][j] == mon[i + 1][j + 1] && mon[i][j] == mon[i + 1][j])
				ans++;
		}
	}*/

	for (int p = -1; p < 2; p++)
	{
		for (int q = -1; q < 2; q++)
		{
			/*for (int i = 0; i < n - 1; i++)
			{
				if (el(i + p, 0 + q) == el(i + p, 1 + q) && el(i + p, 0 + q) == el(i + p + 1, 1 + q) && el(i + p, 0 + q) == el(i + p + 1, 0 + q))
					cur++;
				if (m > 2 && el(i + p, m + q - 2) == el(i + p, m + q - 1) && el(i + p, m + q - 2) == el(i + p + 1, m + q - 1) && el(i + p, m + q - 2) == el(i + p + 1, m + q - 1))
					cur++;
			}
			for (int j = 1; j < m - 2; j++)
			{
				if (el(0 + p, j + q) == el(1 + p, j + q) && el(0 + p, j + q) == el(0 + p, j + q + 1) && el(0 + p, j + q) == el(1 + p, j + q + 1))
					cur++;
				if (n > 2 && el(n - 2 + p, j + q) == el(n + p - 1, j + q) && el(n + p - 2, j + q) == el(n + p - 2, j + q + 1) && el(n + p - 2, j + q) == el(n + p - 1, j + q + 1))
					cur++;
			}*/
			for (int i = 0; i < n - 1; i++)
			{
				for (int j = 0; j < m - 1; j++)
				{
					if (el(i + p, j + q) == el(i + p, j + q + 1) && el(i + p, j + q) == el(i + p + 1, j + q) && el(i + p + 1, j + q + 1) == el(i + p, j + q))
						cur++;
				}
			}
			res = max(res, cur);
			cur = 0;
		}
	}

	
	fout << ans + res;
	
	return 0;
}

