You can connect as many as you like - you can even daisy chain them off of each other if you really had to.....
daisy chain - 5 thunderbolt devices per Thunderbolt/USB4 port of your M1 Mac.
6 devices per Thunderbolt port of an Intel Mac.
Actually, that's only for the length of the chain. Now that there exists Thunderbolt 4 hubs or docks with 3 downstream Thunderbolt ports, the width can be expanded by x3 for each level.
With no hubs, you can connect 5 Thunderbolt devices ((3^0)x(5-0))
With 1 hub, you can connect 12 Thunderbolt devices ((3^1)x(5-1))
With 2 levels of hubs (1+3 hubs) you can connect 27 Thunderbolt devices ((3^2)x(5-2))
With 3 levels of hubs (1+3+9 hubs) you can connect 54 Thunderbolt devices ((3^3)x(5-3))
With 4 levels of hubs (1+3+9+27 hubs) you can connect 81 Thunderbolt devices ((3^4)x(5-4))
That's theoretical. I haven't seen anyone test if chaining hubs together actually works.
Plus, there's a limited amount of PCI resources. For example, there's only 255 PCI buses. Each Thunderbolt device takes a handful of PCI buses. A hub takes 4 so there's no way you can connect 40 of them (unless you have some code to remove the unused bus of the hub).
An M1 Mac allows at least 128 PCI buses per Thunderbolt port (each port is a separate PCI domain). Intel Macs allow fewer because all the Thunderbolt ports are on the same PCI domain and there are other PCI devices on the same domain (max buses per port = (255 - buses for built-in devices)÷number of Thunderbolt ports)
In any case, no matter how many docks you chain together, only a max of two displays can be supported from a single Thunderbolt port.