#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5001;
int N, M;
char grid[MAXN][MAXN];
int pre[MAXN][MAXN];
int main() {
  ios::sync_with_stdio(0);
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> grid[i];
  for (int i = 0; i < MAXN; i++)
    for (int j = 0; j < MAXN; j++)
      if (!(i < N && j < M)) grid[i][j] = '0';
  for (int i = 1; i < MAXN; i++)
    for (int j = 1; j < MAXN; j++)
      pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] +
                  (grid[i - 1][j - 1] == '1');
  int MN = max(M, N), best = M * N;
  for (int K = 2; K <= MN; K++) {
    int cur = 0;
    for (int i = 1; i <= N; i += K)
      for (int j = 1; j <= M; j += K) {
        int x = pre[i + K - 1][j + K - 1] - pre[i + K - 1][j - 1] -
                pre[i - 1][j + K - 1] + pre[i - 1][j - 1];
        cur += min(x, K * K - x);
      }
    best = min(best, cur);
  }
  cout << best << "\n";
  return 0;
}
