#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, m;
char pic[5010][5010];
int pre[2510][2510];
int P(int x, int y) { return pre[min(x, n)][min(y, m)]; }
int sum(int x1, int y1, int x2, int y2) {
  return P(x2, y2) - P(x1 - 1, y2) - P(x2, y1 - 1) + P(x1 - 1, y1 - 1);
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) scanf("%s", pic[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      pic[i][j] -= '0';
      pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + pic[i][j];
    }
  int ans = n * m + 1;
  for (int k = 2; k <= min(n, m); k++) {
    int tmp = 0;
    for (int i = 1; i <= n; i += k)
      for (int j = 1; j <= m; j += k)
        tmp += min(k * k - sum(i, j, i + k - 1, j + k - 1),
                   sum(i, j, i + k - 1, j + k - 1));
    ans = min(ans, tmp);
  }
  printf("%d\n", ans);
  return 0;
}
