#include <bits/stdc++.h>
using namespace std;
const int MAXX = 2e3 + 5;
bool p[MAXX];
void sieve() {
  p[0] = p[1] = 1;
  for (int i = 2; i * i < MAXX; i++)
    if (!p[i])
      for (int j = i * i; j < MAXX; j += i) p[j] = true;
}
int n, m;
int sum[5005][5005];
inline int query(int x1, int y1, int x2, int y2) {
  x1--;
  y1--;
  return sum[x2][y2] - sum[x2][y1] - sum[x1][y2] + sum[x1][y1];
}
string str;
int ans = INT_MAX;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  sieve();
  cin >> n >> m;
  for (int i = 1; i <= (n); i++) {
    cin >> str;
    for (int j = 1; j <= (m); j++) sum[i][j] = (str[j - 1] == '1');
  }
  for (int i = 1; i <= (5003); i++) {
    for (int j = 1; j <= (5003); j++) {
      sum[i][j] += sum[i][j - 1];
    }
  }
  for (int j = 1; j <= (5003); j++) {
    for (int i = 1; i <= (5003); i++) {
      sum[i][j] += sum[i - 1][j];
    }
  }
  for (int k = 2; k <= max(m, n); k++) {
    if (p[k]) continue;
    int cur = 0;
    for (int i = 1; i <= n; i += k) {
      for (int j = 1; j <= m; j += k) {
        int cnt = query(i, j, i + k - 1, j + k - 1);
        cur += min(cnt, k * k - cnt);
      }
    }
    ans = min(ans, cur);
  }
  cout << ans << endl;
  return 0;
}
