Count blocks command or LISP routine

Hello, is there a command or LISP routine that can count blocks within a user-defined area? And then write the numbers either at a specific location or directly into table cells that I specify? Or do you know of a forum where someone could advise me? Thank you very much.