#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010;
const int mo = 0;
int n, m;
int len;
int ans = maxn * maxn;
char a[maxn][maxn];
int s[maxn][maxn];
int main() {
  cin >> n >> m;
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= n; i++) {
    scanf("%s", a[i] + 1);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (a[i][j] - '0');
    }
    for (int j = m; j <= m * 2; j++) s[i][j] = s[i][m];
  }
  for (int i = n; i <= n * 2; i++) {
    for (int j = 1; j <= m * 2; j++) {
      s[i][j] = s[n][j];
    }
  }
  len = min(n, m);
  int now = 0;
  int in, im;
  for (int i = 2; i <= len; i++) {
    now = 0;
    in = n;
    while (in % i != 0) in++;
    im = m;
    while (im % i != 0) im++;
    for (int j = i; j <= in; j += i) {
      for (int k = i; k <= im; k += i) {
        int sum = s[j][k] + s[j - i][k - i] - s[j - i][k] - s[j][k - i];
        now += i * i - max(sum, i * i - sum);
      }
    }
    ans = min(now, ans);
  }
  printf("%d", ans);
  return 0;
}
