#include <bits/stdc++.h>
using namespace std;
char mp[5001][5001];
int psm[5001][5001], psm2d[5001][5001];
int getsum(int x1, int y1, int x2, int y2) {
  return psm2d[x2][y2] - psm2d[x1 - 1][y2] - psm2d[x2][y1 - 1] +
         psm2d[x1 - 1][y1 - 1];
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%s", mp[i] + 1);
  int maxk = max(n, m);
  for (int i = 1; i <= 2 * maxk; ++i)
    for (int j = 1; j <= 2 * maxk; ++j)
      psm[i][j] = psm[i][j - 1] + (mp[i][j] == '1');
  for (int i = 1; i <= 2 * maxk; ++i)
    for (int j = 1; j <= 2 * maxk; ++j)
      psm2d[i][j] = psm2d[i - 1][j] + psm[i][j];
  int ans = 1000000000;
  for (int k = 2; k <= maxk; ++k) {
    int tans = 0;
    for (int i = k; i - k + 1 <= n; i += k)
      for (int j = k; j - k + 1 <= m; j += k) {
        int tmp = getsum(i - k + 1, j - k + 1, i, j);
        tans += min(tmp, k * k - tmp);
      }
    ans = min(ans, tans);
  }
  printf("%d", ans);
  return 0;
}
