few things to consider :
is it a square matrix all the time ? if so , there is a way to remove a for loop
is it a matrix with less than or equal 4 rows and cols ? if so , directly use indices . Easy on Google.
if the matrix is square you dont need to swap i and j when i == j.