This topic is a bit more nuanced, as the information on Apple's website is slightly inaccurate. Target Display Mode may still be a good solution, but it does have a few more specific requirements when it is used with an M1 Mac.
The 2009 and 2010 27" iMacs are probably the best candidates for the use of Target Display Mode because they use Mini DisplayPort connection rather than the Thunderbolt connection on iMacs of later model years. If you use any different model/size iMac, your results may vary. A Thunderbolt cable won't work either --it needs to be Mini DisplayPort.
Unless the 2009 or 2010 Macs have been specially modified, they also won't run a macOS greater than High Sierra anyway, so this is not typically an issue of concern.
I have both done this myself, and I have also seen successful reports online of other people connecting Mini DisplayPort to USB C cables between iMacs in this age range and post-2019 M1 Mac Computers.
I have also connected computers running operating systems later than macOS Catalina to my own iMac this way successfully. I believe that this is because the earlier model year iMacs act more like standard Mini DisplayPort monitors, and there is no Thunderbolt complexity to deal with for macOS software.
See this video for more info:
So to be more accurate than my previous post,
if you specifically have a 2009 or 2010 27" iMac running macOS High Sierra, and you connect it to a brand new M1 Mac with a USB-C to Mini DisplayPort cable, Target Display Mode should still function normally, but otherwise, don't count on it to work.