#include <fstream>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	ifstream cin("video.in");
	ofstream cout("video.out");
	int n, m, i, j, **u, *str, *sto;
	cin >> n >> m;
	char** c;
	c = new char*[n + 1];
	u = new int*[n + 1];
	str = new int[n];
	sto = new int[m];
	for (i = 0; i <= n; ++i)
	{
		c[i] = new char[m + 2];
		u[i] = new int[m];
	}
	cin.getline(*c, m + 1);
	for (i = 0; i < n; ++i)
	{
		cin.getline(c[i], m + 2);
		c[i][m] = *(c[i]);
	}
	for (i = 0; i <= m; ++i)
		c[n][i] = c[0][i];
	for (i = 0; i < n; ++i)
		for (j = 0; j < m; ++j)
			if (((c[i][j] - '1') + (c[i + 1][j] - '1') + (c[i][j + 1] - '1') + (c[i + 1][j + 1] - '1')) % 4 == 0)
				u[i][j] = 1;
			else
				u[i][j] = 0;
	for (i = 0; i < n; ++i)
	{
		str[i] = 0;
		for (j = 0; j < m; ++j)
			str[i] += u[i][j];
	}
	for (j = 0; j < m; ++j)
	{
		sto[j] = 0;
		for (i = 0; i < n; ++i)
			sto[j] += u[i][j];
	}
	int s = 0, e = 1e8;
	for (i = 0; i < n; ++i)
	{
		s += str[i];
		for (j = 0; j < m; ++j)
			if (str[i] + sto[j] - u[i][j] < e)
				e = str[i] + sto[j] - u[i][j];
	}
	cout << s - e;
	return 0;
}