#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5111;
int n, m;
int a[MAXN][MAXN], s[MAXN][MAXN];
int get(int x1, int y1, int x2, int y2) {
  return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];
}
int main() {
  ios::sync_with_stdio(false);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("\n");
    for (int j = 1; j <= m; j++) {
      char c;
      scanf("%c", &c);
      a[i][j] = c - '0';
    }
  }
  for (int i = 1; i <= 2 * max(n, m); i++) {
    for (int j = 1; j <= 2 * max(n, m); j++) {
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
    }
  }
  int ans = 1e9;
  for (int k = 2; k <= max(n, m); k++) {
    int n1 = ((n + k - 1) / k) * k, m1 = ((m + k - 1) / k) * k;
    int total = 0;
    for (int i = k; i <= n1; i += k) {
      for (int j = k; j <= m1; j += k) {
        int sum = get(i - k + 1, j - k + 1, i, j);
        total += min(k * k - sum, sum);
      }
    }
    ans = min(ans, total);
  }
  cout << ans;
}
