This is pretty easy. No iMac other than some 27 inch models work in target display mode. Not all 27 support it, but some do. No other size iMac and no other Mac period support target display mode.
Even the 2009 models 27 inch iMac won’t target display mode - Can I use my early 2009 iMac as an external screen?