Instead of looping over the entire size, can you loop over the width and add indices for the north and south walls? The same for the height and the east and west walls?