#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e4 + 6;
int n, m;
char s[3000][3000];
int is_prime[3000];
int cnt;
void init() {
  for (int i = 0; i < 50; i++) {
    is_prime[i] = 1;
  }
  for (int i = 2; i < 50; i++) {
    if (is_prime[i]) {
      for (int j = i + i; j < 50; j += i) is_prime[j] = 0;
    }
  }
}
int main() {
  init();
  scanf("%d%d", &n, &m);
  getchar();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%c", &s[i][j]);
    }
    getchar();
  }
  int ans = 9000000;
  for (int k = 2; k <= 50; k++) {
    if (!is_prime[k]) continue;
    int as = 0;
    for (int i = 0; i < n; i += k) {
      for (int j = 0; j < m; j += k) {
        int sum = 0;
        for (int l = 0; l < k; l++) {
          if (i + l >= n) break;
          for (int o = 0; o < k; o++) {
            if (j + o >= m) break;
            sum += s[i + l][j + o] - '0';
          }
        }
        if (sum < (k * k - 1) / 2 + 1)
          as += sum;
        else
          as += k * k - sum;
      }
    }
    if (as < ans) ans = as;
  }
  printf("%d\n", ans);
  return 0;
}
