#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <string>

using namespace std;

const int maxn = (int) 1e3 + 100;

int n, m;
int num1, num2;
char c[maxn][maxn];

int search() {
	int res = 0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (c[i][j] == '1') {
				if (c[i + 1][j] == '1' && c[i][j + 1] == '1' && c[i + 1][j + 1] == '1') {
					res++;
				}
			} else if (c[i][j] == '2') {
				if (c[i + 1][j] == '2' && c[i][j + 1] == '2' && c[i + 1][j + 1] == '2') {
					res++;
				}
			}
		}
	}

	return res;
}

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

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;

		for (int j = 0; j < m; j++) {
			if (s[j] == '1') {
				num1++;
			} else {
				num2++;
			}
		}
	}

	if (n < m) {
		swap(n, m);
	}

	if (num1 < num2) {
		swap(num1, num2);
	}

	if (num2 == 0) {
		cout << (n - 1) * (m - 1) << endl;
		return 0;
	}

	int h;

	for (int i = 1; i <= num1; i++) {
		if (i * i <= num1 && (i + 1) * (i + 1) > num1) {
			h = i;
			break;
		}
	}

	if (h > m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (num1 > 0) {
					c[i][j] = '1';
					num1--;
				} else {
					c[i][j] = '2';
				}
			}
		}

		cout << search() << endl;

		return 0;
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			c[i][j] = '2';
		}
	}

	for (int i = 0; i < h; i++) {
		for (int j = 0; j < h; j++) {
			c[i][j] = '1';
		}
	}

	int ost = num1 - h * h;

	for (int i = 0; i < h; i++) {
		if (ost > 0) {
			c[i][h] = '1';
			ost--;
		}
	}

	for (int j = 0; j < h; j++) {
		if (ost > 0) {
			c[h][j] = '1';
		}
	}

	cout << search() << endl;

	return 0;
}