#include <bits/stdc++.h>
using namespace std;
int pic[2505][2505];
int minFactor[2505];
int prime[2505], primeNum;
int n, m;
int lowbit(int x) { return x & -x; }
void calPrime() {
  for (int i = 2; i < 2500; i++) {
    if (!minFactor[i]) {
      prime[primeNum++] = i;
      minFactor[i] = primeNum;
    }
    for (int j = 1; j <= minFactor[i]; j++) {
      int t = i * prime[j - 1];
      if (t >= 2500) break;
      minFactor[t] = j;
    }
  }
}
void insert(int x, int y, int s) {
  int dy;
  while (x <= n) {
    dy = y;
    while (dy <= m) {
      pic[x][dy] += s;
      dy += lowbit(dy);
    }
    x += lowbit(x);
  }
}
int sum(int x, int y) {
  int ans = 0, dy;
  while (x > 0) {
    dy = y;
    while (dy > 0) {
      ans += pic[x][dy];
      dy -= lowbit(dy);
    }
    x -= lowbit(x);
  }
  return ans;
}
int cal(int x1, int y1, int x2, int y2) {
  return sum(x2, y2) - sum(x2, y1) - sum(x1, y2) + sum(x1, y1);
}
int main() {
  calPrime();
  memset(pic, 0, sizeof(pic));
  while (scanf("%d%d", &n, &m) != EOF) {
    int res = 0x7fffffff;
    char t;
    for (int i = 0; i < n; i++) {
      scanf("%c", &t);
      for (int j = 0; j < m; j++) {
        scanf("%c", &t);
        if (t == '0') insert(i + 1, j + 1, 1);
      }
    }
    scanf("%c", &t);
    for (int ka = 0; ka < primeNum; ka++) {
      int k = prime[ka];
      bool pass = 0;
      int ans = 0;
      int a = n / k + !!(n % k);
      int b = m / k + !!(m % k);
      for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
          int c = 0;
          if ((i + 1) * k <= n && (j + 1) * k <= m)
            c = cal(i * k, j * k, i * k + k, j * k + k);
          else {
            if ((i + 1) * k > n) c += k * ((i + 1) * k - n);
            if ((j + 1) * k > m) c += k * ((j + 1) * k - m);
            if ((i + 1) * k > n && (j + 1) * k > m)
              c -= ((i + 1) * k - n) * ((j + 1) * k - m);
            c += cal(i * k, j * k, min(i * k + k, n), min(j * k + k, m));
          }
          ans += min(c, k * k - c);
          if (ans >= res) {
            pass = 1;
            break;
          }
        }
        if (pass) break;
      }
      res = min(res, ans);
    }
    printf("%d\n", res);
  }
  return 0;
}
