From the Formula 1 website:
Unless he drives for more than one team, each driver may use no more than four power units during a championship season.
The power unit is deemed to consist of six separate elements, of which five of each are available to a driver during the season before they are penalised. The elements are [various defined items].
Each driver is permitted to use only four of each of the above six elements during a championship season and any combination of them may be fitted to a car at any one time. Should a driver use more than four of any of the six power unit elements during the course of a season, a grid penalty will be imposed.
In simpler language: an "engine" consists of six different "parts". Each driver may replace each part four times before receiving a penalty. They are further limited so that they cannot replace all parts four times.
Thus for the purposes of the engine replacement limit, the "engine" is counted as one complete set of the engine components available. When you've used one complete set of components, you've used one engine. When you've used four complete sets of components, regardless of the order in which you replaced any specific components, you've used four engines and won't be allowed any more.
In terms of the Racecar of Theseus, it's all still the same engine until you've replaced everything at least once - then for the purpose of counting engines, that's when it becomes a new one!