#include <bits/stdc++.h>
using namespace std;
bool prime[5500];
int rec[1500];
int cnt = 0;
void init_prime_table(int n) {
  cnt = 0;
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (prime[i]) rec[cnt++] = i;
    for (int j = 0; j < cnt && rec[j] <= n / i; ++j) {
      prime[i * rec[j]] = false;
      if (i % rec[j] == 0) break;
    }
  }
}
int mm[5005][5005] = {0};
int sum[5005][5005] = {0};
int main() {
  init_prime_table(2600);
  int n, m;
  scanf("%d%d%*c", &n, &m);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      mm[i][j] = getchar() - '0';
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + mm[i][j];
    }
    getchar();
  }
  int ans = 5000 * 5000, up = max(n, m);
  for (int k = 0; rec[k] <= up; ++k) {
    int temp = 0, tt = rec[k] * rec[k];
    for (int i = rec[k]; i < n + rec[k]; i += rec[k]) {
      for (int j = rec[k]; j < m + rec[k]; j += rec[k]) {
        int a = min(n, i), b = min(m, j);
        int e = sum[a][b] - sum[a][j - rec[k]] - sum[i - rec[k]][b] +
                sum[i - rec[k]][j - rec[k]];
        temp += min(e, tt - e);
      }
    }
    ans = min(ans, temp);
  }
  printf("%d\n", ans);
  return 0;
}
