// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

int n, m;
int[][] a;

void main() {
    scan(n, m);
    int lim = 2 * max(n, m);
    a = new int[][](lim + 1, lim + 1);
    iota(1, n + 1).each!(i => a[i][1 .. m + 1] = readln.chomp.map!(b => (b - '0').to!int).array);

    debug {
        writefln("%(%(%s %)\n%)", a);
        writeln();
    }

    foreach (i ; 1 .. lim + 1) {
        foreach (j ; 1 .. lim + 1) {
            a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
        }
    }

    debug {
        writefln("%(%(%s %)\n%)", a);
        writeln();
    }

    long ans = 4L * n * m;

    foreach (k ; 2 .. max(n, m) + 1) {
        long anst = 0;

        foreach (i ; 1 .. (n + k - 1) / k + 1) {
            foreach (j ; 1 .. (m + k - 1) / k + 1) {
                long x = a[i*k][j*k] - a[(i-1)*k][j*k] - a[i*k][(j-1)*k] + a[(i-1)*k][(j-1)*k];
                anst += min(x, k*k - x);
            }
        }

        ans = min(ans, anst);

        debug {
            writefln("k:%d, anst:%d", k, anst);
        }
    }

    writeln(ans);
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}