Riva: Wire the 9 switches in series and you only need 1 digital pin and the same code you have written.
+1
...R