//#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <string>
using namespace std;
int n, k, a, b, c, j, z;
char ch;
int m[5003][5003],m1[5003][5003];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	scanf("%d%d\n", &n, &k);
	for (int i = 1; i <= n; i++)
	{
		for (int z = 1; z <= k; z++)
		{
			scanf("%c", &ch);
			if (ch == '1')
				m[i][z] = 1;
			else
				m[i][z] = 2;
		}
		scanf("\n");
	}
	int res = 0;
	for (int i = k + 1; i <= 2 * k; i++)
		for (int z = 1; z <= n;z++)
			m[z][i] = m[z][i - k];
	for (int i = n + 1; i <= 2 * n; i++)
		for (int z = 1; z <= 2 * k; z++)
			m[i][z] = m[i - n][z];
	for (int i = 1; i <= 2 * n; i++)
		for (int z = 1; z <= 2 * k; z++)
			if (m[i][z] == m[i - 1][z] && m[i][z] == m[i][z - 1] && m[i][z] == m[i - 1][z - 1])
				m1[i][z] = 1;
	for (int i = 1; i <= 2 * n; i++)
		for (int z = 1; z <= 2 * k; z++)
			m1[i][z] = m1[i - 1][z] + m1[i][z - 1] - m1[i - 1][z - 1]+m1[i][z];
	for (int i = 1; i <= 2 * n; i++)
		for (int z = 1; z <= 2 * k; z++)
			if (i-n+1>0 && z-k+1>0)
				res = max(res, m1[i][z] - m1[i - n + 1][z] - m1[i][z - k + 1] + m1[i - n + 1][z - k + 1]);
	printf("%d", res);
    return 0;
}