#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <climits>

using namespace std;

int choose(vector<vector<bool> > &v, pair<int, int> p) {
	int ans = 0;
	for (int i = p.first; i < v.size() + p.first - 1; i++)
		for (int j = p.second; j < v[0].size() + p.second - 1; j++)
			ans += ((v[i % v.size()][j % v[0].size()] && v[(i + 1) % v.size()][j % v[0].size()] && v[i % v.size()][(j + 1) % v[0].size()] && v[(i + 1) % v.size()][(j + 1) % v[0].size()]) || (!v[i % v.size()][j % v[0].size()] && !v[(i + 1) % v.size()][j % v[0].size()] && !v[i % v.size()][(j + 1) % v[0].size()] && !v[(i + 1) % v.size()][(j + 1) % v[0].size()]));
	return ans;
}

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);

	clock_t start = clock();

	int n, m;
	cin >> n >> m;
	vector<vector<bool> > v(n);
	for (int i = 0; i < n; i++) {
		v[i].resize(m);
		char temp;
		scanf("%c", &temp);
		for (int j = 0; j < m; j++) {
			scanf("%c", &temp);
			v[i][j] = (temp == '1');
		}
	}
	int mx = 0;

	vector<int> di, dj;
	di = { 0, 1, 2, n, n - 1, n - 2 };
	dj = { 0, 1, 2, m, m - 1, m - 2 };

	random_shuffle(di.begin(), di.end());
	random_shuffle(dj.begin(), dj.end());

	for (int i = 0; i < di.size(); i++) {
		for (int j = 0; j < dj.size(); j++)
			mx = max(mx, choose(v, make_pair(di[i], dj[j])));
		//if (clock() - start > 990) break;
	}

	cout << mx;
}