#include <bits/stdc++.h>
using namespace std;
void prime(int);
bool a[5010][5010];
char t[5010][5010];
bool b[5010];
void prime(int n) {
  int i;
  for (i = 2; i <= n; i++) {
    b[i] = true;
  }
  for (i = 2; i < sqrt(n); i++) {
    for (int j = 2; j <= n / i; j++) {
      b[i * j] = false;
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%s", t[i]);
    for (int j = 0; j < m; j++) {
      a[i][j] = t[i][j] - 48;
    }
  }
  int mini = n * m;
  int ma = max(n, m), s, ss = 0, sss = 0;
  prime(ma);
  for (int k = 2; k <= ma; k++) {
    if (!b[k]) continue;
    long long ik = (n / k) * k, jk = (m / k) * k, kk = k * k;
    for (int i = 0; i <= ik; i += k) {
      for (int j = 0; j <= jk; j += k) {
        s = 0;
        for (int l = 0; l < k; l++) {
          for (int b = 0; b < k; b++) {
            s += a[l + i][b + j];
          }
        }
        if (kk - s > s)
          ss += s;
        else
          ss += kk - s;
      }
    }
    if (mini > ss) mini = ss;
    ss = 0;
  }
  cout << mini << endl;
  return 0;
}
