#include <bits/stdc++.h>
using namespace std;
int cnt, n, m, ans = 1e9;
int pri[500], sum[5010][5010], a[5010][5010];
char ch[5010];
bool isprime[5010];
void getprime() {
  for (int i = 2; i <= 2500; i++) {
    if (!isprime[i]) pri[++cnt] = i;
    for (int j = 1; j <= cnt && i * pri[j] <= 2500; j++) {
      isprime[i * pri[j]] = 1;
      if (i % pri[j] == 0) break;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + 1);
    for (int j = 1; j <= m; j++) a[i][j] = ch[j] - '0';
  }
  for (int i = 1; i <= 5000; i++)
    for (int j = 1; j <= 5000; j++)
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
  getprime();
  for (int p = 1; p <= cnt; p++) {
    int k = pri[p];
    if (k > n && k > m) break;
    int x = n / k, y = m / k, ret = 0;
    if (n % k) x++;
    if (m % k) y++;
    for (int i = 1; i <= x; i++)
      for (int j = 1; j <= y; j++) {
        int s0 = sum[i * k][j * k] - sum[i * k - k][j * k] -
                 sum[i * k][j * k - k] + sum[i * k - k][j * k - k];
        int s1 = k * k - s0;
        ret += min(s0, s1);
      }
    ans = min(ans, ret);
  }
  printf("%d\n", ans);
  return 0;
}
