#include <bits/stdc++.h>
using namespace std;
const long long int dx[4] = {+1, -1, 0, 0};
const long long int dy[4] = {0, 0, +1, -1};
const long long int MOD = 1e9 + 7;
const long long int INF = 1e10;
const long long int N = 5000 + 7;
;
long long int cnt[N][N];
long long int get(long long int x1, long long int y1, long long int x2,
                  long long int y2) {
  return cnt[x2][y2] - cnt[x1 - 1][y2] - cnt[x2][y1 - 1] + cnt[x1 - 1][y1 - 1];
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (long long int j = 1; j <= m; j++) {
      cnt[i][j] = s[j - 1] - '0';
    }
  }
  for (long long int i = 1; i < N; i++) {
    for (long long int j = 1; j < N; j++) {
      cnt[i][j] += cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1];
    }
  }
  long long int ans = INF;
  for (long long int k = 2; k <= max(n, m); k++) {
    long long int nn = ceil(1.0 * n / k) * k;
    long long int mm = ceil(1.0 * m / k) * k;
    long long int here = 0;
    for (long long int i = 1; i <= nn; i += k) {
      for (long long int j = 1; j <= mm; j += k) {
        long long int cur = get(i, j, i + k - 1, j + k - 1);
        here += min(cur, k * k - cur);
      }
    }
    ans = min(ans, here);
  }
  cout << ans;
  return 0;
}
