#include <bits/stdc++.h>
using namespace std;
int mp[5050][5050];
int tmp[5050][5050];
bool vis[5000];
int prim[5000];
int n, m, cnt;
void init() {
  memset(vis, 0, sizeof(vis));
  memset(prim, 0, sizeof(prim));
  for (int i = 2; i <= 3000; ++i) {
    if (!vis[i]) {
      prim[cnt++] = i;
      for (int j = i; j <= 3000; j += i) vis[j] = 1;
    }
  }
}
int check(int k) {
  int ans = 0;
  memset(tmp, 0, sizeof(tmp));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) tmp[i / k][j / k] += mp[i][j];
  }
  for (int i = 0; i < (n / k + ((n % k > 0) ? 1 : 0)); ++i) {
    for (int j = 0; j < (m / k + ((m % k > 0) ? 1 : 0)); ++j) {
      ans += min(k * k - tmp[i][j], tmp[i][j]);
    }
  }
  return ans;
}
int main() {
  init();
  char tmp;
  while (~scanf("%d%d", &n, &m)) {
    getchar();
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        scanf("%c", &tmp);
        mp[i][j] = (tmp == '0' ? 0 : 1);
      }
      getchar();
    }
    int ans = 0x3f3f3f3f, up = max(n, m);
    up = sqrt(up) + 1;
    for (int i = 0; i < cnt; ++i) {
      if (prim[i] <= up) ans = min(ans, check(prim[i]));
    }
    printf("%d\n", ans);
  }
  return 0;
}
