#include <bits/stdc++.h>
using namespace std;
const int N = 5007;
int n, m;
char a[N][N];
int ps[N][N];
int ans;
int get_sum(int r1, int r2, int c1, int c2) {
  return ps[r2][c2] - ps[r2][c1 - 1] - ps[r1 - 1][c2] + ps[r1 - 1][c1 - 1];
}
int evaluate(int k) {
  int curr, ans = 0, i, j;
  for (i = 1; i <= n; i += k) {
    for (j = 1; j <= m; j += k) {
      curr = get_sum(i, i + k - 1, j, j + k - 1);
      ans += min(curr, k * k - curr);
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%s", a[i] + 1);
    for (j = 1; j <= m; j++) a[i][j] -= '0';
  }
  for (i = 1; i <= 5000; i++) {
    for (j = 1; j <= 5000; j++) {
      ps[i][j] = ps[i - 1][j] + a[i][j];
    }
  }
  for (i = 1; i <= 5000; i++) {
    for (j = 1; j <= 5000; j++) {
      ps[i][j] += ps[i][j - 1];
    }
  }
  ans = 1000000000;
  for (int k = 2; k <= max(n, m); k++) {
    ans = min(ans, evaluate(k));
  }
  printf("%d\n", ans);
  return 0;
}
