#include <bits/stdc++.h>
using namespace std;
char mp[5050][5050];
int n, m;
int dp[5050][5050];
long long a = 0;
long long minn = LONG_LONG_MAX;
void init() {
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= 5050 - 1; i++)
    for (int j = 1; j <= 5050 - 1; j++)
      dp[i][j] = mp[i][j] == '1' ? dp[i][j - 1] + 1 : dp[i][j - 1];
  for (int i = 1; i <= 5050 - 1; i++)
    for (int j = 1; j <= 5050 - 1; j++) dp[i][j] += dp[i - 1][j];
}
long long solve() {
  int t = max(n, m);
  for (int k = 2; k <= t; k++) {
    a = 0;
    for (int i = k; i <= n + k; i += k) {
      for (int j = k; j <= m + k; j += k) {
        int temp = dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k];
        a += 1LL * min(temp, k * k - temp);
      }
    }
    if (a < minn) minn = a;
  }
  return minn;
}
int main(void) {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
  init();
  long long ans = solve();
  cout << ans << endl;
}
