#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

const int MAXN = 1100;
bool mas[MAXN][MAXN] = {};
bool start[MAXN][MAXN] = {};
int s1[MAXN] = {}, s2[MAXN] = {};
int k1[MAXN] = {}, k2[MAXN] = {};

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	int n, m;
	char c;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> c;
			if (c == '2') {
				mas[i][j] = 1;
			}
		}
	}

	int ans = 0;
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < m - 1; j++) {
			if (mas[i][j] == mas[i + 1][j] && mas[i][j] == mas[i][j + 1] && mas[i][j] == mas[i + 1][j + 1]) {
				start[i][j] = true;
				s1[i]++;
				s2[j]++;
				ans++;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		for (int i1 = 0; i1 < n - 1; i1++) {
			int i2 = (i + i1) % n;
			if (mas[i2][m - 1] == mas[(i2 + 1) % n][m - 1] && mas[i2][m - 1] == mas[i2][0] && mas[i2][m - 1] == mas[(i2 + 1) % n][0])
				k1[i]++;
		}
	}

	for (int j = 0; j < m; j++) {
		for (int j1 = 0; j1 < m - 1; j1++) {
			int j2 = (j + j1) % m;
			if (mas[n - 1][j2] == mas[n - 1][(j2 + 1) % m] && mas[n - 1][j2] == mas[0][j2] && mas[n - 1][j2] == mas[0][(j2 + 1) % m])
				k2[j]++;
		}
	}

	int ans1 = ans;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (i == 0 && j == 0)
				continue;
			if (i == 0) {
				ans1 = max(ans1, ans - s2[j - 1] + k2[0]);
			}
			else {
				if (j == 0) {
					ans1 = max(ans1, ans - s1[i - 1] + k1[0]);
				}
				else {
					int ans2 = ans - s1[i - 1] - s2[j - 1];
					if (start[i - 1][j - 1])
						ans2++;
					ans2 += k1[i] + k2[j];
					ans1 = max(ans1, ans2);
				}
			}
		}
	}
	cout << ans1;
	return 0;
}
