#include<fstream>
#include<algorithm>
#include<string>

using namespace std;

const int maxn = 2005;

int n, m;
string st;
int arr[maxn][maxn];
long long din[maxn][maxn], gor[maxn][maxn], ver[maxn][maxn];
long long ans = 0;

int main() {
	ifstream cin("video.in");
	ofstream cout("video.out");
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	getline(cin, st);
	for (int i = 1; i <= n; ++i) {
		getline(cin, st);
		for (int j = 1; j <= m; ++j) {
			arr[i][j] = arr[i + n][j] = arr[i][j + m] = arr[i + n][j + m] = st[j - 1] - '0';
		}
	}
	for (int i = 1; i <= n * 2; ++i) {
		for (int j = 1; j <= m * 2; ++j) {
			int sv = max(0, i - n);
			int sl = max(0, j - m);
			din[i][j] = din[i - 1][j - 1];
			din[i][j] -= gor[sv + 1][j - 1] - gor[sv + 1][sl];
			din[i][j] -= ver[i - 1][sl + 1] - ver[sv][sl + 1];
			din[i][j] += gor[i][j - 1] - gor[i][sl + 1];
			din[i][j] += ver[i - 1][j] - ver[sv + 1][j];
			gor[i][j] = gor[i][j - 1];
			ver[i][j] = ver[i - 1][j];
			if (arr[i][j] == arr[i - 1][j] && arr[i][j] == arr[i][j - 1] && arr[i][j] == arr[i - 1][j - 1]) gor[i][j] += 1, ver[i][j] += 1, din[i][j] += 1;
			ans = max(ans, din[i][j]);
		}
	}
	cout << ans;
	return 0;
}