function s = polyomino_boundary_word ( p )

%*****************************************************************************80
%
%% polyomino_boundary_word() is given a polyomino and returns its boundary word.
%
%  Discussion:
%
%    The polyomino is described using a binary rectangular array.
%    The 1's indicate the squares that are part of the polyomino.
%    The polyomino should be connected, and have no holes.
%
%           u
%           ^
%           |
%           |
%    l <--- O ---> r
%           |
%           |
%           V
%           d
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    21 June 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer P(M,N): an array describing the polyomino.  Values of 0 are void.
%    Values of 1 indicate a component of the polyomino.
%
%  Output:
%
%    char s(*): the boundary word that describes the polyomino, a sequence of
%    the letters 'r', 'u', 'l', and 'd'.
%
  s = '';

  [ m, n ] = size ( p );
%
%  Find a boundary entry equal to 1.
%
  i0 = -1;
  j0 = -1;
  for i = m : -1 : 1
    for j = 1 : n
      if ( p(i,j) == 1 )
        i0 = i;
        j0 = j;
        break;
      end
    end
    if ( i0 ~= -1 )
      break;
    end
  end

  if ( i0 == -1 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'polyomino_boundary_word(): Error!\n' );
    fprintf ( 1, '  Could not find an entry equal to 1.\n' );
    return
  end
%
%  Initialize the first edge and travel direction.
%
  i0 = i0 + 1;

  i1 = i0;
  j1 = j0;
  i2 = i0;
  j2 = j0 + 1;
  s = 'r';

  while ( true )

    [ i1, j1, i2, j2, s ] = polyomino_boundary_step ( i1, j1, i2, j2, s, p );

    if ( i2 == i0 & j2 == j0 )
      break;
    end

  end

  return
end

