#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <unordered_set>
#include <random>
#include <ctime>
#include <queue>
#include <utility>
int nn, mm;
int MAXN = 1e6 * 2;
int IndexToNumber(int ii, int jj) {
return ii * mm + jj;
}
int BitByIndex(int table, int ii, int jj) {
return (table >> IndexToNumber(ii, jj)) % 2;
}
bool Check(int table) {
for (int ii = 0; ii < nn; ++ii) {
for (int jj = 0; jj < mm; ++jj) {
if (ii > 0) {
if (BitByIndex(table, ii, jj) == BitByIndex(table, ii - 1, jj)) {
return false;
}
}
if (jj > 0) {
if (BitByIndex(table, ii, jj) == BitByIndex(table, ii, jj - 1)) {
return false;
}
}
}
}
return true;
}
int XorByIndex(int table, int ii, int jj) {
return table xor (1 << IndexToNumber(ii, jj));
}
void PushNew(std::vector<bool> &used, std::queue<std::pair<int, int>> &qu, int table, int num) {
for (int ii = 0; ii < nn; ++ii) {
for (int jj = 0; jj < mm; ++jj) {
if (ii + 1 != nn) {
int new_table = XorByIndex(table, ii, jj);
new_table = XorByIndex(new_table, ii + 1, jj);
if (!used[new_table]) {
used[new_table] = true;
qu.push({new_table, num + 1});
}
}
if (jj + 1 != mm) {
int new_table = XorByIndex(table, ii, jj);
new_table = XorByIndex(new_table, ii, jj + 1);
if (!used[new_table]) {
used[new_table] = true;
qu.push({new_table, num + 1});
}
}
}
}
}
int Bfs(int table) {
std::vector<bool> used(MAXN);
std::queue<std::pair<int, int>> qu;
qu.push({table, 0});
used[table] = true;
while (!qu.empty()) {
auto item = qu.front();
if (Check(item.first)) {
return item.second;
}
qu.pop();
PushNew(used, qu, item.first, item.second);
}
return -1;
}
int main() {
std::cin >> nn >> mm;
int table = 0;
for (int i = 0; i < nn; ++i) {
for (int j = 0; j < mm; ++j) {
char sim;
std::cin >> sim;
if (sim == '1') {
table = table | (1 << (i * mm + j));
}
}
}
std::cout << Bfs(table);
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}