The constraints are right because you place a wall on the upper left corner of a case.
So if you place a horizontal wall wallX can be from 0 to 7 and wallY from 1 to 8.
Same way for a vertical wall, wallX ranges from 1 to 8 and wallY from 0 to 7.
So with w = h = 9 the absolute constraints are met.