A contractor who was trapped yesterday afternoon when mud, dirt and rock cascaded into a trench his firm was digging in Potomac was freed early today after a painstaking 8 1/2-hour rescue effort.

The contractor, Richard Mowl, of Boonsboro, Md., was reported to be in good condition after being pulled from a trench in the 9700 block of Conestoga Way about 12:40 a.m. Before being taken to Suburban Hospital, he shook hands with paramedics and appeared to be lucid.

Mowl had been working on a sewer trench for a house when a wall of the excavation fell in on him about 4 p.m. yesterday, burying him up to his shoulders.

Members of a cave-in unit from Fairfax County spearheaded the rescue effort. Digging, described as "extremely difficult" by Montgomery County Fire Department spokesman Ray Mulhall was hampered by the weight of mud and rock and by precautions taken to avoid a further cave-in.

Working in shifts of about 20 minutes, rescuers installed shoring around Mowl and scooped out dirt with special miniature shovels.

Rain was heavy before the cave-in and a pump was used last night to expel water draining into the ditch, described as six to 10 feet deep.

A worker for Mowl's firm said that Mowl was in the trench, clearing dirt from where a pipe connection was to be made.

"We were watching to make sure nothing would happen," said the worker, Michael Hough. "We really didn't think it could cave in because it's such hard dirt. But then we saw it just tumble over."