#include <bits/stdc++.h>
using namespace std;
int n, m;
string img[2505];
int p[5005][5005];
inline int at(int i, int j) {
  if (i < n && j < m) {
    return img[i][j] - '0';
  }
  return 0;
}
inline void pre() {
  p[0][0] = img[0][0] - '0';
  for (int i = 1; i < 5005; ++i) {
    p[0][i] = p[0][i - 1] + at(0, i);
  }
  for (int i = 1; i < 5005; ++i) {
    p[i][0] = p[i - 1][0] + at(i, 0);
  }
  for (int i = 1; i < 5005; ++i) {
    for (int j = 1; j < 5005; ++j) {
      p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + at(i, j);
    }
  }
}
inline int sum(int i1, int j1, int i2, int j2) {
  int res = p[i2][j2];
  if (i1 > 0) {
    res -= p[i1 - 1][j2];
  }
  if (j1 > 0) {
    res -= p[i2][j1 - 1];
  }
  if (i1 > 0 && j1 > 0) {
    res += p[i1 - 1][j1 - 1];
  }
  return res;
}
inline int sum(int i, int j, int k) { return sum(i, j, i + k - 1, j + k - 1); }
inline int compress(int k) {
  int n2 = (n + k - 1) / k;
  int m2 = (m + k - 1) / k;
  int res = 0;
  int size = k * k;
  for (int i = 0; i < n2; ++i) {
    for (int j = 0; j < m2; ++j) {
      int o = sum(i * k, j * k, k);
      if (2 * o > size) {
        res += size - o;
      } else {
        res += o;
      }
    }
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  cin.ignore();
  for (int i = 0; i < n; ++i) {
    getline(cin, img[i]);
  }
  pre();
  int ans = 2000000000;
  for (int k = 2; k <= max(n, m); ++k) {
    ans = min(ans, compress(k));
  }
  cout << ans;
  return 0;
}
