Morocco has unveiled details of an "exceptional operation" to give official papers to some of its 25,000-40,000 illegal immigrants from sub-Saharan Africa, many of whom hope to reach Europe.
The scheme, unveiled on Monday, is part of a new migration policy which the north African kingdom promised back in September in the face of growing international criticism.
"The exceptional operation to regularise the situation of foreigners residing illegally in Morocco" will be rolled out throughout next year, Interior Minister Mohamed Hassad and Migration Minister Anis Birou told a press conference in Sale, near Rabat.
Under the new scheme some 850 immigrants recognised as bona fide asylum-seekers by the UN's refugee agency (UNHCR) will automatically be given legal residency rights, according to Moroccan officials.
A list of criteria will be drawn up for others hoping to be considered for the same treatment. The key tests for legalised residency, taking into account the number of years spent in Morocco, the legal status of a spouse or parents, employment record and medical history.
The "avant garde" programme is in response to the recent waves of illegal immigrants, said Hassad.
He added that between 25,000 and 40,000 clandestine immigrants from sub-Saharan Africa were currently in Morocco.
Human rights groups say the migrants are brought to Morocco from other African countries by traffickers and camp in the wild waiting for a chance to cross into the Spanish territories of Melilla and Ceuta, on the north African coast.
These are the European Union's only land borders with Africa.
Others try to reach Spanish soil -- or Italy or Malta -- by sailing across the Mediterranean in makeshift vessels.
Melilla, which has 80,000 inhabitants, is a key crossroads for migrants who try to slip through to Europe from neighbouring Morocco.
In a bid to ward off regular charges by hundreds of migrants, Spain announced last week it had begun installing barbed wire and "anti-climbing" mesh at points along the 11-kilometre (seven-mile) border.
Morocco's planned measures "arise from a strong wish expressed by the king," Hassad said.
The new plan was first announced in September after Morocco's National Human Rights Council (CNDH) issued a report calling for "a radically new asylum and immigration policy," to which King Mohamed VI responded by admitting "legitimate concerns".
Those concerns had been rising amid reports of racist violence.
Morocco is the closest African country to mainland Europe, separated by the Strait of Gibraltar and just 15 kilometres (nine miles) from Spain at its narrowest point, making it one of the key smuggling routes for illegal migrants crossing into Europe.