#include <bits/stdc++.h>
using namespace std;
template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}
const int N = 5e3 + 7;
int n, m, pre[N][N];
char s[N][N];
int suf(int x, int k) {
  if (x % k == 0) return x / k;
  return x / k + 1;
}
inline int sum(int a, int b, int c, int d) {
  a--, b--;
  return pre[c][d] - pre[c][b] - pre[a][d] + pre[a][b];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> (s[i] + 1);
  for (int i = 1; i <= n + max(n, m); i++) {
    for (int j = 1; j <= m + max(n, m); j++) {
      int val = (s[i][j] == '1' ? 1 : 0);
      pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + val;
    }
  }
  int res = 1e9;
  for (int k = 2; k <= max(n, m); k++) {
    int cnt = 0;
    for (int i = k; i <= suf(n, k) * k; i += k) {
      for (int j = k; j <= suf(m, k) * k; j += k) {
        int sm = sum(i - k + 1, j - k + 1, i, j);
        cnt += min(sm, k * k - sm);
      }
    }
    res = min(res, cnt);
  }
  cout << res << "\n";
  return 0;
}
