#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()

bool solve() {

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

	int n, m;
	char c;
	
	scanf("%d %d", &n, &m);

	vec< vec< int > > a(n + 1, vec< int >(m + 1));

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf(" %c", &c);
			a[i][j] = c - '0' - 1;
		}
	}

	int res = 0;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			int tmp = 0;
			//cout << '\n';
			for (int ci = i; ci < i + n - 1; ci++) {
				for (int cj = j; cj < j + m - 1; cj++) {
					int I, J;
					I = (ci - 1) % n + 1;
					J = (cj - 1) % m + 1;
					int ok[2] = { 0, 0 };

					//cout << a[I][J];

					ok[a[I][J]] = 1;
					ok[a[I][J % m + 1]] = 1;
					ok[a[I % n + 1][J]] = 1;
					ok[a[I % n + 1][J % m + 1]] = 1;

					if (ok[0] ^ ok[1]) tmp++;
				}
				//cout << '\n';
			}
			res = max(res, tmp);
			//cout << tmp << '\n';
		}
	}

	cout << res << '\n';

	return true;
}

int main() {
	
	//while (solve());
	solve();
	
	return 0;
}