#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5e2 + 5;
const int inf = 0x3f3f3f3f;
int mp[maxn][maxn];
char a[maxn];
int n, m;
vector<int> prime;
void pri(int t) {
  bool prim[maxn] = {0};
  for (int i = 2; i * i <= t; i++) {
    if (!prim[i]) {
      prime.push_back(i);
    }
    for (int j = i + i; j <= t; j += i) {
      prim[i] = true;
    }
  }
  return;
}
int main() {
  scanf("%d %d", &n, &m);
  pri(max(n, m));
  for (int i = 1; i <= n; i++) {
    scanf("%s", a + 1);
    for (int j = 1; j <= m; j++) {
      if (a[j] == '1') mp[i][j] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      mp[i][j] = mp[i - 1][j] + mp[i][j - 1] + mp[i][j] - mp[i - 1][j - 1];
    }
  }
  int ans = inf;
  int len = prime.size();
  for (int i = 0; i < len; i++) {
    int k = prime[i];
    int nn = n, mm = m;
    if (n % k != 0) nn = (n / k) * k + k;
    if (m % k != 0) mm = (m / k) * k + k;
    int tmp1 = 0;
    for (int i = 1; i <= nn / k; i++)
      for (int j = 1; j <= mm / k; j++) {
        int x1 = i * k > n ? n : i * k;
        int y1 = j * k > m ? m : j * k;
        int x2 = i * k - k + 1 > n ? n : i * k - k + 1;
        int y2 = j * k - k + 1 > m ? m : j * k - k + 1;
        int tmp =
            mp[x1][y1] + mp[x2 - 1][y2 - 1] - mp[x1][y2 - 1] - mp[x2 - 1][y1];
        if (tmp <= k * k / 2) {
          tmp1 += (tmp);
        } else {
          tmp1 += (k * k - tmp);
        }
      }
    ans = min(ans, tmp1);
  }
  printf("%d\n", ans);
}
