#include <bits/stdc++.h>
int d[2510][2510], n, m, i, j, k, q, s = 10e6, c, t;
char b;
int main() {
  std::cin >> n >> m;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      std::cin >> b,
          d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] + (b == '1');
  for (k = 2; k <= (n < m ? m : n); k++) {
    c = 0;
    for (i = k; i <= n + (k - n % k); i += k)
      for (j = k; j <= m + (k - m % k); j += k)
        q = d[i < n ? i : n][j < m ? j : m] + d[i - k][j - k] -
            d[i < n ? i : n][j - k] - d[i - k][j < m ? j : m],
        c += q < k * k - q ? q : k * k - q;
    s = s > c ? c : s;
  }
  std::cout << s;
}
