{$N+}
program D;

{$APPTYPE CONSOLE}

uses   SysUtils;


type

   D_R=Record
    X,y:Integer;
   End;
   Mas= Array[1..50,1..50] of Byte;
   DV=Array[1..10000] of D_R;
Label Lb,Lb1;
var
  A,B,C:Mas;
  Kol_Dv:Integer;
  Kol_Dv2:Integer;
  Kol_Dv3:integer;
  Not_Dv:Integer;
  Dvoiki:DV;
  Dvoiki2:DV;

  N,M:Integer;
  I,J:Integer;
  I2,J2:Integer;

  Plosh:Longint;
  Plosh2:Longint;
  MinPlosh:Longint;

Procedure CopyMemory(var A:Mas;B:Mas);
var
i,j:Integer;
Begin
         for i:=1 to N Do For j:=1 to M Do A[i,j]:=B[i,j];

End;

Function Max(s,s1:Byte):Byte;
Begin
    if (s>s1)Then Max:=S else Max:=S1;
End;
Function Min(s,s1:Byte):Byte;
Begin
    if (s<s1)Then Min:=S else Min:=S1;
End;

Function MayDraw(A:Mas;x,y,x1,y1:Integer):Boolean;
var MaxX,MaxY:Integer;
  MinX,MinY:Integer;
  i,j:INteger;
Begin
      MaxX:=Max(X,x1);MaxY:=Max(y,y1);
      MinX:=Min(X,x1);MinY:=Min(y,y1);

      for i:=MinX to MaxX Do
          for j:=MinY to MaxY Do
               if(A[J,I]=1)Then Begin  MayDraw:=false;exit;End;

    MayDraw:=true;
End;

Function MayDrawFull(var A:Mas;x,y,x1,y1:Integer):Integer;
var MaxX,MaxY:Integer;
  MinX,MinY:Integer;
  i,j:INteger;
  Res:Integer;
Begin
     MaxX:=Max(X,x1);MaxY:=Max(y,y1);
     MinX:=Min(X,x1);MinY:=Min(y,y1);
     Res:=0;
     Not_Dv:=0;
      for i:=MinX to MaxX Do
          for j:=MinY to MaxY Do Begin
               if(A[J,I]=2)Then Inc(Not_Dv);
               if(A[J,I]<>3)Then Inc(Res);
               A[J,I]:=3;
          End;

     Result:=Res;
End;
Procedure GetTwo;
var i,j:INteger;
Begin
      Kol_Dv:=0;

      for i:=1 to N Do
      For j:=1 to M Do
       if(A[i,j]=2)Then Begin
          INc(Kol_Dv);
          Dvoiki[Kol_Dv].X:=J;
          Dvoiki[Kol_Dv].Y:=I;
       End;

End;

Procedure GetTwo2;
var i,j:INteger;
Begin
      Kol_Dv2:=0;

      for i:=1 to N Do
      For j:=1 to M Do
       if(B[i,j]=2)Then Begin
          INc(Kol_Dv2);
          Dvoiki2[Kol_Dv2].X:=J;
          Dvoiki2[Kol_Dv2].Y:=I;
       End;

End;


begin


   AssignFile(Input,'input.txt');
   Reset(Input);
   Read(N,M);
   MinPlosh:=2147483647;
   For i:=1 to N dO FOR j:=1 TO m do Read(A[i,j]);
   Close(Input);

   GetTwo;
   if(Kol_Dv>500)Then
   begin
    AssignFile(output,'output.txt');
    Rewrite(output);
    write(-1);
    Close(output);

    Goto Lb1;
   end;

   if(Kol_Dv>100)Then
   begin
    AssignFile(output,'output.txt');
    Rewrite(output);
    write(Kol_Dv);
    Close(output);

    Goto Lb1;
   end;



   for i:=1 to Kol_Dv Do Begin
    For j:=I To Kol_Dv Do Begin
     IF(MayDraw(A,Dvoiki[I].x,Dvoiki[I].Y,Dvoiki[J].x,Dvoiki[J].Y))Then
       begin
        CopyMemory(B,A);
        Plosh:=MayDrawFull(B,Dvoiki[I].x,Dvoiki[I].Y,Dvoiki[J].x,Dvoiki[J].Y);
{in}
        GetTwo2;

        if Kol_Dv2 = 0 then
        begin
         if MinPlosh > Plosh then MinPlosh := Plosh;
         Goto Lb;
        end;

        for i2:=1 to Kol_Dv2 Do Begin
         For j2:=I2 To Kol_Dv2 Do Begin
          IF(MayDraw(B,Dvoiki2[I2].x,Dvoiki2[I2].Y,Dvoiki2[J2].x,Dvoiki2[J2].Y))Then
           begin
            CopyMemory(C,B);
            Plosh2:=Plosh + MayDrawFull(C,Dvoiki2[I2].x,Dvoiki2[I2].Y,Dvoiki2[J2].x,Dvoiki2[J2].Y);
            Kol_Dv3:= Kol_Dv2 - Not_Dv;
            if Kol_Dv3 = 0 then
            begin
              if MinPlosh > Plosh2 then MinPlosh := Plosh2;
            end;
           end;
           end;
        end;
{in}
  lb:
       end;
     End;
   End;


   if MinPlosh = 2147483647 then  MinPlosh:=-1;
   AssignFile(output,'output.txt');
   Rewrite(output);
   write(MinPlosh);
   Close(output);
  Lb1:
  { TODO -oUser -cConsole Main : Insert code here }
end.
