function ival = r8mat_is_irreducible ( m, n, a )

%*****************************************************************************80
%
%% R8MAT_IS_IRREDUCIBLE determines if an R8MAT is irreducible.
%
%  Discussion:
%
%    The procedure is based on the idea that a matrix can be viewed as
%    the (weighted) adjacency matrix of a directed graph.  Ignoring the
%    sign and magnitude of the entries, we say there is a directed edge from
%    node I to node J if and only if A(I,J) is nonzero.  Then A is
%    irreducible if and only if this directed graph is strongly connected;
%    that is, if and only if it is possible to start at any node and
%    reach any other (distinct) node by a sequence of connecting edges.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 April 2017
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer M, N, the order of the matrix.
%
%    Input, real A(M,N), the matrix.
%
%    Output, integer IVAL,
%    -1, the matrix is not irreducible;
%    1, the matrix is irreducible.
%
  if ( ~ r8mat_is_square ( m, n, a ) )
    ival = -1;
    return
  end

  for base = 1 :  n

    list(1:n) = 0;
    list(base) = 1;

    nfound = 1;
    ntotal = 1;

    level = 0;

    while ( 1 )

      level = level + 1;
      nfound = 0;

      for i = 1 : n

        if ( list(i) == level )

          for j = 1 : n

            if ( a(i,j) ~= 0.0 )

              if ( list(j) == 0 )
                list(j) = level + 1;
                nfound = nfound + 1;
              end

            end

          end
        end
      end

      ntotal = ntotal + nfound;

      if ( n <= ntotal )
        break
      end

      if ( nfound == 0 )
        ival = -1;
        return
      end

    end

  end

  ival = 1;

  return
end
