function pin = variomino_index ( p )

%*****************************************************************************80
%
%% variomino_index() assigns an index to each nonzero entry of 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.
%
%    The index is computed in column major order.
%
%  Example:
%
%    P = 
%       3  0  2  2
%       3  1  2  0
%       0  1  1  0
%
%    PIN =
%      1 0 5 8
%      2 3 6 0
%      0 4 7 0
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    16 April 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer p(m,n): a matrix representing a variomino.
%
%  Output:
%
%    integer pin(m,n): the index of each nonzero entry.
%
  [ m, n ] = size ( p );

  q = ( p ~= 0 );

  qvec = reshape ( q, m * n, 1 );

  qcum = cumsum ( qvec ) .* qvec;

  pin = reshape ( qcum, m, n );

  return
end
