#define _ijps 0
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <set>
#include <vector>

using namespace std;

#define name "video"
typedef long long ll;
#define forn(i, n) for(int i = 0; i < n; i++)
#define fornn(i, a, n) for(int i = a; i < n; i++)
#define mk make_pair

const int dd = 2e3 + 7;
const int infi = 1e9 + 7;
const ll inf = 1e18 + 7;

int dp[dd][dd];
vector<vector<bool> > T, W;
int n, m;

int main() {
	if (_ijps) {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
	else {
		freopen(name".in", "r", stdin);
		freopen(name".out", "w", stdout);
	}
	cin >> n >> m;
	T.resize(n, vector<bool>(m));
	W.resize(2 * n, vector<bool>(2 * m));
	forn(i, n) {
		forn(j, m) {
			char c;
			scanf(" %c", &c);
			if (c == '1') {
				T[i][j] = 1;
			}
			else {
				T[i][j] = 0;
			}
		}
	}

	forn(i, 2 * n) {
		forn(j, 2 * m) {
			W[i][j] = T[i % n][j % m];
		}
	}
	forn(i, 2 * n) {
		forn(j, 2 * m) {
			int t = 0;
			if (i - 1 >= 0 && j - 1 >= 0) {
				if (W[i][j] == W[i - 1][j] && W[i][j] == W[i][j - 1] && W[i][j] == W[i - 1][j - 1]) {
					t = 1;
				}
			}
			dp[i + 1][j + 1] = t + dp[i][j + 1] + dp[i + 1][j] - dp[i][j];
		}
	}
	int res = 0;
	fornn(i, n, 2 * n + 1) {
		fornn(j, m, 2 * m + 1) {
			int a = dp[i][j] + dp[i - n + 1][j - m + 1] - dp[i][j - m + 1] - dp[i - n + 1][j];
			res = max(res, a);
		}
	}
	cout << res;
}