Since the printing is pretty much hard coded my advice is to create a data strcture and store rows and column indexes in it.
for example
Index[0] >= print row[0]col[0] to row[2]col[3]
Index[1] >= print row[3]col[0] to row[4]col[3]
Index[2] >= print row[0]col[4] to row[2]col[8]
Index[3] >= print row[3]col[4] to row[4]col[8]
and do an iteration on the index. Hopefully this should sort your problem