handle non square matrix