#include <bits/stdc++.h>
using namespace std;
const int N = 2500 + 5;
int n, m;
char s[N][N];
int sum[N][N];
int get(int x, int y) {
  if (x > n) x = n;
  if (y > m) y = m;
  return sum[x][y];
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      sum[i][j] =
          sum[i - 1][j] + sum[i][j - 1] + (s[i][j] == '1') - sum[i - 1][j - 1];
  }
  int ans = 2500 * 2500 + 100;
  for (int k = 2; k <= max(n, m); k++) {
    int t = n / k;
    if (n % k) t++;
    int limx = t * k;
    t = m / k;
    if (m % k) t++;
    int limy = t * k;
    int cnt = 0;
    for (int i = 1; i + k - 1 <= limx; i += k) {
      for (int j = 1; j + k - 1 <= limy; j += k) {
        int temp = get(i + k - 1, j + k - 1) - get(i + k - 1, j - 1) -
                   get(i - 1, j + k - 1) + get(i - 1, j - 1);
        cnt += min(temp, k * k - temp);
      }
    }
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
