#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][j] && v[(i + 1) % v.size()][j] && v[i][(j + 1) % v[0].size()] && v[(i + 1) % v.size()][(j + 1) % v[0].size()]) || (!v[i][j] && !v[(i + 1) % v.size()][j] && !v[i][(j + 1) % v[0].size()] && !v[(i + 1) % v.size()][(j + 1) % v[0].size()]));
	}
	
	int kek = 0;
	for (int i = 0; i < v.size(); i++) {
		int tmp = 0;
		for (int j = 0; j < v[0].size(); j++)
			tmp += wtf[i][j];
		kek += (tmp == v[0].size());
	}
	for (int j = 0; j < v[0].size(); j++) {
		int tmp = 0;
		for (int i = 0; i < v.size(); i++)
			tmp += wtf[i][j];
		kek += (tmp == v.size());
	}

	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 - kek + (ans == v.size() * v[0].size());
}

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

	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);
}