#include <bits/stdc++.h>
using namespace std;
const int N = 2510;
const int INF = 0x3f3f3f3f;
int prime[400];
bool isprime[N];
int k;
void init() {
  k = 0;
  for (int i = 2; i <= N; i++) {
    if (!isprime[i]) {
      prime[k++] = i;
      for (int j = i + i; j <= N; j += i) isprime[j] = 1;
    }
  }
}
char s[N];
int ma[N * 2][N * 2], pre[N * 2][N * 2];
int main() {
  init();
  memset(pre, 0, sizeof(pre));
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i < 2 * N; i++) {
    if (i <= n) scanf("%s", s + 1);
    int tmp = 0;
    for (int j = 1; j <= m; j++) {
      if (i > n)
        ma[i][j] = 0;
      else {
        if (s[j] == '0')
          ma[i][j] = 0;
        else {
          ma[i][j] = 1;
          tmp++;
        }
      }
      pre[i][j] = tmp + pre[i - 1][j];
    }
    for (int j = m + 1; j < N * 2; j++) {
      pre[i][j] = tmp + pre[i - 1][j];
    }
  }
  int ans = INF;
  for (int f = 0; f < k; f++) {
    int p = prime[f];
    int res = 0;
    for (int i = p; i < n + p; i += p) {
      for (int j = p; j < m + p; j += p) {
        int ff = pre[i][j] + pre[i - p][j - p] - pre[i][j - p] - pre[i - p][j];
        res = res + min(p * p - ff, ff);
      }
    }
    ans = min(ans, res);
  }
  printf("%d\n", ans);
  return 0;
}
