#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <chrono>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iso646.h>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
/*                           mnciitbhu                        */
//
//  |\    /| |\    |    --  |  |  -------  |⎻⎻\   |   |  |    |
//  | \  / | | \   |   /    |  |     |     |   |  |   |  |    |
//  |  \/  | |  \  |  (     |  |     |     |--<   |---|  |    |
//  |      | |   \ |   \    |  |     |     |   |  |   |   \  /
//  |      | |    \|    --  |  |     |     |__/   |   |    --
//
#include <queue>
#include <ratio>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
using namespace std::chrono;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#if not defined(ONLINE_JUDGE) && not defined(_MSVC_LANG) && defined(DEBUG_)
#include "myconfig.hpp"
#else
#define LOL(x) ;
#define LOLX(...) ;
#endif
#if not defined(DEBUG_)
#endif
int main() 
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	ll n, m;
	cin >> n >> m;
	vector<string> ar(n);
	for(auto& x : ar)
		cin >> x;
	vector<vector<ll>> dp(2 * n, vector<ll>(2 * m, 0));
	dp[0][0] = (ar[0][0] == '1');

	for(int i = 1; i < m; ++i)
		dp[0][i] = dp[0][i - 1] + (ar[0][i] == '1');
	
	for(int i = m; i < 2 * m; ++i)
		dp[0][i] = dp[0][i - 1];

	for(int i = 1; i < n; ++i)
		dp[i][0] = dp[i - 1][0] + (ar[i][0] == '1');

	for(int i = n; i < 2 * n; ++i)
		dp[i][0] = dp[i - 1][0];

	for(int i = 1; i < 2 * n; ++i)
	{
		for(int j = 1; j < 2 * m; ++j)
		{
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
			if(i < n and j < m)
				dp[i][j] += (ar[i][j] == '1');
		}
	}

	ll ans = numeric_limits<ll>::max();

	for(auto x : dp)
		LOL(x);

	for(int k = 2; k <= min(n, m); ++k)
	{
		LOL(k);
		ll mx = 0;
		for(int i = 0; i < n; i += k)
		{
			for(int j = 0; j < m; j += k)
			{
				int kz = k - 1;
				ll _1 = 0;
				_1 += dp[i + kz][j + kz];
				if(i != 0)
					_1 -= dp[i - 1][j + kz];
				if(j != 0)
					_1 -= dp[i + kz][j - 1];
				if(i != 0 and j != 0)
					_1 += dp[i - 1][j - 1];
				ll _0 = (k * k) - _1;
				mx += min(_1, _0);
				LOLX(i, j, _0, _1);
			}
		}
		LOL(mx);
		ans = min(mx, ans);
	}
	cout << ans;

	return 0;
}
