#include <bits/stdc++.h>
using namespace std;
const int maxn = 2505;
const int inf = 0x3f3f3f3f;
char mp[maxn][maxn];
int sum[maxn][maxn], prime[maxn], tail;
bool is_prime[maxn];
int n, m;
void init() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      sum[i][j] =
          sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + mp[i][j] - '0';
    }
  }
  int N = max(n, m);
  for (int i = 2; i <= N; i++) is_prime[i] = 1;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      prime[++tail] = i;
      for (int j = 2 * i; j <= N; j += i) is_prime[j] = 0;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", mp[i] + 1);
  }
  init();
  int ans = inf;
  for (int k = 1; k <= tail; k++) {
    int x = prime[k], temp = 0;
    for (int i = 0; i < n; i += x) {
      for (int j = 0; j < m; j += x) {
        int ii = i + x > n ? n : i + x, jj = j + x > m ? m : j + x;
        int val = sum[ii][jj] - sum[i][jj] - sum[ii][j] + sum[i][j];
        temp += min(x * x - val, val);
      }
    }
    ans = min(ans, temp);
  }
  cout << ans << endl;
  return 0;
}
