#include <bits/stdc++.h>
using namespace std;
int n, m;
string s;
bool a[5000][5000];
int prex[5001][5001];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '1') a[i][j + 1] = true;
    }
  }
  for (int i = 1; i <= 2 * n; i++) {
    for (int j = 1; j <= 2 * m; j++) {
      prex[i][j] = prex[i][j - 1] + prex[i - 1][j] - prex[i - 1][j - 1] +
                   int(a[i][j] == 1);
    }
  }
  int ans = 0x3fffffff;
  for (int k = 2; k + (n / k) * k <= 2 * n && k + (m / k) * k <= 2 * m; k++) {
    int s = 0;
    for (int i = 1; i <= n / k + 1; i++) {
      for (int j = 1; j <= m / k + 1; j++) {
        int temp = prex[i * k][j * k] - prex[(i - 1) * k][j * k] -
                   prex[i * k][(j - 1) * k] + prex[(i - 1) * k][(j - 1) * k];
        s = s + min(temp, k * k - temp);
      }
    }
    ans = min(ans, s);
  }
  cout << ans << endl;
}
