function [ i_min, i_max, j_min, j_max, m, n, nst ] = i4st_header_read ( ...
  filename )

%*****************************************************************************80
%
%% i4st_header_read() reads the header of an I4ST file.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 September 2018
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string FILENAME, the name of the file.
%
%  Output:
%
%    integer I_MIN, I_MAX, the minimum and maximum rows.
%
%    integer J_MIN, J_MAX, the minimum and maximum columns.
%
%    integer M, the number of rows.
%
%    integer N, the number of columns.
%
%    integer NST, the number of nonzeros.
%
  input = fopen ( filename );

  if ( input < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'I4ST_HEADER_READ - Fatal error!\n' );
    fprintf ( 1, '  Could not open the input file.\n' );
    error ( 'I4ST_HEADER_READ - Fatal error!' );
    return
  end

  nst = 0;
  i_min = + Inf;
  i_max = - Inf;
  j_min = + Inf;
  j_max = - Inf;

  string = '  %d  %d  %d';

  while ( true )

    line = fgets ( input );

    if ( line == -1 )
      break
    end

    [ x, count ] = sscanf ( line, string );

    i = x(1);
    j = x(2);
    aij = x(3);

    nst = nst + 1;
    i_min = min ( i_min, i );
    i_max = max ( i_max, i );
    j_min = min ( j_min, j );
    j_max = max ( j_max, j );

  end

  fclose ( input );

  m = i_max - i_min + 1;
  n = j_max - j_min + 1;

  return
end
