import java.util.Iterator;
import java.lang.Iterable;

class Matrix<E> implements Iterable<E>{
	private Object[][] m = null;
	int rows = 0;
	int cols = 0;

	public Matrix(int rows, int cols) {
		// TODO: validate rows and cols

        // initialize array

		this.rows = rows;
		this.cols = cols;
	}

    // add(), numRows(), numCols(), and peek() removed

	public Iterator<E> iterator() {
		return new MatrixIterator();
	}

	public Iterator<E> diagonalIterator() {
		return new DiagonalIterator();
	}

	class MatrixIterator implements Iterator<E> {
		private int nextElmRow = 0;
		private int nextElmCol = 0;
		private int count = 0;

		public boolean hasNext() {
			return count < (rows * cols);
		}

		@SuppressWarnings("unchecked")
		public E next() {
	//		System.out.printf("it: %d %d\n", nextElmRow, nextElmCol);
			count++;
			E temp = (E) m[nextElmRow][nextElmCol]; 

			nextElmRow = (nextElmCol == cols - 1) ? nextElmRow + 1: nextElmRow;
			nextElmCol = (nextElmCol == cols - 1) ? 0 : nextElmCol + 1;
	
			return temp;
		}
		

	}

	class DiagonalIterator implements Iterator<E> {
		int index = 0;
		int count = 0;

		public boolean hasNext() {
			return count < rows;
		}

		@SuppressWarnings("unchecked")
		public E next() {
			E temp = (E) m[index][index];

			index++;
			count++;
			return temp;
		}

	}



}
