#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
char s[N];
int a[N][N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int sz = max(n, m) * 2;
  for (int i = 1; i <= sz; i++) {
    if (i <= n) scanf("%s", s + 1);
    for (int j = 1; j <= sz; j++) {
      int tmp = 0;
      if (i <= n && j <= m) tmp = s[j] - '0';
      a[i][j] = tmp + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
    }
  }
  int res = sz * sz;
  for (int d = max(n, m); d > 1; d--) {
    int ans = 0;
    int tg = d * d;
    for (int i = 1; i <= n; i += d)
      for (int j = 1; j <= m; j += d) {
        int tmp = (a[i + d - 1][j + d - 1] - a[i + d - 1][j - 1] -
                   a[i - 1][j + d - 1] + a[i - 1][j - 1]);
        ans += min(tmp, tg - tmp);
      }
    res = min(res, ans);
  }
  printf("%d", res);
}
