Start by calculating the number of vertical segments.

The number of vertical columns is equal to the perimeter of the top: 2(x + y). That is 14 in the example.

Each vertical column consists of z segments (8 in this example). So, there are 8*14, or 112 vertical segments.

The general formula is 2z(x + y).

Similar formulas calculate the number of segments parallel to the X- and Y-axes.

The rest should be straightforward.