#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000005;
const long long INFLL = 1000000000000000002ll;
const long long MOD = 1000000007;
inline long long min(long long a, long long b, long long c) {
  return min(min(a, b), c);
}
inline long long min(long long a, long long b, long long c, long long d) {
  return min(min(min(a, b), c), d);
}
inline long long max(long long a, long long b, long long c) {
  return max(max(a, b), c);
}
inline long long max(long long a, long long b, long long c, long long d) {
  return max(max(max(a, b), c), d);
}
bool U[2505];
int A[5005][5005], H, W, S[5005][5005];
vector<int> P;
int sum(int i1, int j1, int i2, int j2) {
  return S[i2][j2] - S[i1 - 1][j2] - S[i2][j1 - 1] + S[i1 - 1][j1 - 1];
}
int solve(int k) {
  int ret = 0;
  for (int i1 = 1; i1 <= H; i1 += k)
    for (int j1 = 1; j1 <= W; j1 += k) {
      int i2 = i1 + k - 1;
      int j2 = j1 + k - 1;
      int on = sum(i1, j1, i2, j2);
      int toggle = min(k * k - on, on);
      ret += toggle;
    }
  return ret;
}
int main() {
  for (int i = (2); i <= (2500); i++)
    if (!U[i]) {
      P.push_back(i);
      for (int j = i; j <= 2500; j += i) U[j] = true;
    }
  cin >> H >> W;
  for (int i = (1); i <= (H); i++) {
    string s;
    cin >> s;
    for (int j = (1); j <= (W); j++) A[i][j] = s[j - 1] == '0' ? 0 : 1;
  }
  for (int i = (1); i <= (5000); i++)
    for (int j = (1); j <= (5000); j++)
      S[i][j] = S[i][j - 1] + S[i - 1][j] - S[i - 1][j - 1] + A[i][j];
  int ans = INF;
  for (int p : P) ans = min(ans, solve(p));
  cout << ans;
}
