function ival = r8mat_is_permutation ( m, n, a )

%*****************************************************************************80
%
%% R8MAT_IS_PERMUTATION checks whether A is a permutation matrix.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 April 2017
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, N, the order of the matrix.
%
%    real A(M,N), the matrix.
%
%  Output:
%
%    integer IVAL:
%    -1, the matrix is not square;
%    -2, the matrix is not a zero-one matrix.
%    -3, there is a row that does not sum to 1.
%    -4, there is a column that does not sum to 1.
%    1, the matrix is a permutation matrix,
%
  if ( ~ r8mat_is_square ( m, n, a ) )
    ival = -1;
    return
  end

  jval = r8mat_is_zero_one ( m, n, a );

  if ( jval ~= 1 )
    ival = -2;
    return
  end

  for i = 1 : m
    if ( sum ( a(i,1:n) ) ~= 1.0 )
      ival = -3;
      return
    end
  end

  for j = 1 : n
    if ( sum ( a(1:m,j) ) ~= 1.0 )
      ival = -4
      return
    end
  end

  ival = 1;

  return
end
