optimize matrix scan

eliminate "row" loop in matrix scan