#include <bits/stdc++.h>
using namespace std;
const int Z = (int)5e3 + 111;
const int inf = (int)1e9 + 111;
const long long llinf = (long long)1e18 + 5;
const int MOD = (int)1e9 + 7;
int a[Z][Z], pref[Z][Z];
int cnt(int X1, int Y1, int X2, int Y2) {
  int res =
      pref[X2][Y2] - pref[X1 - 1][Y2] - pref[X2][Y1 - 1] + pref[X1 - 1][Y1 - 1];
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      char x;
      scanf(" %c", &x);
      a[i][j] = x == '0' ? 0 : 1;
    }
  }
  for (int i = 1; i <= 2 * max(n, m); ++i) {
    for (int j = 1; j <= 2 * max(n, m); ++j) {
      pref[i][j] =
          pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + a[i][j];
    }
  }
  int ans = inf;
  for (int k = 2; k <= max(n, m); ++k) {
    int cur = 0;
    int CNT = (max(n, m) + k - 1) / k;
    for (int i = 1; i <= CNT; ++i) {
      for (int j = 1; j <= CNT; ++j) {
        int pos1 = k * (i - 1) + 1;
        int pos2 = k * (j - 1) + 1;
        cur += min(k * k - cnt(pos1, pos2, pos1 + k - 1, pos2 + k - 1),
                   cnt(pos1, pos2, pos1 + k - 1, pos2 + k - 1));
      }
    }
    ans = min(ans, cur);
  }
  cout << ans;
  return 0;
}
