Updated piece to calculate dimensions based on the shape matrix