main=mapM_ putStrLn.solve.lines=<<getContents
solve(hw:s)=zipWith(\i t->zipWith(g i)[0..]t)[0..]s where
  [h,w]=map read.words$hw
  f i j=if i<0||h<=i||j<0||w<=j||s!!i!!j=='.'then 0 else 1
  g x y c=if c=='#' then c else head$show$sum[f i j|i<-[x-1..x+1],j<-[y-1..y+1],i/=x||j/=y]