{$apptype console}

uses
  Math;

var
  a: array[0..51, 0..51] of byte;
  m, n: integer;
  i1, i2, j1, j2, x, y: integer;
  ii1, ii2, jj1, jj2: integer;
  cur: longint;
  good1, good2: boolean;

function Count: longint;
var
  i, ii, j, jj: integer;
begin
  Result := abs((i1 - i2)*(j1 - j2)) + abs((ii1 - ii2)*(jj1 - jj2));

end;

label
  Stop1, Stop2;  

begin
  assign(input, 'input.txt');
  reset(input);
  assign(output, 'output.txt');
  rewrite(output);

  Read(n, m);
  FillChar(a, sizeof(a), 1);
  for i1 := 1 to m do
    for j1 := 1 to n do Read(a[i1, j1]);


  cur := 10000;
   
  for i1 := 1 to m do
    for j1 := 1 to n do
      if a[i1, j1] = 2 then
        for i2 := i1 to m do
          for j2 := 1 to n do
            if a[i2, j2] = 2 then begin
              good1 := true;
              for x := Min(i1, i2) to Max(i1, i2) do
                for y := Min(j1, j2) to Max(j1, j2) do
                  if a[x, y] = 1 then begin
                    good1 := false;
                    goto Stop1;
                  end;
Stop1:
              if good1 then
                for ii1 := 1 to m do
                  for jj1 := 1 to n do
                    if a[ii1, jj1] = 2 then
                      for ii2 := 1 to m do
                        for jj2 := 1 to n do
                          if a[ii2, jj2] = 2 then begin
                            good2 := true;
                            for x := Min(ii1, ii2) to Max(ii1, ii2) do
                              for y := Min(jj1, jj2) to Max(jj1, jj2) do
                                if a[x, y] = 1 then begin
                                  good2 := false;
                                  goto Stop2;
                                end;
Stop2:                      if good2 then cur := Min(cur, Count);
                          end;
            end;

  if cur < 10000 then WriteLn(-1) else WriteLn(cur);

  Wri

  close(input);
  close(output);
end.