#include <bits/stdc++.h>
using namespace std;
const int mod = 1E9 + 7;
const double eps = 1e-6;
template <typename T>
T abs(T val) {
  return val > 0 ? val : -val;
}
inline int max2(int a, int b) { return ((a > b) ? a : b); }
inline int min2(int a, int b) { return ((a < b) ? a : b); }
inline int max3(int a, int b, int c) { return max2(a, max2(b, c)); }
inline int min3(int a, int b, int c) { return min2(a, min2(b, c)); }
int n, m;
char str[3000];
int MAXX, dp[3000][3000];
int check(int k) {
  int i = k, j = k;
  int ans = 0, area = k * k;
  for (i = k; i <= n; i += k) {
    for (j = k; j <= m; j += k) {
      int tot = dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k];
      int tmp = min(tot, area - tot);
      ans += tmp;
    }
  }
  i -= k;
  j -= k;
  if (i < n || j < m) {
    int jj = m / k * k;
    for (int x = k; x <= jj; x += k) {
      int tot = dp[n][x] - dp[n][x - k] - dp[i][x] + dp[i][x - k];
      int tmp = min(tot, area - tot);
      ans += tmp;
    }
    int ii = n / k * k;
    for (int y = k; y <= ii; y += k) {
      int tot = dp[y][m] - dp[y][jj] - dp[y - k][m] + dp[y - k][jj];
      int tmp = min(tot, area - tot);
      ans += tmp;
    }
    int tot = dp[n][m] - dp[n][jj] - dp[ii][m] + dp[ii][jj];
    int tmp = min(tot, area - tot);
    ans += tmp;
  }
  return ans;
}
void solve() {
  int ans = 0x3f3f3f3f;
  for (int k = 2; k <= MAXX; k++) {
    int tmp = check(k);
    ans = min(ans, tmp);
  }
  printf("%d\n", ans);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(dp, 0, sizeof(dp));
    MAXX = max(n, m);
    for (int i = 1; i <= n; i++) {
      scanf("%s", &str);
      for (int j = 1; j <= m; j++) {
        dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1] +
                   (int)(str[j - 1] - '0');
      }
    }
    solve();
  }
}
