Let's start with what the assignment apparently expected you to do. We can see that the isolated spade will be blob #2, so what is its perimeter?
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1332410/image.png');
imshow(label2rgb(bwlabel(P),lines(7)))
S = regionprops(P,'area');
This gets a lot more sensible if you just deal with everything as a binary image where the spades are in the foreground. If you do that, you don't need to get rid of edge effects at the image border, and you actually have more than one object to start with. Note that because we're calculating the perimeter of the spades instead of the holes in the white background, the result is slightly different by a few pixels. That said, it's still within the requirements.
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1332410/image.png');
S = regionprops(P,'area');
Alternatively, since we're dealing with a positive image now, we could just ask regionprops() for the object perimeters. This wouldn't have worked with the image inverted, since all the spades would be holes belonging to one object.
Bear in mind though that this calculates the perimeter by a different method (see regionprops()), so it won't match the expectations given. A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1332410/image.png');
S = regionprops(BW,'perimeter');