#include <bits/stdc++.h>
using namespace std;
char s[5001][5001];
bool isp[5001];
int p[5001];
int cnt;
int sum[5001][5001];
int n, m;
int main() {
  memset(isp, true, sizeof(isp));
  for (int i = 2; i < 5001; ++i) {
    if (isp[i]) {
      p[++cnt] = i;
      for (int j = i * 2; j < 5001; j += i) {
        isp[j] = false;
      }
    }
  }
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
  for (int i = 1; i < 5001; ++i)
    for (int j = 1; j < 5001; ++j)
      sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] +
                  (s[i][j] ? s[i][j] - '0' : 0);
  int ans = 0x3f3f3f3f;
  for (int k = 1; k <= cnt; ++k) {
    int pr = p[k], tot = pr * pr;
    int tans = 0;
    for (int i = 0; i < n; i += pr)
      for (int j = 0; j < m; j += pr) {
        int one =
            sum[i + pr][j + pr] - sum[i + pr][j] - sum[i][j + pr] + sum[i][j];
        tans += min(one, tot - one);
      }
    ans = min(tans, ans);
  }
  printf("%d\n", ans);
  return 0;
}
