Step 1: Count carbonyl groups before heating:
Compound 1 has one ketone C=O and two acid C=O, that is $3$ in total.
Compound 2 has one ketone C=O and two acid C=O, that is $3$ in total.
Step 2: Which acid groups can leave?:
Only an acid group attached to a carbon next to a C=O (a beta-keto acid) loses $\mathrm{CO_2}$ easily on heating.
In compound 1, both acid groups are on carbons next to the ketone, so both leave. That removes $2$ C=O groups.
In compound 2, the acid groups are on carbons that are not next to the ring ketone, so none can leave.
Step 3: Subtract:
Compound 1 becomes pentan-3-one: $3 - 2 = 1$ carbonyl group.
Compound 2 is unchanged in carbonyl count. Heating may close an anhydride ring, but an anhydride also has two C=O, so the count stays $3$.
Step 4: Add up:
\[ 1 + 3 = 4 \]
Final Answer:
The total number of carbonyl groups is 4.
\[ \boxed{4} \]