Var
  A, B1, B2: array [1..50, 1..50] of byte;
  N, M, i, j, k1, k2, AmA, AmB, L1, L2, P1, P2, T1, T2, Bt1, Bt2, Col: integer;
  f: boolean;

function DelRow(j: integer): boolean;
var i: integer; f: boolean;
begin
  f := true;
  for i := 1 to M do
    if A[j,i] = 2 then begin f := false; Break; end;
  if f then
    for i := 1 to M do A[j,i] := 0;
  DelRow := f;
end;

function DelCol(i: integer): boolean;
var j: integer; f: boolean;
begin
  f := true;
  for j := 1 to N do
    if A[j,i] = 2 then begin f := false; Break; end;
  if f then
    for j := 1 to N do A[j,i] := 0;
  DelCol := f;
end;

procedure DoCol;
var c: integer;
begin
  for j := 1 to N do
    for i := 1 to M do
      if (B1[j,i] = 2) or (B2[j,i] = 2) then begin
	if A[j,i] = 1 then Exit;
	Inc(c);
      end;
  Col := c;
end;

BEGIN
  Assign(input, 'INPUT.TXT'); Reset(input);
  Readln(N, M);
  for j := 1 to N do
    for i := 1 to M do
      Read(A[j,i]);

  j := 1; while (j <= N) and DelRow(j) do Inc(j);
  j := N; while (j >= 1) and DelRow(j) do Dec(j);
  i := 1; while (i <= M) and DelCol(i) do Inc(i);
  i := M; while (i >= 1) and DelCol(i) do Dec(i);

  FillChar(B1, SizeOf(B1), 0);
  FillChar(B2, SizeOf(B2), 0);

  for j := 1 to N do begin
    f := true;
    for i := 1 to M do
      if A[j,i] = 1 then begin f := false; Break; end;
    if f then
      for i := 1 to M do begin B1[j,i] := A[j,i]; A[j,i] := 0; end;
  end;
  for i := 1 to M do begin
    f := true;
    for j := 1 to N do
      if A[j,i] = 1 then begin f := false; Break; end;
    if f then
      for j := 1 to N do B2[j,i] := A[j,i];
  end;

  L1 := 0; L2 := 0; P1 := MaxInt; P2 := P1;

  AmA := 0; AmB := 0;
  for j := 1 to N do for i := 1 to M do if A[j,i] = 2 then Inc(AmA);
  for j := 1 to N do
    for i := 1 to M do
      if (B1[j,i] = 2) or (B2[j,i] = 2) then Inc(AmB);
  if AmA <> AmB then begin
    Writeln(-1);
    Exit;
  end;

  for j := 1 to N do begin
    k1 := MaxInt; k2 := 0;
    for i := 1 to M do
      if B1[j,i] = 2 then begin
	if k1 > i then k1 := i;
	if k2 < i then k2 := i;
      end;
    for i := k1 to k2 do B1[j,i] := 2;
    if k1 < P1 then P1 := k1;
    if k2 - P1 > L1 then L1 := k2 - P1;
  end;
  for i := 1 to M do begin
    k1 := MaxInt; k2 := 0;
    for j := 1 to N do
      if B2[j,i] = 2 then begin
	if k1 > j then k1 := j;
	if k2 < j then k2 := j;
      end;
    for j := k1 to k2 do B1[j,i] := 2;
    if k1 < P2 then P2 := k1;
    if k2 - P2 > L2 then L2 := k2 - P2;
  end;

  T1 := MaxInt; T2 := T1; Bt1 := 0; Bt2 := 0;
  for j := 1 to N do
    for i := 1 to M do
      if B1[j,i] = 2 then begin
        if T1 > j then T1 := j;
	if Bt1 < j then Bt1 := j;
      end;
  for i := 1 to M do
    for j := 1 to N do
      if B2[j,i] = 2 then begin
        if T2 > i then T2 := i;
	if Bt2 < i then Bt2 := i;
      end;

  for j := T1 to Bt1 do
    for i := L1 to L1 + P1 do
      B1[j,i] := 2;
  for i := T2 to Bt2 do
    for j := L2 to L2 + P2 do
      B2[j,i] := 2;

  Col := -1;
  DoCol;
  Assign(output, 'OUTPUT.TXT'); Rewrite(output);
  Writeln(Col);
END.