#include <bits/stdc++.h>
const int64_t kInfty = 1e18;
const double kEps = 1e-7;
const int64_t kMaxN = 2500 + 7;
int64_t n, m;
int64_t a[kMaxN][kMaxN];
int64_t pref[kMaxN][kMaxN];
inline int64_t Get(int64_t x, int64_t y, int64_t u, int64_t v) {
  return pref[u][v] - pref[u][y - 1] - pref[x - 1][v] + pref[x - 1][y - 1];
}
int64_t Toggle(int64_t k) {
  int64_t res = 0;
  for (int64_t i = 1; i <= n; i += k)
    for (int64_t j = 1; j <= m; j += k) {
      int64_t x = Get(i, j, std::min(i + k - 1, n), std::min(j + k - 1, m));
      x = std::min(x, k * k - x);
      res += x;
    }
  return res;
}
int32_t main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cin >> n >> m;
  for (int64_t i = 1; i <= n; ++i)
    for (int64_t j = 1; j <= m; ++j) {
      char c;
      std::cin >> c;
      a[i][j] = c - '0';
    }
  for (int64_t i = 1; i < kMaxN; ++i)
    for (int64_t j = 1; j < kMaxN; ++j)
      pref[i][j] =
          pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + a[i][j];
  int64_t res = kInfty;
  for (int64_t k = 2; k <= std::max(n, m); ++k) {
    int64_t cost = Toggle(k);
    res = std::min(res, cost);
  }
  std::cout << res;
}
