#include <bits/stdc++.h>
using namespace std;
int n, m;
int pm, pn;
const int maxn = 5000 + 50;
bool mat[maxn][maxn];
void pMat(int k) {
  pn = n;
  pm = m;
  pn = pn + (k - pn % k);
  pm = pm + (k - pm % k);
}
int toggle(int k) {
  int res = 0;
  for (int i = 0; i < pn; i += k) {
    for (int j = 0; j < pm; j += k) {
      int cnt = 0;
      for (int l = 0; l < k; l++) {
        for (int q = 0; q < k; q++) {
          cnt += mat[i + l][j + q];
        }
      }
      res += min(cnt, (k * k) - cnt);
    }
  }
  return res;
}
int main() {
  int isPrime[30] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37, 41,
                     43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      mat[i][j] = s[j] - '0';
    }
  }
  int ans = 1000000000;
  for (int i = 0; i < 20; i++) {
    int k = isPrime[i];
    pMat(k);
    int res = toggle(k);
    ans = min(ans, res);
  }
  cout << ans;
  return 0;
}
