Maryland and Virginia officials were nearing the finish line on their budgets for next year when they came up against a jarring roadblock last week: the Gramm-Rudman-Hollings balanced budget legislation.

The legislation, signed into law by President Reagan on Thursday, mandates deep annual cuts in the federal deficit and requires a balanced budget by 1991. That is expected to mean sharp cuts in federal funds to states and local governments, and potentially dramatic shifts in the relationship among federal, state and local governments.

But no one was certain what monies will be cut and by how much, or who will be hurt and who will be helped. State and local officials are already fearing that vital programs may be slashed, and they note reports from government sources that the Office of Management and Budget has recommended halting all federal spending for the Metro subway system.

"We don't even have a rough order of magnitude at this point," said Dennis H. Parkinson, director of Maryland's Office of Budget Analysis, which is concerned about that the state may have to foot a costly bill for the savings and loan crisis there.

Thus there has been "absolutely zero attempt" to incorporate it into the budget that Gov. Harry Hughes will present to the General Assembly next month.

"The thing that's so troublesome about this is that we're trying to figure out what's happening," said Paul W. Timmreck, director of Virginia's budget office, which is in the final stages of its state budget preparations.

"From a management point of view, totally aside from the size of the cuts, it's going to cause us problems," said Virginia state Sen. Joseph V. Gartlan Jr. (D-Fairfax), a member of the state Senate Finance Committee. "It's a heck of a time for this to be landing on our desks."

Under the balanced budget legislation passed Wednesday by Congress, across-the-board cuts of $11.7 billion are expected March 1. In fiscal 1987, which begins Oct. 1, the measure sets a target of $144 billion, compared to the current prediction of a $194 billion deficit. Congress and the president thus must agree to $50 billion in cuts or the automatic, across-the-board reductions will be enacted.

A small number of programs are exempted from the automatic cuts. They include Social Security, veterans pensions and compensations, and some low-income assistance programs, including Medicaid, aid to families with dependent children, and food stamps. In addition, Medicare, veterans health and some other health programs can be cut only 1 percent in fiscal 1986 and 2 percent after that.

State and local officials said the federal money that has poured into state and local coffers during the last two decades for transportation, education, social services, housing, economic development, energy and the environment could be reduced to a trickle.

"This means an absolute decline in aid to state and local governments," said Maryland Del. Lucille Maurer (D-Montgomery), chairman of the Joint Committee on Federal Relations. "It will have a long-term impact on the relationship of all levels of government.

"After decades when the role of the federal government was increasing," the federal government has begun to pull back, a shift that started in 1981, said Maurer. "Now we can anticipate that there will be an acceleration of the shifting of responsibilities from the federal to the state government."

About $1.7 billion of the money in Maryland's $8 billion budget comes from federal revenues. Virginia, which has a $16.6 two-year budget, gets more than $2 billion from the federal government over a two-year period.

The District of Columbia reports that about one-third of its $2.7 billion budget comes from the federal government. About half of that comes from federal payments in lieu of taxes, which was not exempted from automatic cuts, according to Alphonse Hill, deputy mayor for finance.

The National Association of State Budget Officers estimated that the March 1 cut will mean that states will be hit with a 4.6 percent reduction in federal funds for programs not exempted from the across-the-board cut. Reductions in following years are expected to be more severe.

"I think we're going to be the worst off" of all the area governments, said Hill, because so much of the District's revenues come from the federal government and because there has been less economic growth in the District than in the suburbs.

Fairfax and Montgomery counties, both affluent areas, have been able to absorb the federal budget cuts of recent years with few problems. Officials in each county, in fact, repeatedly have urged Congress to reduce the deficit, saying lower interest rates on bonds that they float to raise money would help them the most.

Prince George's County, which has a charter amendment limiting its property tax rate, had more problems with federal budget cuts, and next year "there's going to be some gnashing of teeth" as the county tries to deal with the reduction of federal funds, said County Executive Parris N. Glendening. "But in the long run, we will probably be better off."

Said Glendening, "I guess the question is: Is the glass half full and filling up or half empty and emptying all the way? . . . I would trade the loss of federal monies in a moment if that's what is needed to keep inflation down."

Glendening explained that three years ago the county had to increase its annual budget by $60 million to keep pace with inflation, while it now must add less than $24 million. Interest rates on bonds that the county floats have dropped from about 8 percent to 5.7 percent, saving the county a significant amount of money. And the unemployment rate has dropped from 5 percent to 3.1 percent, meaning more tax revenues.

But Glendening said he is concerned about the potential loss of funds for some special projects, such as the Metro subway system.

Suburban Washington members of Congress said they are concerned that the regional economy could be hit hard if the budget cuts mean more layoffs for federal workers. The Federal Government Services Task Force, a caucus of members of Congress who represent federal workers, estimated that in fiscal 1987 there could be as many firings as there were in 1982 -- about 7,800 people. In addition, Gramm-Rudman-Hollings eliminates the cost-of-living pension raise that was scheduled for federal retirees this winter.

Virginia Republican Sens. Paul S. Trible Jr. and John W. Warner voted for the measure, saying that it would alleviate the deficit, but Maryland Sens. Charles McC. Mathias Jr., a Republican, and Paul S. Sarbanes, a Democrat, voted against it, warning that the bill could create economic chaos.

Virginia Republican Reps. Frank Wolf and Stan Parris and Maryland Democratic Reps. Michael D. Barnes and Steny H. Hoyer voted against Gramm-Rudman-Hollings. All four said they thought Congress was abdicating its responsibility by using automatic across-the-board cuts, and that members of Congress were elected to use their judgment on budget issues.

In November, Wolf and Parris voted for a Republican version of the automatic, balanced budget legislation, which originated in the Senate, and Barnes and Hoyer voted for a Democratic version passed by the House. The measure passed last week was a compromise between the two bills worked out between the House and Senate.

Hoyer said he has always been opposed to the idea but voted for the measure in November to support the Democratic leadership in its negotiations with Senate Republicans. He added that he knew he would have another chance to vote on the bill if a compromise was reached. Barnes said he did not recall the exact vote.

Wolf said he voted for the measure, hoping that it would be made "more workable and responsible" in conference. Parris said he preferred the original Republican version because it did not exempt Social Security and low-income programs. Parris said that all federal programs should be "on the table" if cuts are to be made.

Parris and Wolf complained that they received copies of the final proposal only hours before they were expected to vote on it. Like the state budget officers, many members of Congress say they are still uncertain exactly what Gramm-Rudman-Hollings will mean.

However, budget officials such as Maryland's Parkinson must finalize plans for how the state proposes to spend its money next year, without knowing what money it will have to spend. "This is absolutely the worst time they could have done this," he said.

Budget adjustments will have to be made by the Maryland legislature, or after the session by the governor, if information continues to be slim, said Parkinson.

Virginia's Timmreck said he expects his and other states to depend more heavily on revenue reserves or so-called rainy-day funds. This year the Virginia legislature set aside a $55 million revenue reserve for emergencies.

Timmreck said the money has not been used, but he expects that to change.

"I think what we've seen so far is just the tip of the iceberg," he said.