import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;


void main()
{
  auto hw = readln.chomp.split.map!(to!int);
  auto field = new char[][](hw[0], hw[1]);
  foreach (i; 0..hw[0]) {
    auto line = readln.chomp;
    foreach (j; 0..hw[1]) {
      field[i][j] = line[j].to!char;
    }
  }

  auto cost = int.max;

  int solve(int s, int c) {
    if (field[$-1][s] == '0') return -1;
    int add;
    auto f_tmp = new char[](hw[0]);
    foreach (i; 0..hw[0]) {
      f_tmp[i] = field[i][s];
      if (field[i][s] == '0') continue;
      if (s > 0 && field[i][s-1] == field[i][s]) add++;
      if (s < hw[1]-1 && field[i][s+1] == field[i][s]) add++;
    }
    foreach_reverse (i; 1..hw[0]) {
      field[i][s] = field[i-1][s];
    }
    field[0][s] = '0';
    int ret;
    int tmp;
    foreach (i; 0..hw[1]) {
      tmp += solve(i, c + add);
    }
    if(tmp == -hw[1]) {
      cost = min(cost, c);
      ret = 1;
    }

    foreach (i; 0..hw[0]) {
      field[i][s] = f_tmp[i];
    }

    return ret;
  }

  foreach (i; 0..hw[1]) {
    solve(i, 0);
  }

  cost.writeln;
}
