#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, x, V, an, X1, Y1, X2, Y2, V1, S[2525][2525];
char s[2525];
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    for (scanf("%s", s + 1), j = 1; j <= m; j++)
      S[i][j] = S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1] + s[j] - '0';
  V = 1e9;
  for (k = 2; k <= 2500; k++) {
    an = 0;
    for (i = k; i - k <= n; i += k) {
      for (j = k; j - k <= m; j += k) {
        X2 = min(i, n);
        Y2 = min(j, m);
        X1 = i - k;
        Y1 = j - k;
        V1 = S[X2][Y2] + S[X1][Y1] - S[X2][Y1] - S[X1][Y2];
        an += min(k * k - V1, V1);
      }
    }
    V = min(V, an);
  }
  printf("%d", V);
}
