#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <climits>

using namespace std;

int choose(vector<vector<bool> > &v) {
	vector<vector<bool> > wtf(v.size());
	for (int i = 0; i < v.size(); i++) {
		wtf[i].resize(v[0].size());
		for (int j = 0; j < v[0].size(); j++)
			wtf[i][j] = ((v[i % v.size()][j % v[0].size()] && v[(i + 1) % v.size()][j % v[0].size()] && v[i % v.size()][(j + 1) % v[0].size()] && v[(i + 1) % v.size()][(j + 1) % v[0].size()]) || (!v[i % v.size()][j % v[0].size()] && !v[(i + 1) % v.size()][j % v[0].size()] && !v[i % v.size()][(j + 1) % v[0].size()] && !v[(i + 1) % v.size()][(j + 1) % v[0].size()]));
	}
	int minfi = INT_MAX, minfj = INT_MAX, posi, posj;
	for (int i = 0; i < v.size(); i++) {
		int tmp = 0;
		for (int j = 0; j < v[0].size(); j++)
			tmp += wtf[i][j];
		if (minfi > tmp) {
			minfi = tmp;
			posi = i;
		}
	}
	for (int j = 0; j < v[0].size(); j++) {
		int tmp = 0;
		for (int i = 0; i < v.size(); i++)
			tmp += wtf[i][j];
		if (minfj > tmp) {
			minfj = tmp;
			posj = j;
		}
	}

	for (int i = 0; i < v.size(); i++)
		wtf[i][posj] = false;
	for (int j = 0; j < v[0].size(); j++)
		wtf[posi][j] = false;

	int ans = 0;
	for (int i = 0; i < v.size(); i++)
		for (int j = 0; j < v[0].size(); j++)
			ans += wtf[i][j];
	return ans;
}

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);

	clock_t start = clock();

	int n, m;
	cin >> n >> m;
	vector<vector<bool> > v(n);
	for (int i = 0; i < n; i++) {
		v[i].resize(m);
		char temp;
		scanf("%c", &temp);
		for (int j = 0; j < m; j++) {
			scanf("%c", &temp);
			v[i][j] = (temp == '1');
		}
	}
	cout << choose(v);
}