Refactor word search in matrix