	PROGRAM convert

C	maximum size of matrix dimension
	INTEGER MAX_DIM
	PARAMETER (MAX_DIM = 2048)

C	maximum size of memory on each processor
	INTEGER MAX_MEM
	PARAMETER (MAX_MEM = 40960)

C	Matrix A
	REAL A(0:MAX_DIM, 0:MAX_DIM)

C	Memory Y
	REAL Y(0:MAX_MEM)

C	Matrix size ROWS * COLUMNS = (M_total * N_total)
	INTEGER M_total, N_total

C	each BLOCK index (i_start, j_start) ====> (i_end, j_end)
	INTEGER i_start, j_start
	INTEGER i_end, j_end

C	count is the sum of input elements
	INTEGER count

C	addr is the memory address
	INTEGER addr

	count = 0

C	open the result file for read
	open (UNIT = 10, FILE = "matrix.f.rst")

C	read in the matrix size
	read (10, *) M_total, N_total

C	read in one BLOCK index
100	read (10, *) i_start, j_start, i_end, j_end
	addr = 0

C	read in all one BLOCK elements
	read (10, *) (Y(i), i = 0, (i_end-i_start+1)*(j_end-j_start+1)-1)

C	assign the elements to its corresponding position in the matrix
	DO i = i_start, i_end
	  DO j = j_start, j_end
		A(i, j) = Y(addr)
		addr = addr + 1
	  END DO
	END DO

C	increase the input number
	count = count + (i_end-i_start+1) * (j_end-j_start+1)

	IF (count .lt. (M_total * N_total)) GOTO 100

C	print out the matrix
	DO i = 0, M_total-1
	  print *, (A(i, j), j=0, N_total-1)
	END DO

	END PROGRAM
