How to remove interior faces while keeping exterior faces untouched?

'F::3 ', // 1
With row one it looks like a corridor. How would you read original map array and then convert it to the following below converted array through a function? Example:

Original map

'   xxxxxx   x   ', // 1
'    x           ', // 2
'    xxxxxx      '  // 3	

Converted map

'   L*...J   U   ', // 1
'    H           ', // 2
'    L::::3      '  // 3