#include<iostream>
#include<vector>
#pragma warning (disable:4996)

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
#endif

	int n, m;
	cin >> n >> m;
	
	int n1, n2;
	n1 = 0;
	n2 = 0;
	vector<vector<char> > d(n, vector<char>(m));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> d[i][j];
			if (d[i][j] - '0' == 1)
				n1++;
			else
				n2++;
		}
	}

	if (n <= m) {
		int h1 = n1 / n;
		int h11 = n1 % n;
		int otv1 = 0;

		if(h1 != 0)
		otv1 += (h1 - 1)*(n - 1);
		if (h1 >= 1 && h11 > 0)
			otv1 += h11 - 1;

		int h2 = n2 / n;
		int h22 = n2 % n;
		int otv2 = 0;

		if(h2 != 0)
		otv2 += (h2 - 1)*(n - 1);
		if (h2 >= 1 && h22 > 0)
			otv2 += h22 - 1;

		cout << otv1 + otv2;
	}
	else {
		int y = n;
		n = m;
		m = n;
		int h1 = n1 / n;
		int h11 = n1 % n;
		int otv1 = 0;

		if (h1 != 0)
			otv1 += (h1 - 1)*(n - 1);
		if (h1 >= 1 && h11 > 0)
			otv1 += h11 - 1;

		int h2 = n2 / n;
		int h22 = n2 % n;
		int otv2 = 0;

		if (h2 != 0)
			otv2 += (h2 - 1)*(n - 1);
		if (h2 >= 1 && h22 > 0)
			otv2 += h22 - 1;

		cout << otv1 + otv2;
	}
	
	return 0;
}