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



Sample input:
3 5
.....
.#.#.
.....


Sample output:
11211
1#2#1
11211

Submission status: Runtime Error

Solution code:
from sys import stdin
h, w = [int(x) for x in stdin.readline().rstrip().split()]
matrix = [stdin.readline().rstrip() for _ in range(h)]
for i in range(h):
    matrix[i] = [matrix[i][x:x+1] for x in range(len(matrix[i]))]
    matrix[i] = [0 if x == '.' else x for x in matrix[i]]
for i in range(h):
    for j in range(w):
        if matrix[i][j] == '#':
            for n in range(-1, 2):
                for m in range(-1, 2):
                    i_n = i + n
                    j_m = j + m
                    if matrix[i_n][j_m] != '#' and i_n >= 0 and j_m >= 0:
                        matrix[i_n][j_m] += 1
for i in range(h):
  for j in range(w):
  	print('{}'.format(matrix[i][j]), end = '')
  print('')

Submission status: Runtime Error

Solution code:
from sys import stdin
h, w = [int(x) for x in stdin.readline().rstrip().split()]
matrix = [stdin.readline().rstrip() for _ in range(h)]
for i in range(h):
    matrix[i] = [matrix[i][x:x+1] for x in range(len(matrix[i]))]
    matrix[i] = [0 if x == '.' else x for x in matrix[i]]
for i in range(h):
    for j in range(w):
        if matrix[i][j] == '#':
            for n in range(-1, 2):
                for m in range(-1, 2):
                    i_n = i + n
                    j_m = j + m
                    if matrix[i_n][j_m] != '#' and i_n >= 0 and j_m >= 0:
                        matrix[i_n][j_m] += 1

print(*matrix, sep = '\n')

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h):
    s.append(list(input()))
t = []
for i in range(h):
    temp = []
    for j in range(w):
        count = 0
        if s[i][j] != '#':
            if i == 0:
                if j == 0:
                    if s[i][j + 1] == '#':
                        count += 1
                    if s[i + 1][j] == '#':
                        count += 1
                    if s[i + 1][j + 1] == '#':
                        count += 1

                elif j == w - 1:
                    if s[i][j - 1] == '#':
                        count += 1
                    if s[i + 1][j - 1] == '#':
                        count += 1
                    if s[i + 1][j] == '#':
                        count += 1

                else:
                    if s[i][j - 1] == '#':
                        count += 1
                    if s[i][j + 1] == '#':
                        count += 1
                    if s[i + 1][j - 1] == '#':
                        count += 1
                    if s[i + 1][j] == '#':
                        count += 1
                    if s[i + 1][j + 1] == '#':
                        count += 1

            elif i == h - 1:
                if j == 0:
                    if s[i - 1][j] == '#':
                        count += 1
                    if s[i - 1][j + 1] == '#':
                        count += 1
                    if s[i][j + 1] == '#':
                        count += 1

                elif j == w - 1:
                    if s[i - 1][j - 1] == '#':
                        count += 1
                    if s[i - 1][j] == '#':
                        count += 1
                    if s[i][j - 1] == '#':
                        count += 1

                else:
                    if s[i - 1][j - 1] == '#':
                        count += 1
                    if s[i - 1][j] == '#':
                        count += 1
                    if s[i - 1][j + 1] == '#':
                        count += 1
                    if s[i][j - 1] == '#':
                        count += 1
                    if s[i][j + 1] == '#':
                        count += 1

            else:
                if j == 0:
                    if s[i - 1][j] == '#':
                        count += 1
                    if s[i - 1][j + 1] == '#':
                        count += 1
                    if s[i][j + 1] == '#':
                        count += 1
                    if s[i + 1][j] == '#':
                        count += 1
                    if s[i + 1][j + 1] == '#':
                        count += 1

                elif j == w - 1:
                    if s[i - 1][j - 1] == '#':
                        count += 1
                    if s[i - 1][j] == '#':
                        count += 1
                    if s[i][j - 1] == '#':
                        count += 1
                    if s[i + 1][j - 1] == '#':
                        count += 1
                    if s[i + 1][j] == '#':
                        count += 1

                else:
                    if s[i - 1][j - 1] == '#':
                        count += 1
                    if s[i - 1][j] == '#':
                        count += 1
                    if s[i - 1][j + 1] == '#':
                        count += 1
                    if s[i][j - 1] == '#':
                        count += 1
                    if s[i][j + 1] == '#':
                        count += 1
                    if s[i + 1][j - 1] == '#':
                        count += 1
                    if s[i + 1][j] == '#':
                        count += 1
                    if s[i + 1][j + 1] == '#':
                        count += 1

            temp.append(str(count))
        else:
            temp.append('#')
    t.append(temp)
for i in range(h):
    row = ''
    for x in t[i]:
        row += x
    print(row)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
s =[input() for i in range(h)]


for i in range(h):
  for j in range(w):
    count = 0 
    if s[i][j]==".":
      for k in [[0,1],[1,0],[-1,0],[0,-1],[1,1],[-1,-1],[1,-1],[-1,1]]:
        if 0<=k[0]+i<h and  0<=k[1]+j<w:
          if s[k[0]+i][k[1]+j]=="#":
            count = count +1
      s[i] = s[i][0:j]+str(count)+s[i][j+1:w]
      
for i in range(h):
  for j in range(w):
    print(s[i][j], end="")
  h,w = map(int,input().split())
s =[input() for i in range(h)]


for i in range(h):
  for j in range(w):
    count = 0 
    if s[i][j]==".":
      for k in [[0,1],[1,0],[-1,0],[0,-1],[1,1],[-1,-1],[1,-1],[-1,1]]:
        if 0<=k[0]+i<h and  0<=k[1]+j<w:
          if s[k[0]+i][k[1]+j]=="#":
            count = count +1
      s[i] = s[i][0:j]+str(count)+s[i][j+1:w]
      
for i in range(h):
  for j in range(w):
    print(s[i][j], end="")
  h,w = map(int,input().split())
s =[input() for i in range(h)]


for i in range(h):
  for j in range(w):
    count = 0 
    if s[i][j]==".":
      for k in [[0,1],[1,0],[-1,0],[0,-1],[1,1],[-1,-1],[1,-1],[-1,1]]:
        if 0<=k[0]+i<h and  0<=k[1]+j<w:
          if s[k[0]+i][k[1]+j]=="#":
            count = count +1
      s[i] = s[i][0:j]+str(count)+s[i][j+1:w]
      
for i in range(h):
  for j in range(w):
    print(s[i][j], end="")
  print()

Submission status: Runtime Error

Solution code:
[h,w] = [int(i) for i in input().split()]
s = []
for i in range(h):
    s.append(input())
ans1 = []
if h != 1 and w != 1:
    for i in range(h):
        ans = []
  #上下のエラーを排除
        if i != 0 and i != h-1:
            for j in range(w):
            #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j+1]+s[i-1][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i-1][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        elif i == 0:
            for j in range(w):
                #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        else:
            for j in range(w):
        #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j+1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i-1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        ans1.append(ans)

elif h != 1 and w == 1:
    #hは非1でwが1
    for i in range(h):
        ans = []
        if i !=0 and i != h-1:
            if s[i][0] == ".":
                ans.append((s[i-1][0]+s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        elif i == 0:
            if s[i][0] == ".":
                ans.append((s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        else:
            if s[i][0] == ".":
                ans.append((s[i-1][0]).count("#"))
            else:
                ans.append(s[i][0])
        ans1.append(ans)
elif h == 1 and w != 1:
    #hは1でwが非1
    for j in range(w):
        ans = []
        if j != 0 and j!= w-1:
            if s[0][j] == ".":
                ans.append((s[0][j-1]+s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        elif j == 0:
            if s[0][j] == ".":
                ans.append((s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        else:
            if s[0][j] == ".":
                ans.append((s[0][j-1]).count("#"))
            else:
                ans.append(s[0][j])
        ans1.append(ans)
else:
    ans = []
    if s[0][0] == ".":
        ans.append(0)
    else:
        ans.append(s[0][0])
    ans1.append(ans)

for i in range(h):
    for j in range(w):
        print(ans1[i][j],end = "")
    print("")

Submission status: Runtime Error

Solution code:
[h,w] = [int(i) for i in input().split()]
s = []
for i in range(h):
    s.append(input())
ans1 = []

for i in range(h):
    ans = []
  #上下のエラーを排除
    if i != 0 and i != h-1:
        for j in range(w):
            #左右のエラーを排除
            if j != 0 and j != w-1:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            elif j == 0:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i+1][j]+s[i][j+1]+s[i-1][j+1]+s[i+1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            else:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i-1][j-1]+s[i+1][j-1]).count("#"))
                else:
                    ans.append(s[i][j])
    elif i == 0:
        for j in range(w):
            #左右のエラーを排除
            if j != 0 and j != w-1:
                if s[i][j] == ".":
                    ans.append((s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            elif j == 0:
                if s[i][j] == ".":
                    ans.append((s[i+1][j]+s[i][j+1]+s[i+1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            else:
                if s[i][j] == ".":
                    ans.append((s[i+1][j]+s[i][j-1]+s[i+1][j-1]).count("#"))
                else:
                    ans.append(s[i][j])
    else:
        for j in range(w):
        #左右のエラーを排除
            if j != 0 and j != w-1:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            elif j == 0:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i][j+1]+s[i-1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            else:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i][j-1]+s[i-1][j-1]).count("#"))
                else:
                    ans.append(s[i][j])
    ans1.append(ans)

for i in range(h):
    for j in range(w):
        print(ans1[i][j],end = "")
    print("\n")

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[list(input().replace(".","0")) for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      for k in range(-1,2):
        for l in range(-1,2):
          if 0<=i+k<=h and 0<=j+l<=w and s[i+k][j+l]!="#":
            s[i+k][j+l]=str(int(s[i+k][j+l])+1)
for i in range(h):
  for j in range(w):
    print(s[i][j], end="")
  print()

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
c=[]
for i in range(a):
	c.append([i for i in input()])
#print(c)
#print(c[0][0])
d=[]
for i in range(b):
	d.append([])

for j in range(a):
	for i in range(b):
		if c[j][i]=="#":
			d[j].append("#")
		else:
			e=0
			if j==0 and i==b-1:
				if c[0][b-2]=="#":
					e=e+1
				if c[1][b-1]=="#":
					e=e+1
				if c[1][b-2]=="#":
					e=e+1
				d[j].append(e)
			elif j==0 and i==0:
				if c[0][1]=="#":
					e=e+1
				if c[1][0]=="#":
					e=e+1
				if c[1][1]=="#":
					e=e+1
				d[j].append(e)
			elif j==a-1 and i==0:
				if c[a-2][0]=="#":
					e=e+1
				if c[a-2][1]=="#":
					e=e+1
				if c[a-1][1]=="#":
					e=e+1
				d[j].append(e)
			elif j==a-1 and i==b-1:
				if c[a-2][b-1]=="#":
					e=e+1
				if c[a-2][b-2]=="#":
					e=e+1
				if c[a-1][b-2]=="#":
					e=e+1
				d[j].append(e)
			elif j==0 and (i!=0 and i!=b-1):
				if c[0][i-1]=="#":
					e=e+1
				if c[0][i+1]=="#":
					e=e+1
				if c[1][i]=="#":
					e=e+1
				if c[1][i-1]=="#":
					e=e+1
				if c[1][i+1]=="#":
					e=e+1
				d[j].append(e)
			elif j==a-1 and (i!=0 and i!=b-1):
				if c[a-1][i-1]=="#":
					e=e+1
				if c[a-1][i+1]=="#":
					e=e+1
				if c[a-2][i]=="#":
					e=e+1
				if c[a-2][i-1]=="#":
					e=e+1
				if c[a-2][i+1]=="#":
					e=e+1
				d[j].append(e)
			elif i==0 and (j!=0 and j!=a-1):
				if c[j-1][0]=="#":
					e=e+1
				if c[j+1][0]=="#":
					e=e+1
				if c[j][1]=="#":
					e=e+1
				if c[j-1][1]=="#":
					e=e+1
				if c[j+1][1]=="#":
					e=e+1
				d[j].append(e)
			elif i==b-1 and (j!=0 and j!=a-1):
				if c[j-1][b-1]=="#":
					e=e+1
				if c[j+1][b-1]=="#":
					e=e+1
				if c[j][b-2]=="#":
					e=e+1
				if c[j-1][b-2]=="#":
					e=e+1
				if c[j+1][b-2]=="#":
					e=e+1
				d[j].append(e)
			else:
				if c[j-1][i-1]=="#":
					e=e+1
				if c[j-1][i]=="#":
					e=e+1
				if c[j-1][i+1]=="#":
					e=e+1
				if c[j][i+1]=="#":
					e=e+1
				if c[j][i-1]=="#":
					e=e+1
				if c[j+1][i-1]=="#":
					e=e+1
				if c[j+1][i]=="#":
					e=e+1
				if c[j+1][i+1]=="#":
					e=e+1
				d[j].append(e)

#print(d)
for j in range(a):
	for i in range(b):
		print(d[j][i],end="")
	print("")

Submission status: Runtime Error

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

def one_c(i,j):
    c_tmp = 0
    if S[i][j] == '#': c_tmp += 1
    return c_tmp

def two_c1(i,j):
    c_tmp = 0
    if S[i][j-1] == '#': c_tmp += 1
    if S[i][j] == '#': c_tmp += 1
    return c_tmp

def two_c2(i,j):
    c_tmp = 0
    if S[i][j-1] == '#': c_tmp += 1
    if S[i][j+1] == '#': c_tmp += 1
    return c_tmp

def three_c(i,j):
    c_tmp = 0
    if S[i][j-1] == '#': c_tmp += 1
    if S[i][j] == '#': c_tmp += 1
    if S[i][j+1] == '#': c_tmp += 1
    return c_tmp

if H == W == 1 and S[0][0] == '.':
    print('0')
    exit()

for i in range(H):
    for j in range(W):
        count = 0
        if S[i][j] == '#':
            count = '#'
        elif 0 < i < H-1:
            if 0 < j < W-1:
                count += three_c(i-1,j)
                count += two_c2(i,j)
                count += three_c(i+1,j)
            elif j == 0:
                count += two_c1(i-1,j+1)
                count += one_c(i,j+1)
                count += two_c1(i+1,j+1)
            elif j == W-1:
                count += two_c1(i-1,j)
                count += one_c(i,j-1)
                count += two_c1(i+1,j)
        elif i == 0:
            if 0 < j < W-1:
                count += two_c2(i,j)
                count += three_c(i+1,j)
            elif j == 0:
                count += one_c(i,j+1)
                count += two_c1(i+1,j+1)
            elif j == W-1:
                count += one_c(i,j-1)
                count += two_c1(i+1,j)
        elif i == H-1:
            if 0 < j < W-1:
                count += three_c(i-1,j)
                count += two_c2(i,j)
            elif j == 0:
                count += two_c1(i-1,j+1)
                count += one_c(i,j+1)
            elif j == W-1:
                count += two_c1(i-1,j)
                count += one_c(i,j-1)
        ans.append(count)
for i in range(len(ans)):
    if (i+1) % W == 0:
        print(ans[i])
    else:
        print(ans[i], end='')

Submission status: Runtime Error

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

def one_c(i,j):
    c_tmp = 0
    if S[i][j] == '#': c_tmp += 1
    return c_tmp

def two_c1(i,j):
    c_tmp = 0
    if S[i][j-1] == '#': c_tmp += 1
    if S[i][j] == '#': c_tmp += 1
    return c_tmp

def two_c2(i,j):
    c_tmp = 0
    if S[i][j-1] == '#': c_tmp += 1
    if S[i][j+1] == '#': c_tmp += 1
    return c_tmp

def three_c(i,j):
    c_tmp = 0
    if S[i][j-1] == '#': c_tmp += 1
    if S[i][j] == '#': c_tmp += 1
    if S[i][j+1] == '#': c_tmp += 1
    return c_tmp

for i in range(H):
    for j in range(W):
        count = 0
        if S[i][j] == '#':
            count = '#'
        elif 0 < i < H-1:
            if 0 < j < W-1:
                count += three_c(i-1,j)
                count += two_c2(i,j)
                count += three_c(i+1,j)
            elif j == 0:
                count += two_c1(i-1,j+1)
                count += one_c(i,j+1)
                count += two_c1(i+1,j+1)
            elif j == W-1:
                count += two_c1(i-1,j)
                count += one_c(i,j-1)
                count += two_c1(i+1,j)
        elif i == 0:
            if 0 < j < W-1:
                count += two_c2(i,j)
                count += three_c(i+1,j)
            elif j == 0:
                count += one_c(i,j+1)
                count += two_c1(i+1,j+1)
            elif j == W-1:
                count += one_c(i,j-1)
                count += two_c1(i+1,j)
        elif i == H-1:
            if 0 < j < W-1:
                count += three_c(i-1,j)
                count += two_c2(i,j)
            elif j == 0:
                count += two_c1(i-1,j+1)
                count += one_c(i,j+1)
            elif j == W-1:
                count += two_c1(i-1,j)
                count += one_c(i,j-1)
        ans.append(count)
for i in range(len(ans)):
    if (i+1) % W == 0:
        print(ans[i])
    else:
        print(ans[i], end='')

Submission status: Runtime Error

Solution code:
import numpy as np
h, w = map(int, input().split())
num_array = np.zeros((h, w))
space_array = np.ones((h, w))

mine_map = np.array(
    ((0 if i == "." else 1 for i in input())) for j in range(h))
space_array = mine_map.copy()

for i, s in enumerate(mine_map):
    for j, m in enumerate(s):
        if m == 1: # m=mass
            il, iu = i-1, i+2
            jl, ju = j-1, j+2
            il, jl = max(il, 0), max(jl, 0)
            iu, ju = min(iu, h), min(ju, w)
            num_array[il:iu, jl:ju] += 1

for i, s in enumerate(space_array):
    out = ("#" if m == 1 else str(int(num_array[i,j])) for j, m in enumerate(s))
    print("".join(out))

Submission status: Runtime Error

Solution code:
import numpy as np

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

in_table = []
for h in range(H):
    line = [*input()]    
    in_table.append(line)

result_table = np.zeros((H, W), dtype=np.int8)
for y, line in enumerate(in_table):
    for x, cell in enumerate(line):
        if cell == '#':
            result_table[y-1:y+1+1, x-1:x+1+1] += 1
            result_table[y][x] = -1

for line in result_table.astype(str):
    for cell in line:
        if cell == '-1':
            print('#', end='')
        else:
            print(cell, end='')
    print('')

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ms=[]
for i in range(h):
    m=input()
    ms.append(list(m))
count=0
for i in range(h):
    for j in range(w):
        if ms[i][j]==".":
            if i>0 and j>0:
                if ms[i-1][j-1]=="#":
                    count+=1
            if i>0 :
               if ms[i-1][j]=="#":
                    count+=1
            if i>0 and w-1>j:
                if ms[i-1][j+1]=="#":
                    count+=1
            if j>0 :
                if ms[i][j-1]=="#":
                    count+=1
            if w-1>j :
                if ms[i][j+1]=="#":
                    count+=1
            if h-1>i and j>0 :
                if ms[i+1][j-1]=="#":
                    count+=1
            if h-1>i:
                if ms[i+1][j]=="#":
                    count+=1
            if h-1>i and h-1>j:
                if ms[i+1][j+1]=="#":
                    count+=1
            ms[i][j]=str(count)
            count=0

for i in range(h):
    temp="".join([ms[i][j] for j in range(w)])
    print(temp)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ms=[]
for i in range(3):
    m=input()
    ms.append(list(m))
count=0
for i in range(h):
    for j in range(w):
        if ms[i][j]==".":
            if i>0 and j>0 :
                if ms[i-1][j-1]=="#":
                    count+=1
            if i>0 :
                if ms[i-1][j]=="#":
                    count+=1
            if i>0 and w>j+1 :
                if ms[i-1][j+1]=="#":
                    count+=1
            if j>0 :
                if ms[i][j-1]=="#":
                    count+=1
            if w>j+1 :
                if ms[i][j+1]=="#":
                    count+=1
            if h>i+1 and j>0 :
                if ms[i+1][j-1]=="#":
                    count+=1
            if h>i+1 :
                if ms[i+1][j]=="#":
                    count+=1
            if h>i+1 and h>j+1 :
                if ms[i+1][j+1]=="#":
                    count+=1
            ms[i][j]=str(count)
            count=0

for i in range(h):
    temp="".join(ms[i])
    print(temp)

Submission status: Runtime Error

Solution code:
g,r=map(int,input().split())
A=["."]*(r+2)
for i in range(g):
  w="."+input()+"."
  A.append(w)
A.append("."*(r+2))
for i in range(1,g+1):
  for j in range(1,r+1):
    if A[i][j]==".":
      B=[]
      for m in [i-1,i,i+1]:
        for k in [j-1,j,j+1]:
          B.append(A[m][k])
      A[i]=A[i][1:r+1:].replace(".",str(B.count("#")),1)
      A[i]="."+A[i]+"."
for i in range(1,g+1):
  print(A[i][1:r+1:])

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [0] * h
for i in range(h):
    s[i] = str(input())
t = [[0]*w]*h
for j in range(h):
    if j == 0:
        for k in range(w):
            if k == 0:
                u = s[0][1] + s[1][0] + s[1][1]
            elif k == w-1:
                u = s[0][k-1] +s[1][k-1] + s[1][k]
            else:
                u = s[0][k-1] +s[0][k+1] + s[1][k-1] + s[1][k] + s[1][k+1]
            v = u.count('#')
            if s[j][k] == '#':
                v = '#'
            t[j][k] = str(v)
    elif j == h-1:
        for k in range(w):
            if k == 0:
                u = s[j][1] + s[j-1][0] + s[j-1][1]
            elif k == w-1:
                u = s[j][k-1] +s[j-1][k-1] + s[j-1][k]
            else:
                u = s[j][k-1] +s[j][k+1] + s[j-1][k-1] + s[j-1][k] + s[j-1][k+1]
            v = u.count('#')
            if s[j][k] == '#':
                v = '#'
            t[j][k] = str(v)
    else:
         for k in range(w):
            if k == 0:
                u = s[j][1] + s[j-1][0] + s[j-1][1] + s[j+1][0] + s[j+1][1]
            elif k == w-1:
                u = s[j][k-1] +s[j-1][k-1] + s[j-1][k] + s[j+1][k-1] + s[j+1][k]
            else:
                u = s[j][k-1] +s[j][k+1] + s[j-1][k-1] + s[j-1][k] + s[j-1][k+1] + s[j+1][k-1] + s[j+1][k] + s[j+1][k+1]
            v = u.count('#')
            if s[j][k] == '#':
                v = '#'
            t[j][k] = str(v)
    print(''.join(t[j]))

Submission status: Runtime Error

Solution code:
g,r=map(int,input().split())
A=["."*r]
for i in range(g):
  w="."+input()+"."
  A.append(w)
A.append("."*r)
B=[]
for i in range(1,g+1):
  for j in range(1,r+1):
    if A[i][j]==".":
      for m in [i-1,i,i+1]:
        for k in [j-1,j,j+1]:
          B=append(A[m][k])
      A[i][j]=B.count("#")
for i in range(g):
  print(A[g])

Submission status: Runtime Error

Solution code:
def draw_map(count_map, row_index, col_index) :
	for i in range(row_index-1, row_index+2) :
		if (i < 0 or len(count_map) <= i) :
			continue
		for j in range(col_index-1, col_index+2) :
			if (j < 0 or len(count_map[0]) <= j) :
				continue
			count_map[i][j] += 1
	
	return count_map
    				
    			

H,W = [int(i) for i in input().split()]
mines = []
for i in range(H) :
	mines.append(input())

# 白地図を用意して、1ずつ足す
# 空きマスだったら数字に置き換える
count_map = [[0 for i in range(W)] for j in range(H)]
for i in range(H) :
	for j in range(W) :
		if (mines[i][j] == "#") :
			mine_map = draw_map(count_map,i,j)

for i in range(H) :
	out_line = ""
	for j in range(H) :
		if (mines[i][j] == ".") :
			out_line += str(count_map[i][j])
		else :
			out_line += "#"
	print(out_line)