read H W
for ((i=0;$i<$H;i++));do
  read tmps
  for ((j=0;$j<$W;j++));do
    eval S_${i}_${j}=`echo $tmps|cut -c $(($j+1))`
  done
done

for ((i=0;$i<$H;i++));do
  for ((j=0;$j<$W;j++));do
    eval trout=\$S_${i}_${j}
    if [ "$trout" = "." ];then
      cnt=0
      for k in `seq $(($i-1)) $(($i+1))`;do
        if [ -1 -lt $k ] && [ $k -lt $H ];then
          for l in `seq $(($j-1)) $(($j+1))`;do
            eval trout=\$S_${k}_${l}
            if [ -1 -lt $l ] && [ $l -lt $W  ] && [ "$trout" = "#" ];then
              cnt=$(($cnt+1))
            fi
          done
        fi
      done
      eval S_${i}_${j}=\"\$cnt\"
    fi
  done
done

for ((i=0;$i<$H;i++));do
  tmp=""
  for ((j=0;$j<$W;j++));do
    eval tmp=\$tmp\$S_${i}_${j}
  done
  echo $tmp
done
