#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[2560][2560];
int cnt[2560][2560];
bool mark[5555];
const int LIM = 50;
int judge(int bs) {
  int br = (n + bs - 1) / bs;
  int bc = (m + bs - 1) / bs;
  for (int i = 0; i < br; i++)
    for (int j = 0; j < bc; j++) cnt[i][j] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == '1') cnt[i / bs][j / bs]++;
  int ret = 0;
  for (int i = 0; i < br; i++)
    for (int j = 0; j < bc; j++) {
      ret += min(cnt[i][j], bs * bs - cnt[i][j]);
    }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  int ans = n * m * 5;
  for (int i = 2; i <= LIM; i++) {
    if (mark[i]) continue;
    ans = min(ans, judge(i));
    for (int j = 2 * i; j <= LIM; j += i) mark[i] = true;
  }
  ans = min(ans, judge(n));
  ans = min(ans, judge(m));
  printf("%d\n", ans);
  return 0;
}
