#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 + n, vec< int >(m + m)), sum = a, col = a, str = a;

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

	int res = 0;

	for (int i = 0; i + 1 < n + n; i++) {
		for (int j = 0; j + 1 < m + m; j++) {
			sum[i][j] = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
		}
	}
	
	for (int j = 0; j + 1 < m + m; j++) {
		int tmp = 0;
		for (int i = n; i + 1 < n + n; i++) tmp += (sum[i][j] == 0 || sum[i][j] == 4);
		col[n][j] = tmp;
		for (int i = n - 1; i >= 0; i--) {
			tmp += (sum[i][j] == 0 || sum[i][j] == 4);
			tmp -= (sum[i + n - 1][j] == 0 || sum[i + n - 1][j] == 4);
			col[i][j] = tmp;
		}
	}

	for (int i = 0; i + 1 < n + n; i++) {
		int tmp = 0;
		for (int j = 0; j + 1 < m; j++) tmp += col[i][j];
		str[i][0] = tmp;
		for (int j = 1; j <= m ; j++) {
			tmp += col[i][j + m - 2];
			tmp -= col[i][j - 1];
			str[i][j] = tmp;
		}
	}

	for (int i = 0; i <= n;i++) {
		for (int j = 0; j <= m; j++)
			res = max(res, str[i][j]);
	}

	printf("%d\n", res);

	return true;
}

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