reduced number of iterations when looping over rows/cols of matrix