import sys\x0d M,t,e,r='*',sys.stdin.read().split(),enumerate,range\x0d for y,l in e(t):\x0d o = ''\x0d for x,v in e(l):\x0d if v==M:\x0d o += M\x0d else:\x0d s = 0\x0d for j in r(y - 1, y + 2):\x0d for i in r(x - 1, x + 2):\x0d if j >= 0 and i >= 0:\x0d try:\x0d if t[j][i] == M:\x0d s += 1\x0d except:\x0d pass\x0d o += str(s)\x0d t[y]=o\x0d print '\n'.join(t)
Note that non-ascii characters in the above source code will be escaped (such as \x9f).