#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2500 + 5;
vector<long long> prime;
long long pre[2 * maxn][2 * maxn];
void precal() {
  vector<bool> isP(maxn, 1);
  for (long long i = 2; i < maxn; ++i) {
    if (isP[i]) {
      prime.push_back(i);
      for (long long j = i * i; j < maxn; j += i) isP[j] = 0;
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  precal();
  long long n, m, ans = 1E8, cnt, MaxN, MaxM, val;
  cin >> n >> m;
  vector<string> v(n);
  for (auto &S : v) cin >> S;
  for (long long i = 1; i < 2 * maxn; ++i) {
    for (long long j = 1; j < 2 * maxn; ++j) {
      if (i > n)
        pre[i][j] = pre[i - 1][j];
      else if (j > m)
        pre[i][j] = pre[i][j - 1];
      else
        pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] +
                    (v[i - 1][j - 1] == '1');
    }
  }
  for (auto p : prime) {
    cnt = 0, MaxN = (n - 1) / p * p + p, MaxM = (m - 1) / p * p + p;
    for (long long i = p; i <= MaxN; i += p) {
      for (long long j = p; j <= MaxM; j += p) {
        val = pre[i][j] - pre[i - p][j] - pre[i][j - p] + pre[i - p][j - p];
        cnt += min(val, p * p - val);
      }
    }
    ans = min(ans, cnt);
    if (p > max(n, m)) break;
  }
  cout << ans << "\n";
  return 0;
}
