#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5005;
int n, m;
int mat[N][N];
int sum(int x1, int y1, int x2, int y2) {
  return mat[x2][y2] - mat[x2][y1 - 1] - mat[x1 - 1][y2] + mat[x1 - 1][y1 - 1];
}
int mark[N];
int brute(int k) {
  int ans = 0;
  for (int i = 1; i <= n; i += k) {
    for (int j = 1; j <= m; j += k) {
      int tmp = sum(i, j, i + k - 1, j + k - 1);
      ans += min(tmp, k * k - tmp);
    }
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%1d", &mat[i][j]);
  for (int i = 1; i < N; i++)
    for (int j = 1; j < N; j++) {
      mat[i][j] += mat[i - 1][j];
      mat[i][j] += mat[i][j - 1];
      mat[i][j] -= mat[i - 1][j - 1];
    }
  int ans = 1e9;
  for (int k = 2; k <= max(n, m); k++)
    if (!mark[k]) {
      ans = min(ans, brute(k));
      for (int j = k; j < N; j += k) mark[j] = 1;
    }
  printf("%d\n", ans);
}
