#include <bits/stdc++.h>
using namespace std;
const int N = 2502;
bool notPrime[N];
vector<int> primes;
void sieve() {
  notPrime[0] = notPrime[1] = true;
  for (int i = (2); i < (N); i++) {
    if (!notPrime[i]) {
      primes.push_back(i);
      for (int j = i + i; j < N; j += i) {
        notPrime[j] = true;
      }
    }
  }
}
int n, m;
int dp[N * 2][N * 2];
void init() {
  sieve();
  scanf("%d %d", &n, &m);
  char row[m + 2];
  for (int i = (0); i < (n); i++) {
    scanf("%s", row);
    for (int j = (0); j < (m); j++) {
      if (row[j] == '1') {
        dp[i + 1][j + 1]++;
      }
    }
  }
  for (int i = (1); i < (N); i++)
    for (int j = (1); j < (N); j++) {
      dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
    }
}
void solve() {
  init();
  int res = INT_MAX;
  for (int &k : primes) {
    int sum = 0;
    int k2 = k * k;
    for (int i = 0; i < n; i += k) {
      for (int j = 0; j < m; j += k) {
        int i2 = min(n, i + k), j2 = min(m, j + k);
        int x = dp[i2][j2] - dp[i2][j] - dp[i][j2] + dp[i][j];
        sum += min(x, k2 - x);
      }
    }
    res = min(res, sum);
  }
  printf("%d\n", res);
}
int main() {
  { solve(); }
  return 0;
}
