#include <bits/stdc++.h>
using namespace std;
char mp[5004][5004];
int fp[5004][5004];
int n, m;
int cnt1 = 0, cnt0 = 0, ans = 1999122700ll;
int orphan(int cp) {
  int lop = 0, rm;
  for (int i = 1; i + cp - 1 <= n * 2; i += cp) {
    for (int j = 1; j + cp - 1 <= m * 2; j += cp) {
      rm = fp[i + cp - 1][j + cp - 1] + fp[i - 1][j - 1] -
           fp[i + cp - 1][j - 1] - fp[i - 1][j + cp - 1];
      lop += min(rm, cp * cp - rm);
    }
  }
  return lop;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
  for (int i = n + 1; i <= 2 * n; i++) {
    for (int j = 1; j <= 2 * m; j++) mp[i][j] = '0';
  }
  for (int i = 1; i <= n; i++) {
    for (int j = m + 1; j <= 2 * m; j++) mp[i][j] = '0';
  }
  for (int i = 1; i <= 2 * n; i++) {
    for (int j = 1; j <= 2 * m; j++) {
      fp[i][j] =
          fp[i - 1][j] + fp[i][j - 1] - fp[i - 1][j - 1] + (mp[i][j] - '0');
    }
  }
  for (int k = 2; k <= min(n, m); k++) {
    ans = min(ans, orphan(k));
  }
  cout << ans << endl;
  return 0;
}
