Each element of the matrix has two indexes, so you need to use a nested loop to iterate through all the elements.
Usually a matrix is iterated row by row: the outer loop iterates over the row indices, while the inner loop iterates over the column indices.
But if necessary, you can iterate over the matrix and by columns, then the cycles are reversed.