Anti-aliasing ASCII art Background

ASCII art is the practice of creating images by using ASCII text to form shapes.

Aliasing is the effect created by the large "pixels" of ASCII art, which are the size of characters. The image becomes blocky and hard to see. Anti-aliasing removes this effect by creating a gradient and by softening the hard edges of the ASCII art.

The Challenge

Your challenge is to write the shortest program possible that will take a piece of ASCII art and will output a version that has been anti-aliased.

What sort of anti-aliasing?

All of the ASCII art will consist of two types of symbols: Spaces and non-whitespace. For each non-whitespace character, your program must determine whether it is in a position where it need to be anti-aliased. If it is, then you need to replace it with the correct character. If it isn't, then the character stays the same.

How do you know if a character needs to be anti-aliased? The answer depends on the characters that are immediately above, below, left, and right of the character (not the diagonals). Here is a chart of when anti-aliasing is required, where ? and x can stand for any non-whitespace character.

 x? -&gt;  d?
 ?      ? 


?x  -&gt; ?b 
 ?      ? 

 ?      ? 
?x  -&gt; ?F 


 ?      ? 
 x? -&gt;  Y?



 x  -&gt;  ;   Note: This character has been changed from _ to ;
 ?      ? 

 ?      ? 
 x  -&gt;  V 



?x  -&gt; ?&gt; 



 x? -&gt;  &lt;?



 x  -&gt;  @ 


Input (and example pre-anti-aliasing ASCII art)

First, there will be two lines of input (to STDIN), a number H followed by a number W. There will then be H lines of exactly W characters each (excluding the newline). These following lines will be the ASCII art that needs to be anti-aliased. Here is an example input (not beautiful, but a test):

7
9
  888888 
 888888  
999 98  7
 666666  
  666666 
   6666  
    6    


Output (and example anti-aliased art)

Your program should output to STDOUT the ASCII art (of the same dimensions), which has been anti-aliased. Here is the output for the above input. Notice how the the border characters are treated as bordering whitespace.

  d8888&gt; 
 d8888F  
&lt;99 98  @
 Y6666b  
  Y6666&gt; 
   Y66F  
    V    


This might not look that good (due to the space between lines in the code block), it looks better with larger ASCII art, and the quality depends on the exact font used.

Another Example

Input

12
18
   xx  xxx  xxx   
  xxxx  xxx  xxx  
 xxxxxx  xxx  xxx 
xxx  xxx  xxx  xxx
xxxx xxx  xxx  xxx
 xxxxxx  xxx  xxx 
  xxxx  xxx  xxx  
x  xx  xxx  xxx  x
xx    xxx  xxx  xx
xxx  xxx  xxx  xxx
xxxx  xxx  xxx  xx
xxxxx  xxx  xxx  x


Output

   db  &lt;xb  &lt;xb   
  dxxb  Yxb  Yxb  
 dxxxxb  Yxb  Yxb 
dxx  xxb  xxb  xxb
Yxxb xxF  xxF  xxF
 YxxxxF  dxF  dxF 
  YxxF  dxF  dxF  
;  YF  dxF  dxF  ;
xb    dxF  dxF  dx
xxb  &lt;xF  &lt;xF  &lt;xx
xxxb  Yxb  Yxb  Yx
Yxxx&gt;  Yx&gt;  Yx&gt;  V


Rules, Restrictions, and Notes

Your program should be written in printable ASCII characters only, so that we can make art out of the programs. Other than that, the standard code-golf rules apply.
Ruby, 180 168 characters
gets
w=1+gets.to_i
f=*(readlines*&quot;&quot;).chars
f.zip(f[1..-1]+s=[&quot; &quot;],s+f,s*w+f,f[w..-1]+s*w){|a,*b|$&gt;&lt;&lt;&quot;@;V#{a}&gt;bF#{a}&lt;dY#{a*5}&quot;[a&gt;&quot; &quot;?(b.map{|y|y&gt;&quot; &quot;?1:0}*&quot;&quot;).to_i(2):3]}

Another Ruby implementation which takes a zip approach. You can see the second example running online.
Edit: Using readlines saves 12 characters.