#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const double g = 10.0, eps = 1e-7;
const int N = 2500 + 10, maxn = 1000 + 10, inf = 0x3f3f3f;
long long ma[N][N];
long long prime[N], cnt;
bool vis[N];
void getprime() {
  memset(vis, 1, sizeof vis);
  for (int i = 2; i < N / 2; i++) {
    if (vis[i]) {
      prime[cnt++] = i;
      for (int j = 2 * i; j < N / 2; j += i) vis[j] = 0;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cnt = 0;
  getprime();
  int n, m;
  cin >> n >> m;
  memset(ma, 0, sizeof ma);
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) ma[i][j + 1] = s[j] - '0';
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      ma[i][j] += ma[i - 1][j] + ma[i][j - 1] - ma[i - 1][j - 1];
  long long ans = 1e9;
  for (int u = 0; u < cnt; u++) {
    long long p = prime[u];
    if (p > n || p > m) break;
    long long k = 0;
    for (int i = p; i <= n + p; i += p) {
      for (int j = p; j <= m + p; j += p) {
        long long te, x, y;
        if (i > n)
          x = n;
        else
          x = i;
        if (j > m)
          y = m;
        else
          y = j;
        te = ma[x][y] - ma[i - p][y] - ma[x][j - p] + ma[i - p][j - p];
        k += min(te, p * p - te);
      }
    }
    ans = min(ans, k);
  }
  cout << ans << endl;
  return 0;
}
