function fnorm = r8mat_is_stochastic ( m, n, a )

%*****************************************************************************80
%
%% R8MAT_IS_STOCHASTIC checks whether an R8MAT is stochastic.
%
%  Discussion:
%
%    A matrix is stochastic if it has no negative entries,
%    and every row sums to 1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    17 April 2017
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer M, N, the row and column dimensions of 
%    the matrix.  M and N must be positive.
%
%    Input, real A(M,N), the matrix.
%
%    Output, real FNORM, the norm of the stochastic error.
%
  t1 = 0.0;
  for j = 1 : n
    for i = 1 : m
      if ( a(i,j) < 0.0 )
        t1 = t1 - a(i,j);
      end
    end
  end

  t2 = 0.0;
  for i = 1 : m
    t2 = t2 + abs ( sum ( a(i,1:n) ) - 1.0 );
  end 

  fnorm = t1 + t2;

  return
end
