#pragma warning(disable:4996)
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;


int n, m;
char c[1007][1007];

int chk() {
	int i, j, s = 0;
	for (i = 0; i < n - 1; i++) {
		for (j = 0; j < m - 1; j++) {
			if (c[i][j] == c[i + 1][j] && c[i][j] == c[i][j + 1] && c[i][j] == c[i + 1][j + 1]) {
				s++;
			}
		}
	}
	return s;
}

void sol() {
	int i, j, c1 = 0, c2 = 0, t;
	scanf("%d%d\n", &n, &m);
	for (i = 0; i < n; i++) {
		for (j = 0; j < m; j++) {
			c[i][j] = getchar();
			if (c[i][j] == '1')c1++;
			else c2++;
		}
		getchar();
	}
	int ans = 0, a, b;
	if (n < m) {
		t = n;
		n = m;
		m = t;
	}
	for (i = 0; i < n; i++) {
		for (j = 0; j < m; j++) {
			if (c1 > 0) {
				c[i][j] = '1';
				c1--;
			}
			else {
				c[i][j] = '2';
			}
		}
	}
	ans = max(ans, chk());

	for (i = 2; i <= sqrt((double)c1); i++) {
		if (c1%i == 0) {
			b = i;
			a = c1 / i;
			if (a <= n&&b <= m) {
				ans = max(ans, (m - 1)*(n - 1) - a - b + 1);
			}
		}
	}
	for (i = 2; i <= sqrt((double)c2); i++) {
		if (c2%i == 0) {
			b = i;
			a = c2 / i;
			if (a <= n&&b <= m) {
				ans = max(ans, (m - 1)*(n - 1) - a - b + 1);
			}
		}
	}

	printf("%d", chk());
}

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
	sol();
	return 0;
}