#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, p, M, ans, anss, sum;
char c;
int a[5010][5010], s[5010][5010];
int fmax(int a, int b) { return a > b ? a : b; }
int fmin(int a, int b) { return a < b ? a : b; }
int main() {
  scanf("%d%d", &n, &m);
  memset(a, 0, sizeof(a));
  memset(s, 0, sizeof(s));
  for (i = 1; i <= n; i++) {
    c = getchar();
    for (j = 1; j <= m; j++) {
      c = getchar();
      a[i][j] = c - '0';
    }
  }
  M = fmax(n, m);
  for (i = 1; i <= n + M; i++)
    for (j = 1; j <= m + M; j++)
      s[i][j] = a[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
  M = fmax(n, m);
  ans = M * M;
  for (p = 2; p <= M; p++) {
    anss = 0;
    for (i = 1; i <= n; i = i + p)
      for (j = 1; j <= m; j = j + p) {
        sum = s[i + p - 1][j + p - 1] - s[i + p - 1][j - 1] -
              s[i - 1][j + p - 1] + s[i - 1][j - 1];
        anss = anss + fmin(sum, p * p - sum);
      }
    ans = fmin(ans, anss);
  }
  printf("%d\n", ans);
}
