#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
double fRand(double fMin, double fMax) {
  double f = (double)rand() / RAND_MAX;
  return fMin + f * (fMax - fMin);
}
template <class T>
T min(T a, T b, T c) {
  return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
int n, m, f[5005][5005];
char s[5005][5005];
bool isPrime(int x) {
  int lim = sqrt(x);
  for (int i = (2); i <= (lim); ++i) {
    if (x & i == 0) return false;
  }
  return true;
}
int Calc(int x1, int y1, int x2, int y2) {
  return f[x2][y2] - f[x1 - 1][y2] - f[x2][y1 - 1] + f[x1 - 1][y1 - 1];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i <= (n - 1); ++i) {
    scanf("%s", s[i]);
    for (int j = (0); j <= (m - 1); ++j) f[i + 1][j + 1] = s[i][j] - '0';
  }
  for (int i = (1); i <= (max(m, n) * 2); ++i)
    for (int j = (1); j <= (max(m, n) * 2); ++j)
      f[i][j] += f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1];
  int ans = 2000000007;
  for (int k = (2); k <= (max(n, m)); ++k) {
    if (isPrime(k)) {
      int sum = 0;
      for (int x = 1; x <= n; x += k) {
        for (int y = 1; y <= m; y += k) {
          int d = Calc(x, y, x + k - 1, y + k - 1);
          sum += min(d, k * k - d);
        }
      }
      ans = min(ans, sum);
    }
  }
  printf("%d", ans);
  return 0;
}
