There's quite a few ways to do this... If you just want a count of the overlap, you should use 3dABoverlap. You could try something like this (bash syntax):
for aMask in mask*+tlrc.HEAD
do
3dABoverlap -no_automask ${aMask} Reference+tlrc
done
Once you have that working, you can use the -quiet option and pipe the output to a file.