#include <bits/stdc++.h>
using namespace std;
const long long infll = powl(2, 62) - 1;
const long double pi = 4.0 * atanl(1.0);
const long long mod = powl(10, 9) + 7;
bool isPrime(long long num) {
  if (num == 2) return true;
  if (num <= 1 || !(num % 2)) return false;
  long long root = (long long)sqrtl(num);
  for (long long i = 3; i <= root; i += 2)
    if (!(num % i)) return false;
  return true;
}
long long toggle(vector<string> &arr, long long k, long long &row,
                 long long &col) {
  long long counter = 0;
  for (long long i = 0; i < row; i += k) {
    for (long long j = 0; j < col; j += k) {
      long long one = 0, zero = 0;
      for (long long a = i; a < i + k; ++a) {
        for (long long b = j; b < j + k; ++b) {
          if (a >= row || b >= col) {
            zero++;
          } else {
            if (arr[a][b] == '1')
              one++;
            else
              zero++;
          }
        }
      }
      counter += min(one, zero);
    }
  }
  return counter;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long row, col;
  cin >> row >> col;
  vector<string> arr(row);
  for (long long i = 0; i < (long long)(row); ++i) cin >> arr[i];
  long long ans = toggle(arr, 2, row, col);
  for (long long k = 3; k * k <= min(row, col); ++k) {
    if (isPrime(k)) {
      long long temp = toggle(arr, k, row, col);
      ans = min(ans, toggle(arr, k, row, col));
    }
  }
  cout << ans << '\n';
  return 0;
}
