#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

const int sz = 2e3;

int ar[sz][sz];
int v[sz], h[sz];

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			char cur;
			cin >> cur;
			ar[i][j] = cur - '0';
		}
	}
	int s = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (ar[i][j] == ar[(i + 1) % n][j] && ar[i][j] == ar[i][(j + 1) % m] && ar[i][j] == ar[(i + 1) % n][(j + 1) % m]) {
				s++;
				h[i]++;
				v[j]++;
			}
		}
	}
	int res = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			res = max(res, s - h[i] - v[j] +(ar[i][j] == ar[(i + 1) % n][j] && ar[i][j] == ar[i][(j + 1) % m] && ar[i][j] == ar[(i + 1) % n][(j + 1) % m]));
		}
	}
	printf("%d", res);
	return 0;
}

