Problem:
 Score : 200 points    Problem Statement You are given an H × W grid. The squares in the grid are described by H strings, S_1,...,S_H . The j -th character in the string S_i corresponds to the square at the i -th row from the top and j -th column from the left (1 \leq i \leq H,1 \leq j \leq W) .  . stands for an empty square, and # stands for a square containing a bomb.  Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square. (Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.) He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  Print the strings after the process.      Constraints  1 \leq H,W \leq 50  S_i is a string of length W consisting of # and . .         Input Input is given from Standard Input in the following format:  H  W  S_1  :  S_H       Output Print the H strings after the process. The i -th line should contain a string T_i of length W , where the j -th character in T_i corresponds to the square at the i -th row from the top and j -th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W) .        Sample Input 1 3 5
.....
.#.#.
.....      Sample Output 1 11211
1#2#1
11211  For example, let us observe the empty square at the first row from the top and first column from the left. There is one bomb square adjacent to this empty square: the square at the second row and second column. Thus, the . corresponding to this empty square is replaced with 1 .       Sample Input 2 3 5
#####
#####
#####      Sample Output 2 #####
#####
#####  It is possible that there is no empty square.       Sample Input 3 6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...      Sample Output 3 #####3
#8#7##
####5#
4#65#2
#5##21
#4#310

Solution (Python):

import sys
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(str(input()))
arr = [[]*w]*h

for i in range(h):
    arr[i] = list(S[i])

if h == 1 and w == 1:
    if arr[0][0] == '
        print('
    else:
        print(0)
    sys.exit()
elif h == 1:
    for i in range(w):
        c = 0
        if arr[0][i] == '
            print('
        elif i == 0:
            if arr[0][1] == '
                c += 1
            print(c,sep = '', end = '')
        elif i == w-1:
            if arr[0][-2] == '
                c += 1
            print(c,sep = '', end = '')
        else:
            if arr[0][i-1] == '
                c += 1
            if arr[0][i+1] == '
                c += 1
            print(c,sep = '',end = '')
    print('')
    sys.exit()
elif w == 1:
    for i in range(h):
        c = 0
        if arr[i][0] == '
            print('
        elif i == 0:
            if arr[1][0] == '
                c += 1
            print(c)
        elif i == h-1:
            if arr[-2][0] == '
                c += 1
            print(c)
        else:
            if arr[i-1][0] == '
                c += 1
            if arr[i+1][0] == '
                c += 1
            print(c)
    sys.exit()
    
for i in range(h):
    for j in range(w):
        if arr[i][j] == '
            print('
        else:
            if i == 0 and j == 0:
                c = 0
                if arr[0][1] == '
                    c += 1
                if arr[1][0] == '
                    c += 1
                if arr[1][1] == '
                    c += 1
                print(c,sep = '', end = '')
            elif i == 0 and j == w-1:
                c = 0
                if arr[0][-2] == '
                    c += 1
                if arr[1][-1] == '
                    c += 1
                if arr[1][-2] == '
                    c += 1
                print(c,sep = '', end = '')
            elif i == 0:
                c = 0
                if arr[i][j-1] == '
                    c+= 1
                if arr[i][j+1] == '
                    c +=1
                if arr[i+1][j-1] == '
                    c += 1
                if arr[i+1][j] == '
                    c += 1
                if arr[i+1][j+1] == '
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1 and j == 0:
                c = 0
                if arr[-1][1] == '
                    c += 1
                if arr[-2][0] == '
                    c += 1
                if arr[-2][1] == '
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1 and j == w-1:
                c = 0
                if arr[-1][-2] == '
                    c += 1
                if arr[-2][-1] == '
                    c += 1
                if arr[-2][-2] == '
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1:
                c = 0
                if arr[i][j-1] == '
                    c+= 1
                if arr[i][j+1] == '
                    c +=1
                if arr[i-1][j-1] == '
                    c += 1
                if arr[i-1][j] == '
                    c += 1
                if arr[i-1][j+1] == '
                    c += 1
                print(c,sep = '', end = '')
            elif j == 0:
                c = 0
                if arr[i-1][j] == '
                    c += 1
                if arr[i+1][j] == '
                    c += 1
                if arr[i][j+1] == '
                    c += 1
                if arr[i-1][j+1] == '
                    c += 1
                if arr[i+1][j+1] == '
                    c += 1
                print(c,sep = '', end = '')
            elif j == w-1:
                c = 0
                if arr[i-1][j] == '
                    c += 1
                if arr[i+1][j] == '
                    c += 1
                if arr[i][j-1] == '
                    c += 1
                if arr[i-1][j-1] == '
                    c += 1
                if arr[i+1][j-1] == '
                    c += 1
                print(c,sep = '', end = '')
            else:
                c = 0
                if arr[i-1][j-1] == '
                    c += 1
                if arr[i-1][j] == '
                    c += 1
                if arr[i-1][j+1] == '
                    c += 1
                if arr[i][j-1] == '
                    c += 1
                if arr[i][j+1] == '
                    c += 1
                if arr[i+1][j-1] == '
                    c += 1
                if arr[i+1][j] == '
                    c += 1
                if arr[i+1][j+1] == '
                    c += 1
                print(c,sep = '', end = '')
    print('')

Solution (Python):
import math,heapq,collections,sys,numpy
sys.setrecursionlimit(10**7)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

H,W = LI()
LS = []
LS.append("@"*(W+2))
for _ in range(H):
    LS.append("@" + S() + "@")
LS.append("@"*(W+2))

def count(i,j):
    c = 0
    for k in range(-1,2):
        for l in range(-1,2):
            if LS[i+k][j+l] == "
                c +=1
    return c

for i in range(1,H+1):
    for j in range(1,W+1):
        if LS[i][j] == ".":
            LS[i] = LS[i][0:j] + str(count(i,j)) +  LS[i][j+1:W+2]

for i in range(1,H+1):
    print(LS[i][1:W+1])

Solution (Python):
H, W = list(map(int,input().split()))
l=[]
for i in range(H):
    l.append(list(input()))
ans=[[0 for i in range(W)] for j in range(H)]
for i in range(H):
    for j in range(W):
        for dx, dy in [(1,0), (0, 1), (0 , -1 ), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]:
            try:
                if j+dx < 0 or i+dy< 0:
                    continue
                if l[i+dy][j+dx]=='
                    ans[i][j] += 1
            except:
                pass
for i in range(H):
    for j in range(W):
        if l[i][j]=='
            ans[i][j] = '
for i in range(H):
    for j in range(W):
        print(ans[i][j],end='')
    print()

Solution (Python):
h, w = [int(i) for i in input().split()]
a = []
a.append(['.'] * (w + 2))
for j in range(h):
    a.append(['.'] + list(input()) + ['.'])
a.append(['.'] * (w + 2))

for j in range(1, h + 1):
    for i in range(1, w + 1):
        if a[j][i] == '.':
            n = [
                a[j - 1][i - 1],
                a[j - 1][i],
                a[j - 1][i + 1],
                a[j][i - 1],
                a[j][i + 1],
                a[j + 1][i - 1],
                a[j + 1][i],
                a[j + 1][i + 1],
            ].count('
            print(n, end='')
        else:
            print('
    print()

Solution (Python):
h,w = map(int, input().strip().split())
F = []
for _ in range(h):
  F.append(list(input()))
 
for i in range(h):
  for j in range(w):
    if F[i][j] == ".":
      u = max(i-1,0)
      d = min(i+2,h)
      l = max(j-1,0)
      r = min(j+2,w)
      n = 0
      for line in F[u:d]:
          n += line[l:r].count("
      F[i][j] = str(n)

for line in F:
    print("".join(line))

Solution (Python):

range = xrange
H, W = map(int, raw_input().split(' '))
S = [[c for c in raw_input()] for _ in range(H)]

answer = []
for h in range(H):
    row = []
    for w in range(W):
        if S[h][w] == '.':
            row.append([
                S[i][j]
                for i in range(max(h - 1, 0), min(H, h + 2))
                for j in range(max(w - 1, 0), min(W, w + 2))
            ].count('
        else:
            row.append(S[h][w])
    answer.append(row)

for r in answer:
    print u''.join(map(unicode, r))

Solution (Python):
h,w = map(int, raw_input().split())
c = '.'*(w+2)
mass = []
mass.append(c)
for _ in xrange(h):
    s = '.' + raw_input() + '.'
    mass.append(s)
mass.append(c)

ans = [['.' for _ in xrange(w)] for _ in xrange(h)]
for i in xrange(h):
    for j in xrange(w):
        if mass[i+1][j+1] == '
            ans[i][j] = '
        else:
            count = 0
            for a in [-1, 0, 1]:
                for b in [-1, 0, 1]:
                    if mass[i+1+a][j+1+b] == '
            ans[i][j] = str(count)

for x in ans:
    print "".join(x)

Solution (Python):



H,W = map(int,input().split(" "))

mat = []
bomb = []
for i in range(H):
	S = list(input())

	for val,j in zip(S,range(W)):
		if val == '
			bomb.append((i,j))
		else:
			S[j] = 0

	mat.append(S)


def rep (i,j,mat,H,W):
	for k in [i-1,i,i+1]:
		for l in [j-1,j,j+1]:

			if (k < 0) or (k > (H-1)) or (l < 0) or (l > (W-1)):
				continue

			if mat[k][l] == '
				continue
			else:
				mat[k][l] += 1

for t in bomb:
	rep(t[0],t[1],mat,H,W)

for i in range(H):
	for j in range(W):
		print(mat[i][j],end="")
	print("")

Solution (Python):
h,w=[int(i) for i in input().split()]
s=[input() for i in range(h)]
count=0

M = [["

for i in range(h):
  for j in range(w):
    count=0
    if s[i][j]==".":
      
      if j-1>=0:
           if s[i][j-1]=="
               count=count+1
      if j<w-1:
           if s[i][j+1]=="
               count=count+1            
      if i-1>=0:
           if s[i-1][j]=="
               count=count+1    
      if i<h-1:
           if s[i+1][j]=="
               count=count+1                
              
      if j-1>=0 and i-1>=0:
           if s[i-1][j-1]=="
               count=count+1
      if j<w-1 and i-1>=0:
           if s[i-1][j+1]=="
               count=count+1            
      if i<h-1 and j-1>=0:
           if s[i+1][j-1]=="
               count=count+1    
      if i<h-1 and j<w-1:
           if s[i+1][j+1]=="
               count=count+1                     
              
      M[i][j]=count

M_LIST=[]
for i in range(h):
    M_LIST.append("".join(map(str,M[i])))
    print(M_LIST[i])

Solution (Python):
a,b=[int(i) for i in input().split()]

x=list()
y=list()

for i in range(a):
    l=input()
    x.append(list(l))
    y.append(x[i][:b])

z=[["" for i in range(b)] for j in range(a)]
for i in range(a):
    for j in range(b):
        if y[i][j]=='
            z[i][j]='
        else:
            n=0
            if i+1<a:
                    if y[i+1][j]=="
                         n+=1
            if j+1<b:
                    if y[i][j+1]=="
                         n+=1
            if i+1<a and j+1<b:
                    if y[i+1][j+1]=="
                         n+=1
            if i-1>=0:
                    if y[i-1][j]=="
                         n+=1
            if j-1>=0:
                    if y[i][j-1]=="
                         n+=1
            if i-1>=0 and j-1>=0:
                    if y[i-1][j-1]=="
                         n+=1
            if i+1<a and j-1>=0:
                    if y[i+1][j-1]=="
                         n+=1
            if i-1>=0 and j+1<b:
                    if y[i-1][j+1]=="
                         n+=1
            z[i][j]=str(n)
        
    print("".join(z[i]))

Solution (Python):
def main():
    h,w=[int(i) for i in input().split()]
    field=[]
    for i in range(h):
        field.append(input())

    for i in range(h):
        new_line=[]
        for j in range(w):
            ans=0
            if field[i][j]=='
                new_line.append('
                continue
            if i-1 >=0 and field[i-1][j]=='
                ans+=1
            if i+1 < h  and field[i+1][j]=='
                ans+=1
            if j-1 >=0 and field[i][j-1]=='
                ans+=1
            if j+1 < w and field[i][j+1]=='
                ans+=1
            if i-1 >= 0 and j-1 >= 0 and field[i-1][j-1]=='
                ans+=1
            if i-1 >= 0 and j+1 < w and field[i-1][j+1]=='
                ans+=1
            if i+1 < h and j-1 >= 0 and field[i+1][j-1]=='
                ans+=1
            if i+1 < h and j+1 < w  and field[i+1][j+1]=='
                ans+=1
            new_line.append(str(ans))
        print(''.join(new_line))

if __name__=='__main__':
    main()

Solution (Python):


H,W=map(int,input().rstrip().split())
panel=[]
for i in range(H):
    panel.append([x for x in input().rstrip()])
    


def countBomb(y, x):
    cnt=0
    vec=[
        (0,-1),
        (1,-1),
        (1,0),
        (1,1),
        (0,1),
        (-1,1),
        (-1,0),
        (-1,-1)
    ]
    
    for tpl in vec:
        tmpX=x+int(tpl[0])
        tmpY=y+int(tpl[1])
        if(tmpX<0 or tmpY<0 or tmpX>=W or tmpY>=H):
            continue
        if(panel[tmpY][tmpX]=="
            cnt+=1
    return cnt
            

result=[]
for i in range(H):
    row=[]
    for j in range(W):
        puts="
        if(panel[i][j]=="."):
            puts=countBomb(i,j)
        row.append(str(puts))
    
    print("".join(row),flush=True)

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]

T = [['' for j in range(W)] for i in range(H)]

for i, Ti in enumerate(T):
    for j, Tij in enumerate(Ti):
        if S[i][j] == '
            T[i][j] = '
        else:
            num = 0
            for di in [-1, 0, 1]:
                for dj in [-1, 0, 1]:
                    if 0 <= i + di < H and 0 <= j + dj < W:
                        if S[i + di][j + dj] == '
                            num += 1
            T[i][j] = str(num)

print('\n'.join([''.join(T[i]) for i in range(H)]))

Solution (Python):
H,W=map(int,raw_input().split(' '))

m=['.']*((H+2)*(W+2))

for h in range(1,H+1):
    S=raw_input()
    i=0
    for w in range(1,W+1):
        m[h*(W+2)+w]=S[i]
        i+=1

r=[0]*(H*W)
i=0
for h in range(1,H+1):
    for w in range(1,W+1):
        p=h*(W+2)+w
        if m[p]=='
            r[i]='
        else:
            if m[p-(W+2)-1]=='
                r[i]+=1
            if m[p-(W+2)]=='
                r[i]+=1
            if m[p-(W+2)+1]=='
                r[i]+=1
            if m[p-1]=='
                r[i]+=1
            if m[p+1]=='
                r[i]+=1
            if m[p+(W+2)-1]=='
                r[i]+=1
            if m[p+(W+2)]=='
                r[i]+=1
            if m[p+(W+2)+1]=='
                r[i]+=1
        i+=1

r=map(str,r)
for h in range(H):
    print ''.join(r[h*W:h*W+W])

Solution (Python):
s = input().split(" ")
H = int(s[0])
W = int(s[1])
S = []
for i in range(H):
    S.append(input())
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, 1, 1, 1, 0, -1, -1, -1]
res = []
for i in range(H):
    res.append([])
    for j in range(W):
        res[i].append(0)
for i in range(H):
    for j in range(W):
        if S[i][j] == "
            continue
        cnt = 0
        for k in range(8):
            tx = i + dx[k]
            ty = j + dy[k]
            if 0 <= tx and tx < H and 0 <= ty and ty < W and S[tx][ty] == "
                cnt += 1
        res[i][j] = cnt
for i in range(H):
    for j in range(W):
        if S[i][j] == "
            print(S[i][j], end = '')
        else:
            print(res[i][j], end = '')
    print('')

Solution (Python):
H,W = map(int,input().split())
S = []
res = [[0 for i in range(W)]for i in range(H)]

for i in range(H):
    s = list(input())
    S.append(s)

for i in range(H):
    for j in range(W):
        if S[i][j] == '
            res[i][j] = '
            for x,y in [[j-1,i-1],[j,i-1],[j+1,i-1],[j-1,i],[j+1,i],[j-1,i+1],[j,i+1],[j+1,i+1]]:
                if 0 <= x and 0 <= y and x <= W-1 and y <= H-1:
                    if res[y][x] != '
                        res[y][x] += 1
for i in range(H):
    for j in range(W):
        res[i][j] = str(res[i][j])
for i in range(H):
    print("".join(res[i]))

Solution (Python):
def main():
    H, W = map(int, input().split())
    S = tuple(input() for _ in [0] * H)

    sq = ['0' * (W + 2)]
    apnd = sq.append
    for s in S:
        apnd('0' + s + '0')
    else:
        apnd('0' * (W + 2))

    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if sq[i][j] == '.':
                n = 0
                if sq[i-1][j-1] in '
                if sq[i-1][j] in '
                if sq[i-1][j+1] in '
                if sq[i][j-1] in '
                if sq[i][j+1] in '
                if sq[i+1][j-1] in '
                if sq[i+1][j] in '
                if sq[i+1][j+1] in '
                print(n, end='')
            else:
                print(sq[i][j], end='')
        print()

main()

Solution (Python):
import sys

input = sys.stdin.readline
print = sys.stdout.write

def checkmine(matrix,r,c):
    if matrix[r][c] == "
        return "

    dr = [-1,-1,-1, 0,0, 1,1,1]
    dc = [-1, 0, 1,-1,1,-1,0,1]
    
    numberOfBombs = 0
    for i in range(8):
        
        try:
            if matrix[r+dr[i]][c+dc[i]] == "
                numberOfBombs += 1
        except:
            pass
        
    return numberOfBombs

N,M = map(int,input().split())
l = []
for i in range(N):
    l.append(input())
    
for i in range(N):
    for j in range(M):
        print(str(checkmine(l,i,j)))
    print("\n")

Solution (Python):
import numpy as np

H, W = (list(map(int, input().split())))
S = [[_ for _ in input()] for i in range(H)]
for i in range(H):
  for j in range(W):
    if(S[i][j] == "."):
      count = 0
      if(i - 1 in range(H)):
        if(j - 1 in range(W)):
          if(S[i-1][j-1] == "
            count += 1
        if(j in range(W)):
          if(S[i- 1][j] == "
            count += 1
        if(j + 1 in range(W)):
          if(S[i-1][j + 1] == "
            count += 1
      if(i in range(H)):
        if(j - 1 in range(W)):
          if(S[i][j-1] == "
            count += 1
        if(j in range(W)):
          if(S[i][j] == "
            count += 1
        if(j + 1 in range(W)):
          if(S[i][j + 1] == "
            count += 1
      if(i + 1 in range(H)):
        if(j - 1 in range(W)):
          if(S[i+1][j-1] == "
            count += 1
        if(j in range(W)):
          if(S[i+1][j] == "
            count += 1
        if(j + 1 in range(W)):
          if(S[i+1][j + 1] == "
            count += 1
      S[i][j] = count
for i in range(H):
  out = ""
  for char in S[i]:
    out += str(char)
  print(out)

Solution (Python):
h, w = [int(i) for i in input().split()]

m = [None for i in range(h)]

for i in range(h):
    m[i] = str(input())

for i in range(h):
    for j in range(w):
        if m[i][j] == ".":
            a = 0
            for ii in range(-1, 2, 1):
                for jj in range(-1, 2, 1):
                    if i+ii >= 0 and j+jj >= 0 and i+ii < h and j+jj <w:
                        a += (m[i+ii][j+jj] == "
            a = str(a)
            m[i] = m[i][:j] + a + m[i][j+1:]

for i in range(h):
    print(m[i])

Solution (Python):

h,w = list(map(int, input().split()))
s = []
for i in range(h):
    s.append(input())
    
ss = [[0]*(w +2) for i in range(h+2)]


for i in range(h):
    for j in range(w):
        if s[i][j]=="
            ss[i+1][j+1]+=9
            ss[i][j]+=1
            ss[i][j+1]+=1
            ss[i][j+2]+=1
            ss[i+1][j]+=1
            ss[i+1][j+2]+=1
            ss[i+2][j]+=1
            ss[i+2][j+1]+=1
            ss[i+2][j+2]+=1

for i in range(h):
    l = ss[i+1][1:w+1]
    
    l = [str(l[i]) if l[i]<9 else "
    print("".join(l))

Solution (Python):
def get_bomb_num(i, j):
    cnt = 0
    for p in range(-1,2):
        for s in range(-1,2):
            l = i + p
            m = j + s
            if (p == 0 and s == 0) or (l < 0) or (l >= H) or (m < 0) or (m >= W):
                continue
            if b[l][m] == '
                cnt += 1
    return cnt

H,W = list(map(int, input().split()))
b =[]
for i in range(H):
    b.append(str(input()))

for i in range(H):
    for j in range(W):
        if b[i][j] == '
            print('
        else:
            print(get_bomb_num(i,j), end='')
    print('')

Solution (Python):
board = []
ans = []
H, W = list(map(int, input().split()))

def get_mine_number(j, i):
    mine_num = 0
    for deltaj in [-1, 0, 1]:
        for deltai in [-1, 0, 1]:
            if not -1 < j + deltaj < H or not -1 < i + deltai < W:
                continue
            if deltaj == 0 and deltai == 0:
                continue
            if board[j+deltaj][i+deltai] == "
                mine_num += 1
    return str(mine_num)
        
        
    

def main():
    for i in range(H):
        board.append(input())
    for jind, line in enumerate(board):
        ansline = ""
        for iind, point in enumerate(line):
            if point == ".":
                ansline += get_mine_number(jind, iind)
            else:
                ansline += point
        ans.append(ansline)
    for l in ans:
        print(l)
    
main()

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]

for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            left = False if w == 0 else True
            right = False if w == W - 1 else True
            up = False if h == 0 else True
            down = False if h == H - 1 else True

            count = 0
            count = count + 1 if left and up and S[h - 1][w - 1] == '
            count = count + 1 if up and S[h - 1][w] == '
            count = count + 1 if right and up and S[h - 1][w + 1] == '
            count = count + 1 if left and S[h][w - 1] == '
            count = count + 1 if right and S[h][w + 1] == '
            count = count + 1 if left and down and S[h + 1][w - 1] == '
            count = count + 1 if down and S[h + 1][w] == '
            count = count + 1 if right and down and S[h + 1][w + 1] == '

            print(str(count), end='')
        else:
            print("
    print()

Solution (Python):


h,w = map(int,input().split())
bomb = []
for _ in range(h):
    n = list(list(input()))
    bomb.append(n)

for y in range(h):
    for x in range(w):
        if bomb[y][x] == '.':
            counter = 0
            for yy in list([-1,0,1]):
                if y == 0 and yy == -1: continue
                if y == h-1 and yy == 1: continue
                for xx in list([-1,0,1]):
                    if x == 0 and xx == -1: continue
                    if x == w-1 and xx == 1: continue
                    try:
                        if bomb[y+yy][x+xx]=='
                    except IndexError:
                        counter += 0
            bomb[y][x] = counter

for y in range(h):
    string = ''
    for x in range(w): string += str(bomb[y][x])
    print(string)

Solution (Python):
i, j = [int(x) for x in raw_input().split()]
l = []
for x in xrange(i):
	l.append(list(raw_input()))
o = []
for x in xrange(i):
	for y in xrange(j):
		if l[x][y] == '
			o.append('
			continue
		
		temp = 0
		for d_x in xrange(-1, 2):
			for d_y in xrange(-1, 2):
				if x + d_x < 0 or x + d_x >= i or y + d_y < 0 or y + d_y >= j:
					continue
				try:
					if l[x + d_x][y + d_y] == '
						temp += 1
				except:
					continue
		s = 0
		o.append(str(temp))
	o.append('\n')
print ''.join(o[:-1])

Solution (Python):
inputs = input().split()
H = int(inputs[0])
W = int(inputs[1])

grid = []

grid.append('.'*W+'..')
for i in range(H):
    inputs = input().strip()
    inputs = '.' + inputs + '.'
    grid.append(inputs)
grid.append('.'*W+'..')


dx = [1,1,1,0,-1,-1,-1,0]
dy = [1,0,-1,-1,-1,0,1,1]

ans = [[0 for x in range(W)] for y in range(H)]

for row in range(len(grid)-2):
    for col in range(W):
        if grid[row+1][col+1] == '
            ans[row][col] = '
        for i in range(8):
            if grid[row+1 + dy[i]][col+1 + dx[i]] == '
                if ans[row][col] != '
                    ans[row][col] += 1

for row in ans:
    print(''.join(map(lambda x: str(x), row)))

Solution (Python):



h, w = [int(i) for i in input().split()]
all_map = []
for _ in range(h):
    all_map.append([i for i in input()])
for i in range(h):
    for j in range(w):
        if all_map[i][j] == ".":
            counter = 0
            for c_height in range(max(0, i -1), min(h, i + 1 + 1)):
                for c_width in range(max(0, j -1), min(w, j + 1 + 1)):
                    if all_map[c_height][c_width] == "
                        counter += 1
            all_map[i][j] = counter
for item in all_map:
    print("".join([str(i) for i in item]))

Solution (Python):
H, W = map(int, input().split())

Map = [['0']*W]*H

for i in range(H):
    Map[i] = list(input())
    
for i in range(H):
    for j in range(W):
        bomb = 0
        if Map[i][j] == '.':
            if i-1>=0 and  j-1>=0 and Map[i-1][j-1] == '
                bomb+=1
            if i-1>=0 and  Map[i-1][j] == '
                bomb+=1
            if i-1>=0 and  j+1<W and Map[i-1][j+1] == '
                bomb+=1
            if j+1<W and Map[i][j+1] == '
                bomb+=1
            if i+1<H and  j+1<W and Map[i+1][j+1] == '
                bomb+=1
            if i+1<H and Map[i+1][j] == '
                bomb+=1
            if i+1<H and  j-1>=0 and Map[i+1][j-1] == '
                bomb+=1
            if j-1>=0 and Map[i][j-1] == '
                bomb+=1
            Map[i][j] = str(bomb)
        
for i in range(H):
    print(''.join(Map[i]))

Solution (Python):
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())


def solve(h_, w_):
    count = 0
    for h in range(max(0, h_ - 1), min(h_ + 2, H)):
        for w in range(max(0, w_ - 1), min(w_ + 2, W)):
            if S[h][w] is '
                count += 1
    return count


for n in range(H):
    ans = []
    for i in range(W):
        if S[n][i] is '
            ans.append('
        else:
            ans.append(str(solve(n, i)))
    print(''.join(ans))

Solution (Python):
H, W = map(int, input().split())
f = []
for i in range(H):
    f.append(input())
for i in range(H):
    for j in range(W):
        if '
            print('
        else:
            sum = 0
            if i - 1 >= 0:
                if j - 1 >= 0:
                    if f[i-1][j-1] == '
                        sum = sum + 1
                if j >= 0:
                    if f[i-1][j] == '
                        sum = sum + 1
                if j + 1 < W:
                    if f[i-1][j+1] == '
                        sum = sum + 1
            if j - 1 >= 0:
                if f[i][j-1] == '
                    sum = sum + 1
            if f[i][j] == '
                sum = sum + 1
            if j + 1 < W:
                if f[i][j+1] == '
                    sum = sum + 1
            if i  + 1 < H:
                if j - 1 >= 0:
                    if f[i+1][j-1] == '
                        sum = sum + 1
                if j >= 0:
                    if f[i+1][j] == '
                        sum = sum + 1
                if j + 1 < W:
                    if f[i+1][j+1] == '
                        sum = sum + 1
            print(sum, end="")
    print()

Solution (Python):
h,w = input().split()
h = int(h)
w = int(w)
s_list = []
for _ in range(h):
    s = input()
    s_chars =[]
    for char in s:
        s_chars.append(char)
    s_list.append(s_chars)

import numpy as np
ar = np.array(s_list)
ar = np.insert(ar,0,[["."]*w],axis=0)
ar = np.insert(ar,h+1,[["."]*w],axis=0)
ar = np.insert(ar,0,[["."]*(h+2)],axis=1)
ar = np.insert(ar,w+1,[["."]*(h+2)],axis=1)
answer_list = []
for i in range(1,h+1):
    syoukei = ""
    for j in range(1,w+1):
        if ar[i,j] == ".":
            count = 0
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if ar[k,l] == "
                        count += 1
            syoukei += str(count)
        else:
            syoukei += "
    answer_list.append(syoukei)

for ans in answer_list:
    print(ans)

Solution (Python):
import sys
H,W = input().split()
H,W = int(H),int(W)
board = [["0" for w in range(W+2)] for h in range(H+2)]

for h in range(H):
    tmp = list(input())
    for w in range(W):
        board[h+1][w+1] = tmp[w]

ansboard = [["." for w in range(W+2)] for h in range(H+2)]

for h in range(H+2):
    for w in range(W+2):
        if board[h][w] == 0:
            pass
        elif board[h][w] == ".":
            counter = 0
            for j in range(3):
                for i in range(3):
                    if board[h-(j-1)][w-(i-1)] == "
                        counter += 1
            ansboard[h][w] = counter
        else:
            ansboard[h][w] = "

for h in range(H):
    for w in range(W):
        sys.stdout.write(str(ansboard[h+1][w+1]))
    sys.stdout.write("\n")

Solution (Python):
def solve():
    H, W = list(map(int, input().split(" ")))
    field = []
    for _ in range(H):
        field.append(list(input()))
    answer = [[None] * W for _ in range(H)]

    for i in range(H):
        for j in range(W):
            if field[i][j] == ".":
                cnt = 0
                for x in [-1, 0, 1]:
                    for y in [-1, 0, 1]:
                        dx = i + x
                        dy = j + y
                        if dx >= 0 and dx < H and dy >=0 and dy < W and field[dx][dy] == "
                            cnt += 1
                    answer[i][j] = cnt
            else:
                answer[i][j] = "


    for i, col in enumerate(answer):
        answer[i] = [str(x) for x in col]

    for col in answer:
        print("".join(col))
        
        
solve()

Solution (Python):
h, w = map(int, input().split())
s = ['0'*(w+2)]

for i in range(1,h+1) :
    tmp = input().replace('.', '0')
    tmp = tmp.replace('
    s.append('0' + tmp + '0')
s.append('0'*(w+2))

ans = [''*w for _ in range(h)]
for i in range(h) :
    for j in range(w) :
        if s[i+1][j+1] == '1' :
            ans[i] += '
        else :
            ans[i] += str(int(s[i][j])  +int(s[i+1][j])  +int(s[i+2][j])+
                          int(s[i][j+1])+                 int(s[i+2][j+1])+
                          int(s[i][j+2])+int(s[i+1][j+2])+int(s[i+2][j+2]))

for ss in ans :
    print(ss)