Ok, non avevo considerato alcuni 0, per correggere basta considerare lunghezza%key
Ho modificato in-place http://ideone.com/HwbAjl
In sostanza se lunghezza%key != 0, nell'ultima riga ci saranno, partendo da sinistra, esattamente lunghezza%key caratteri, perciò basta non inserire nelle posizioni
[r-1][j] per ogni lunghezza%key <= j < c