#include <algorithm>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
	ifstream cin("video.in");
	ofstream cout("video.out");
	int n, m; cin >> n >> m;
	vector<vector<bool>> matr(n, vector<bool>(m));
	for (int i = 0; i < n; ++i)
	{
		string s; cin >> s;
		for (int j = 0; j < m; ++j)
			matr[i][j] = s[j] == '2';
	}
	vector<int> rows(n);
	vector<int> columns(m);
	vector<vector<bool>> cells(n, vector<bool>(m));
	int cnt = 0;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
		{
			int i1 = (i + 1) % n;
			int j1 = (j + 1) % m;
			if (matr[i][j] == matr[i1][j] && matr[i][j] == matr[i][j1] && matr[i][j] == matr[i1][j1])
			{
				++rows[i];
				++columns[j];
				++cnt;
				cells[i][j] = true;
			}
		}
	int mx = 0;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			mx = max(mx, cnt - rows[i] - columns[j] + (int)cells[i][j]);
	cout << mx << endl;
}