Instead of using "set at (loopindex, 1)", use (array.width-1, 1). The first loop pushes stuff to the end and loopindex ends up being at the end of the array. In the second loop, loopindex is not the end of array. You could also use loopindex+TileFloor.count instead of loopindex in the second loop.
You can offset the grid using this.
grid_position = round((x-offset)/gridsize)*gridsize+offset
For example when the walls are vertical.
x = round((Mouse.X-24)/48)*48+24
y = round((Mouse.Y)/48)*48