The Enterprise goes to investigate Planet Questionmark, a planet covered in massive cities thousands of miles across. It transpires that the people there, a century and a half ago, had built machines to handle all labor for them, including agriculture and, eventually, city-building. For years, now, the city-building machines have been out of control, building cities without end, and after the agricultural machines broke down, the people there were in dire straits, having forgotten how to repair the machines or grow their own food. The city-building machines are too resilient for them to destroy, and they will soon cover the whole planet in cities--and no people will survive to live in them.
Spock discovers that the metal of which the machines are constructed is destroyed by amidogen (NH2), and they destroy the machines and the factory that constructed them, then leave the people of the planet to rebuild. Kirk informs Spock that Starfleet Command is happy with their report, to which Spock replies: "Naturally, Captain! We preserved a civilization which would otherwise have perished! It is the only logical reaction!"