function q = variomino_condense ( p )

%*****************************************************************************80
%
%% variomino_condense() condenses a variomino.
%
%  Discussion:
%
%    A polyomino is a shape formed by connecting unit squares edgewise
%
%    A variomino is a polyomino in which each square has been assigned
%    a value between 1 and v.
%
%    A variomino can be represented by an MxN matrix, whose entries are
%    between 1 and v for squares that are part of the variomino, 
%    and 0 otherwise.
%
%    This program is given an MxN matrix that is meant to represent a 
%    variomino.  It  "condenses" the matrix, if possible, by removing initial 
%    and final rows and columns that are entirely zero.
%
%    While this procedure might save a slight amount of space, its purpose
%    is to simplify the task of manipulating variominos, embedding them in
%    larger shapes, and detecting whether two variominos describe the same
%    shape.
%
%    It is entirely possible, and usual, that the output quantities are
%    simply copies of the input quantities.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    16 April 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer p(mp,np): the matrix representing the variomino.  
%
%  Output:
%
%    integer q(mq,nq): the matrix representing the condensed variomino.
%
  [ mp, np ] = size ( p );
%
%  Discard nonsense.
%
  if ( mp <= 0 | np <= 0 )
    mq = 0;
    nq = 0;
    q = [];
    return
  end
%
%  Copy the input.
%
  mq = mp;
  nq = np;
  q = p;
%
%  If row 1 is zero, remove it.
%
  while ( all ( q(1,:) == 0 ) ) 
    q = q(2:mq,:);
    mq = mq - 1;
    if ( mq <= 0 )
      mq = 0;
      nq = 0;
      q = [];
      return
    end
  end
%
%  If row MQ is zero, remove it.
%
  while ( all ( q(mq,:) == 0 ) )
    q = q(1:mq-1,:);
    mq = mq - 1;
    if ( mq <= 0 )
      mq = 0;
      nq = 0;
      q = [];
      return
    end
  end
%
%  If column 1 is zero, remove it.
%
  while ( all ( q(:,1) == 0 ) )
    q = q(:,2:nq);
    nq = nq - 1;
    if ( nq <= 0 )
      mq = 0;
      nq = 0;
      q = [];
      return
    end
  end
%
%  If column N is zero, remove it.
%
  while ( all ( q(:,nq) == 0 ) )
    q = q(:,1:nq-1);
    nq = nq - 1;
    if ( nq <= 0 )
      mq = 0;
      nq = 0;
      q = [];
      return
    end
  end

  return
end
