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:
h, w = map(int, input().split())

s = []

for i in range(h):
    s.append(list(input()))

#print(s)

count = 0

for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == "#":
            pass

        elif i == 0 and j == 0:
            if s[i][j + 1] == "#":
                count += 1
            if s[i + 1][j] == "#":
                count += 1
            if s[i + 1][j + 1] == "#":
                count += 1

            s[i][j] = count

        elif i == 0 and j == w - 1:
            if s[i + 1][j] == "#":
                count += 1
            if s[i + 1][j - 1] == "#":
                count += 1
            if s[i][j - 1] == "#":
                count += 1

            s[i][j] = count

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

            s[i][j] = count

        elif i == h - 1 and j == w - 1:
            if s[i - 1][j] == "#":
                count += 1
            if s[i - 1][j - 1] == "#":
                count += 1
            if s[i][j - 1] == "#":
                count += 1

            s[i][j] = count

            
        elif i == 0:
            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

            s[i][j] = count

        elif i == h - 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
            
            s[i][j] = count

        elif j == 0:
            if s[i - 1][j] == "#":
                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 + 1][j + 1] == "#":
                count += 1

            s[i][j] = count

        elif j == w - 1:
            if s[i - 1][j] == "#":
                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 + 1][j - 1] == "#":
                count += 1

            s[i][j] = count

        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

            s[i][j] = count

#print(s)
for i in range(h):
    print(''.join(map(str, s[i])))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
l=[list(input()) for i in range(h)]
for x in range(h):
 for y in range(w):
  if l[x][y]=='.':
   c=0
   if x>0 and y>0:
    if l[x-1][y-1]=='#':c+=1
   if x>0:
    if l[x-1][y]=='#':c+=1
   if x>0 and y+1<h:
    if l[x-1][y+1]=='#':c+=1
   if y>0:
    if l[x][y-1]=='#':c+=1
   if y+1<h:
    if l[x][y+1]=='#':c+=1
   if x+1<h and y>0:
    if l[x+1][y-1]=='#':c+=1
   if x+1<h:
    if l[x+1][y]=='#':c+=1
   if x+1<h and y+1<h:
    if l[x+1][y+1]=='#':c+=1
   l[x][y]=str(c)
for j in range(h):
  l[j]=''.join(l[j])
  print(l[j])

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[]
ans=''
anslist=[]
minelist=[]
for _ in range(h):
    s.append(list(str(input())))
for i in range(h):
    for j in range(w):
        if (s[i])[j]=='#':
            ans=ans+'#'
        else:
            if j==0:
                if i==0:
                    minelist.append((s[1])[0])
                    minelist.append((s[1])[1])
                    minelist.append((s[0])[1])
                    ans=ans+str(minelist.count('#'))
                elif i==h-1:
                    minelist.append((s[i-1])[0])
                    minelist.append((s[i-1])[1])
                    minelist.append((s[i])[1])
                    ans=ans+str(minelist.count('#'))
                else:
                    minelist.append((s[i-1])[0])
                    minelist.append((s[i-1])[1])
                    minelist.append((s[i])[1])
                    minelist.append((s[i+1])[0])
                    minelist.append((s[i+1])[1])
                    ans=ans+str(minelist.count('#'))
            elif i==0:
                if j==w-1:
                    minelist.append((s[0])[w-2])
                    minelist.append((s[1])[w-1])
                    minelist.append((s[1])[w-2])
                    ans=ans+str(minelist.count('#'))
                else:
                    minelist.append((s[0])[j-1])
                    minelist.append((s[0])[j+1])
                    minelist.append((s[1])[i-1])
                    minelist.append((s[1])[i])
                    minelist.append((a[1])[i+1])
                    ans=ans+str(minelist.count('#'))
            else:
                minelist.append((s[i-1])[j-1])
                minelist.append((s[i-1])[j])
                minelist.append((s[i-1])[j+1])
                minelist.append((s[i])[j-1])
                minelist.append((s[i])[j+1])
                minelist.append((s[i+1])[j-1])
                minelist.append((s[i+1])[j+1])
                minelist.append((s[i+1])[j])
                ans=ans+str(minelist.count('#'))
        minelist=[]
    anslist.append(ans)
for L in anslist:
    print(L)

Submission status: Runtime Error

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()

    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

#tate
h = int(arr_data[0][0])
#yoko
w = int(arr_data[0][1])

arr = []
for i in range(1, h + 1):
    
    arr_temp = []
        
    for temp in arr_data[i][0]:
        
        if temp==".":
            arr_temp.append(0)
        else:
            arr_temp.append(1)
            
    arr.append(arr_temp)
    
arr_bakudan=[]    
for i in range(h):
    
    arr_temp=[]
    for j in range(w):
        
        #先頭行
        if i==0:
            
            #先頭列
            if j==0:
                
                if arr[i][j]==1:
                    cnt="#"
                else:
                    cnt=arr[i+1][j]+arr[i][j+1]+arr[i+1][j+1]
                arr_temp.append(str(cnt))   
                    
            #最終列    
            elif j==w-1:
            
                if arr[i][j]==1:
                    cnt="#"
                else:
                    cnt=arr[i+1][j]+arr[i][j-1]+arr[i+1][j-1]
                arr_temp.append(str(cnt))    
                
            else:
            
                if arr[i][j]==1:
                    cnt="#"
                else:    
                    cnt=arr[i][j-1]+arr[i][j+1]+arr[i+1][j]+ arr[i+1][j-1]+  arr[i+1][j+1]
                arr_temp.append(str(cnt))    
                
        #最終行            
        elif i==h-1:
        
            #先頭列
            if j==0:
                
                if arr[i][j]==1:
                    cnt="#"
                else:
                    cnt=arr[i-1][j]+arr[i][j+1]+arr[i-1][j+1]
                arr_temp.append(str(cnt))    
                
            #最終列  
            elif j==w-1:
                
                if arr[i][j]==1:
                    cnt="#"
                else:
                    cnt=arr[i-1][j]+arr[i-1][j-1]+arr[i][j-1]
                arr_temp.append(str(cnt))    
                
            else:
                
                if arr[i][j]==1:
                    cnt="#"
                else:
                    cnt=arr[i][j+1]+arr[i][j-1]+arr[i-1][j]+  arr[i-1][j-1]+  arr[i-1][j+1]
                arr_temp.append(str(cnt))    
                    
        else:
            
            #先頭列
            if j==0:
                     
                if arr[i][j]==1:
                    cnt="#"
                else:
                    cnt= arr[i-1][j]+arr[i+1][j]+arr[i][j+1]+arr[i-1][j+1]+arr[i+1][j+1]
                arr_temp.append(str(cnt))   
                    
               
            #最終列  
            elif j==w-1:
                
                if arr[i][j]==1:
                    cnt="#"
                else:
                    cnt=arr[i-1][j]+arr[i+1][j]+arr[i][j-1]+arr[i-1][j-1]+arr[i+1][j-1]
                arr_temp.append(str(cnt))   
              
            else:

                if arr[i][j]==1:
                    cnt="#"
                else:
                    cnt=arr[i-1][j]+arr[i+1][j]+arr[i][j-1]+arr[i-1][j-1]+arr[i+1][j-1]+arr[i][j+1]+arr[i-1][j+1]+arr[i+1][j+1]
                arr_temp.append(str(cnt))    
    
    arr_bakudan.append(arr_temp)
                  
for v in arr_bakudan:
    
    print("".join(v))

Submission status: Runtime Error

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()

    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

#tate
h = int(arr_data[0][0])
#yoko
w = int(arr_data[0][1])

arr = []
for i in range(1, h + 1):
    
    arr_temp = []
        
    for temp in arr_data[i][0]:
        
        arr_temp.append(temp)
    
    arr.append(arr_temp)
    
arr_bakudan=[]    
for i in range(h):
    
    arr_temp=[]
    for j in range(w):
        
        #先頭行
        if i==0:
            
            #先頭列
            if j==0:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i+1][j+1]=="#":
                        cnt+=1
                    
                arr_temp.append(str(cnt))   
                    
            #最終列    
            elif j==w-1:
            
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i+1][j-1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                
            else:
            
            
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:    
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i+1][j-1]=="#":
                        cnt+=1
                    if arr[i+1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                
        #最終行            
        elif i==h-1:
        
            #先頭列
            if j==0:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i-1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                
            #最終列  
            elif j==w-1:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i-1][j-1]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                
            else:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i-1][j-1]=="#":
                        cnt+=1
                    if arr[i-1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                    
        else:
            
            #先頭列
            if j==0:
                        
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i-1][j+1]=="#":
                        cnt+=1
                    if arr[i+1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))   
                    
               
            #最終列  
            elif j==w-1:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i-1][j-1]=="#":
                        cnt+=1
                    if arr[i+1][j-1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))   
                    
               
            else:

                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i-1][j-1]=="#":
                        cnt+=1
                    if arr[i+1][j-1]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i-1][j+1]=="#":
                        cnt+=1
                    if arr[i+1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
    
    arr_bakudan.append(arr_temp)
                  
for v in arr_bakudan:
    
    print("".join(v))

Submission status: Runtime Error

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()

    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:


        return array_result


def check01(arr_bakudan, x, y, h, w):
    
    print(x,y,h,w,arr_bakudan[x][y])
    
    cnt = 0
    
    if x-1 >= 0 and y-1 >= 0 and arr_bakudan[x-1][y-1] == "#":
    
        cnt += 1
    
    if x-1 >= 0 and arr_bakudan[x-1][y] == "#":
        
        cnt += 1
    
    if x-1 >= 0 and y + 1 >= h-1 and arr_bakudan[x-1][y + 1] == "#":
        
        cnt += 1
   
    if y-1 >= 0 and arr_bakudan[x][y-1] == "#":
            
        cnt += 1
    
    if x + 1 >= w-1 and y-1 >= 0 and arr_bakudan[x + 1][y-1] == "#":
        
        cnt += 1


    if arr_bakudan[x][y] == "#":
        cnt += 1

    if y + 1 >= h-1 and arr_bakudan[x][y + 1] == "#":
        cnt += 1

    if x + 1 >= w-1 and arr_bakudan[x + 1][y] == "#":
        cnt += 1
    
    if x + 1 >= w-1 and y + 1 >= h-1 and arr_bakudan[x + 1][y + 1] == "#":
        cnt += 1
        
    return cnt

arr_data = getinputdata()

#tate
h = int(arr_data[0][0])
#yoko
w = int(arr_data[0][1])

arr = []
for i in range(1, h + 1):
    
    arr_temp = []
        
    for temp in arr_data[i][0]:
        
        arr_temp.append(temp)
    
    arr.append(arr_temp)
    
arr_bakudan=[]    
for i in range(h):
    
    arr_temp=[]
    for j in range(w):
        
        #先頭行
        if i==0:
            
            #先頭列
            if j==0:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i+1][j+1]=="#":
                        cnt+=1
                    
                arr_temp.append(str(cnt))   
                    
            #最終列    
            elif j==w-1:
            
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i+1][j-1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                
            else:
            
            
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:    
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i+1][j-1]=="#":
                        cnt+=1
                    if arr[i+1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                
        #最終行            
        elif i==h-1:
        
            #先頭列
            if j==0:
                
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i-1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                
            #最終列  
            elif j==w-1:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i-1][j-1]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                
            else:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i-1][j-1]=="#":
                        cnt+=1
                    if arr[i-1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
                    
        else:
            
            #先頭行
            if j==0:
                        
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i-1][j+1]=="#":
                        cnt+=1
                    if arr[i+1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))   
                    
               
            #最終列  
            elif j==w-1:
                
                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i-1][j-1]=="#":
                        cnt+=1
                    if arr[i+1][j-1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))   
                    
               
            else:

                cnt=0
                if arr[i][j]=="#":
                    cnt="#"
                else:
                    if arr[i-1][j]=="#":
                        cnt+=1
                    if arr[i+1][j]=="#":
                        cnt+=1
                    if arr[i][j-1]=="#":
                        cnt+=1
                    if arr[i-1][j-1]=="#":
                        cnt+=1
                    if arr[i+1][j-1]=="#":
                        cnt+=1
                    if arr[i][j+1]=="#":
                        cnt+=1
                    if arr[i-1][j+1]=="#":
                        cnt+=1
                    if arr[i+1][j+1]=="#":
                        cnt+=1
                arr_temp.append(str(cnt))    
    
    arr_bakudan.append(arr_temp)
                  
for v in arr_bakudan:
    
    print("".join(v))

Submission status: Runtime Error

Solution code:
H,W = map(int, input().split())
board = []
for i in range(0,H):
	board.append(input())
board = list(board)
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
for i in range(0,H):
	for j in range(0,W):
		if board[i][j] == '#':
			continue
		num = 0
		for k in range(0, 8):
			ni = i + dy[k]
			nj = j + dx[k]
			if (ni < 0) or (H <= ni):
				continue
			if (nj < 0) or (W <= nj):
				continue
			if board[ni][nj] == '#':
				num += 1
		board[i][j] = str(num)
		
	for i in range(0,H):
		print(board[i])

Submission status: Runtime Error

Solution code:
x=input().split()
h=int(x[0])
w=int(x[1])
inp=[0]*h*w
for i in range(h):
    x=input()
    y=0
    r=i*w
    for j in x:
        if j=='#':
            inp[r+y]='#'
            if h==w and w==1:
                break
            if r+y==0:
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y+w+1]!='#':
                    inp[r+y+w+1]+=1
            elif r+y==w-1:
                if inp[w-2]!='#':
                    inp[w-2]+=1
                if inp[w-1+w]!='#':
                    inp[w-1+w]+=1
                if inp[w-2+w]!='#':
                    inp[w-2+w]+=1
            elif r+y==(h-1)*w:
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y-w+1]!='#':
                    inp[r+y-w+1]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
            elif r+y==h*w-1:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y-w-1]!='#':
                    inp[r+y-w-1]+=1
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
            elif r+y<w-1:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y+w-1]!='#':
                    inp[r+y+w-1]+=1
                if inp[r+y+w+1]!='#':
                    inp[r+y+w+1]+=1
            elif (r+y)%w==0:
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y-w+1]!='#':
                    inp[r+y-w+1]+=1
                if inp[r+y+w+1]!='#':
                    inp[r+y+w+1]+=1
            elif (r+y)%w==w-1:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y+w-1]!='#':
                    inp[r+y+w-1]+=1
                if inp[r+y-w-1]!='#':
                    inp[r+y-w-1]+=1
            elif r+y>(h-1)*w:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y-w-1]!='#':
                    inp[r+y-w-1]+=1
                if inp[r+y-w+1]!='#':
                    inp[r+y-w+1]+=1
            else:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y+w-1]!='#':
                    inp[r+y+w-1]+=1
                if inp[r+y+w+1]!='#':
                    inp[r+y+w+1]+=1
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y-w+1]!='#':
                    inp[r+y-w+1]+=1
                if inp[r+y-w-1]!='#':
                    inp[r+y-w-1]+=1
                
        y+=1
ans=''
for i in range(h):
    for j in range(w):
        ans+=str(inp[i*w+j])
    print(ans)
    ans=''

Submission status: Runtime Error

Solution code:
x=input().split()
h=int(x[0])
w=int(x[1])
inp=[0]*h*w
for i in range(h):
    x=input()
    y=0
    r=i*w
    for j in x:
        if j=='#':
            inp[r+y]='#'
            if r+y==0:
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y+w+1]!='#':
                    inp[r+y+w+1]+=1
            elif r+y==w-1:
                if inp[w-2]!='#':
                    inp[w-2]+=1
                if inp[w-1+w]!='#':
                    inp[w-1+w]+=1
                if inp[w-2+w]!='#':
                    inp[w-2+w]+=1
            elif r+y==(h-1)*w:
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y-w+1]!='#':
                    inp[r+y-w+1]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
            elif r+y==h*w-1:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y-w-1]!='#':
                    inp[r+y-w-1]+=1
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
            elif r+y<w-1:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y+w-1]!='#':
                    inp[r+y+w-1]+=1
                if inp[r+y+w+1]!='#':
                    inp[r+y+w+1]+=1
            elif (r+y)%w==0:
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y-w+1]!='#':
                    inp[r+y-w+1]+=1
                if inp[r+y+w+1]!='#':
                    inp[r+y+w+1]+=1
            elif (r+y)%w==w-1:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y+w-1]!='#':
                    inp[r+y+w-1]+=1
                if inp[r+y-w-1]!='#':
                    inp[r+y-w-1]+=1
            elif r+y>(h-1)*w:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y-w-1]!='#':
                    inp[r+y-w-1]+=1
                if inp[r+y-w+1]!='#':
                    inp[r+y-w+1]+=1
            else:
                if inp[r+y-1]!='#':
                    inp[r+y-1]+=1
                if inp[r+y+1]!='#':
                    inp[r+y+1]+=1
                if inp[r+y+w]!='#':
                    inp[r+y+w]+=1
                if inp[r+y+w-1]!='#':
                    inp[r+y+w-1]+=1
                if inp[r+y+w+1]!='#':
                    inp[r+y+w+1]+=1
                if inp[r+y-w]!='#':
                    inp[r+y-w]+=1
                if inp[r+y-w+1]!='#':
                    inp[r+y-w+1]+=1
                if inp[r+y-w-1]!='#':
                    inp[r+y-w-1]+=1
                
        y+=1
ans=''
for i in range(h):
    for j in range(w):
        ans+=str(inp[i*w+j])
    print(ans)
    ans=''