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 = []
cnt = [[0 for _ in range(W)] for _ in range(H)]
for h in range(H):
    s = list(input())
    for w in range(W):
        if w == 0:
            cnt[h][w] += s[w+1] == "#"
        elif w == W-1:
            cnt[h][w] += s[w-1] == "#"
        else:
            cnt[h][w] += s[w+1] == "#"
            cnt[h][w] += s[w-1] == "#"
    S.append(s)
    
for h in range(H):
    for w in range(W):
        if h == 0:
            cnt[h][w] += S[h+1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h+1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h+1][w-1] == "#"
            else:
                cnt[h][w] += S[h+1][w-1] == "#"
                cnt[h][w] += S[h+1][w+1] == "#"
        elif h == H-1:
            cnt[h][w] += S[h-1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h-1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h-1][w-1] == "#"
            else:
                cnt[h][w] += S[h-1][w-1] == "#"
                cnt[h][w] += S[h-1][w+1] == "#"
        else:
            cnt[h][w] += S[h+1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h+1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h+1][w-1] == "#"
            else:
                cnt[h][w] += S[h+1][w-1] == "#"
                cnt[h][w] += S[h+1][w+1] == "#"
            cnt[h][w] += S[h-1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h-1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h-1][w-1] == "#"
            else:
                cnt[h][w] += S[h-1][w-1] == "#"
                cnt[h][w] += S[h-1][w+1] == "#"
                
for h in range(H):
    s = S[h]
    if s.count("#") == 0:
        ans = map(str, cnt[h])
        print("".join(ans))
        continue
    for w in range(W):
        if s[w] == "#":
            cnt[h][w] = "#"
    ans = map(str, cnt[h])
    print("".join(ans))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

def get_count(S, H, W, i, j):
    num = 0
    start_i = start_j = -1
    end_i = end_j = 2
    if i == 0:
        start_i = 0
    elif i == W-1:
        end_i = 1
    if j == 0:
        start_j = 0
    elif j == H-1:
        end_j = 1

    for y in range(start_j, end_j, 1):
        for x in range(start_i, end_i, 1):
            if S[j+y][i+x] == "#":
                num += 1

    return num


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

for y in range(H):
    for x in range(W):
        if S[y][x] == ".":
            num = get_count(S, H, W, x, y)
            S[y][x] = str(num)

for y in range(H):
    print("".join(S[y]))

Submission status: Runtime Error

Solution code:
import re
import copy

def accept_input():
    H, W = map(int, input().split())
    S = []
    for _ in range(H):
        S.append(input())
    return H,W,S

def process(s):
    if s == "#":
        return 1
    else:
        return 0
H,W,S = accept_input()
new_S = copy.deepcopy(S)
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            new_S[i] = new_S[i][:j]+"#"+new_S[i][j+1:]
        else:
            if i == 0:
                if j == 0:
                    k = process(new_S[i][j+1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])
                elif j == W-1:
                    k = process(new_S[i][j-1])+process(new_S[i+1][j-1])+process(new_S[i+1][j])
                else:
                    k = process(new_S[i][j+1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])+process(new_S[i+1][j-1])+process(new_S[i][j-1])
            elif i == H-1:
                if j == 0:
                    k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])
                elif j == W-1:
                    k = process(new_S[i][j-1])+process(new_S[i-1][j-1])+process(new_S[i-1][j])
                else:
                    k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])+process(new_S[i-1][j-1])+process(new_S[i][j-1])
            else:
                if j == 0:
                    k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])++process(new_S[i+1][j+1])+process(new_S[i+1][j])
                elif j == W-1:
                    k = process(new_S[i-1][j])+process(new_S[i-1][j-1])+process(new_S[i][j-1])+process(new_S[i+1][j])+process(new_S[i+1][j-1])
                else:    
                    k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])+process(new_S[i-1][j-1])+process(new_S[i][j-1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])+process(new_S[i+1][j-1])
            new_S[i] = new_S[i][:j]+str(k)+new_S[i][j+1:]
for l in new_S:
    print(l)

Submission status: Runtime Error

Solution code:
import re
import copy

def accept_input():
    H, W = map(int, input().split())
    S = []
    for _ in range(H):
        S.append(input())
    return H,W,S

def process(s):
    if s == "#":
        return 1
    else:
        return 0

new_S = copy.deepcopy(S)
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            new_S[i] = new_S[i][:j]+"#"+new_S[i][j+1:]
        else:
            if i == 0:
                if j == 0:
                    k = process(new_S[i][j+1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])
                elif j == W-1:
                    k = process(new_S[i][j-1])+process(new_S[i+1][j-1])+process(new_S[i+1][j])
                else:
                    k = process(new_S[i][j+1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])+process(new_S[i+1][j-1])+process(new_S[i][j-1])
            elif i == H-1:
                if j == 0:
                    k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])
                elif j == W-1:
                    k = process(new_S[i][j-1])+process(new_S[i-1][j-1])+process(new_S[i-1][j])
                else:
                    k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])+process(new_S[i-1][j-1])+process(new_S[i][j-1])
            else:
                k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])+process(new_S[i-1][j-1])+process(new_S[i][j-1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])+process(new_S[i+1][j-1])
            new_S[i] = new_S[i][:j]+str(k)+new_S[i][j+1:]
for l in new_S:
  print(l)

Submission status: Runtime Error

Solution code:
import re
import copy

def accept_input():
    H, W = map(int, input().split())
    S = []
    for _ in range(H):
        S.append(input())
    return H,W,S

def process(s):
    if s == "#":
        return 1
    else:
        return 0

new_S = copy.deepcopy(S)
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            new_S[i] = new_S[i][:j]+"#"+new_S[i][j+1:]
        else:
            if i == 0:
                if j == 0:
                    k = process(new_S[i][j+1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])
                elif j == W-1:
                    k = process(new_S[i][j-1])+process(new_S[i+1][j-1])+process(new_S[i+1][j])
                else:
                    k = process(new_S[i][j+1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])+process(new_S[i+1][j-1])+process(new_S[i][j-1])
            elif i == H-1:
                if j == 0:
                    k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])
                elif j == W-1:
                    k = process(new_S[i][j-1])+process(new_S[i-1][j-1])+process(new_S[i-1][j])
                else:
                    k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])+process(new_S[i-1][j-1])+process(new_S[i][j-1])
            else:
                k = process(new_S[i][j+1])+process(new_S[i-1][j+1])+process(new_S[i-1][j])+process(new_S[i-1][j-1])+process(new_S[i][j-1])+process(new_S[i+1][j+1])+process(new_S[i+1][j])+process(new_S[i+1][j-1])
            new_S[i] = new_S[i][:j]+str(k)+new_S[i][j+1:]

Submission status: Runtime Error

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

S = [list(input()) for i in range(H)]
for i in range(1,H-1):
  for j in range(1,W-1):
    if S[i][j] == '.':
      x = [S[i-1][j-1], S[i-1][j], S[i-1][j+1], S[i][j-1], S[i][j+1],S[i+1][j-1], S[i+1][j],S[i+1][j+1]]
      S[i][j] = x.count('#')

for j in range(1,W-1):
  if S[0][j] == '.':
    x = [S[0][j-1], S[0][j+1], S[1][j-1], S[1][j], S[1][j+1]]
    S[0][j] = x.count('#')
    
  if S[H-1][j] == '.':
    x = [S[H-1][j-1], S[H-1][j+1], S[H-2][j-1], S[H-2][j], S[H-2][j+1]]
    S[H-1][j] = x.count('#')

for i in range(1, H-1):
  if S[i][0] == '.':
    x =[S[i-1][0],S[i-1][1],  S[i][1], S[i+1][0], S[i+1][1]]
    S[i][0] = x.count('#')
  if S[i][W-1] == '.':
    x =[S[i-1][W-1],S[i-1][W-2],  S[i][W-2], S[i+1][W-1], S[i+1][W-2]]
    S[i][W-1] = x.count('#')

if S[0][0] == '.':
  x = [S[0][1], S[1][0], S[1][1]]
  S[0][0] =x.count('#')
if S[H-1][0] == '.':
  x = [S[H-1][1], S[H-2][0], S[H-2][1]]
  S[H-1][0] = x.count('#')
if S[0][W-1] == '.':
  x = [S[0][W-2], S[1][W-1], S[1][W-2]]
  S[0][W-1] = x.count('#')
if S[H-1][W-1] == '.':
  x = [S[H-1][W-2], S[H-2][W-1], S[H-2][W-2]]
  S[H-1][W-1] = x.count('#')

for i in range(len(S)):
  S[i] = [str(x) for x in S[i]]
  print(''.join(S[i]))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())

l=[list(input()) for i in range(h)]
ans=[[0]*w for i in range(h)]

for i in range(h):
  for j in range(w):
    if l[i][j]=="#":
      ans[i][j]=="#"
    else:
      for a in range(-1,2):
        for b in range(-1,2):
          if i+a<h and j+b<w ansl[i+a][j+b]=="#":
            ans[i][j]+=1
            
for i in range(h):
  print(*ans[i])

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
l = [["."]*(w+2)]
for i in range(h):
    l.append(["."] + [i for i in input()] + ["."])
l.append(["."]*(w+2))
 
def count(i,j):
    if l[i][j] == "#":
        return "#"
    c = 0
    for n in range(i-1,i+2):
        for m in range(j-1,j+2):
            if l[n][m] == "#":
                c += 1
    return str(c)
 
ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]
 
print("\n".join(ans))
h,w = map(int,input().split())
l = [["."]*(w+2)]
for i in range(h):
    l.append(["."] + [i for i in input()] + ["."])
l.append(["."]*(w+2))

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

ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]

print("\n".join(ans))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
l=[["."*(w+2)]]
for i in range(h):
  l.append(["."]+[i for i in input()]+["."])
l.append(["."]*(w+2))

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

ans=["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]

print("\n".join(ans))

Submission status: Runtime Error

Solution code:
h,w = map(int, input().split())
s = []
for _ in range(h):
	s.append(list(input()))
           
for i in range(len(s)):
	if i == 0:
		for j in range(w):
			if j == 0:
				s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i+1][j],s[i+1][j+1]].count("#")))
			elif j == w-1:
				s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i+1][j],s[i+1][j-1]].count("#")))
			else:
				s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i+1][j],s[i+1][j+1],s[i+1][j-1]].count("#")))
	elif i == h-1:
		for j in range(w):
			if j == 0:
				s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i-1][j],s[i-1][j+1]].count("#")))
			elif j == w-1:
				s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i-1][j],s[i-1][j-1]].count("#")))
			else:
				s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i-1][j],s[i-1][j+1],s[i-1][j-1]].count("#")))
	else:
		for j in range(w):
			if j == 0:
				s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i-1][j],s[i-1][j+1],[i+1][j],s[i+1][j+1]].count("#")))
			elif j == w-1:
				s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i-1][j],s[i-1][j-1],s[i+1][j],s[i+1][j-1]].count("#")))
			else:
				s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i-1][j],s[i-1][j+1],s[i-1][j-1],s[i+1][j],s[i+1][j+1],s[i+1][j-1]].count("#")))

for k in s:
  print("".join(k))

Submission status: Runtime Error

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

for h in range(H):
  output = ""
  for w in range(W):
    if S[h][w] == "#":
      output += "#"
    else:
      count = 0
      
      if w == 0:
        count += 1 if S[h][1]=="#" else 0
        if h != 0:
          count += 1 if S[h-1][1]=="#" else 0
          count += 1 if S[h-1][0]=="#" else 0
        if h+1 != H:
          count += 1 if S[h+1][1]=="#" else 0
          count += 1 if S[h+1][0]=="#" else 0
          
      elif w == W-1:
        count += 1 if S[h][-2]=="#" else 0
        if h != 0:
          count += 1 if S[h-1][-2]=="#" else 0
          count += 1 if S[h-1][-1]=="#" else 0
        if h+1 != H:
          count += 1 if S[h+1][-2]=="#" else 0
          count += 1 if S[h+1][-1]=="#" else 0
          
      else:
        count += S[h].count("#", w-1, w+2)
        if h != 0:
          count += S[h-1].count("#", w-1, w+2)
        if h != H-1:
          count += S[h+1].count("#", w-1, w+2)
        
        
      output += str(count)
  print(f'{output}')

Submission status: Runtime Error

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

for h in range(H):
  output = ""
  for w in range(W):
    if S[h][w] == "#":
      output += "#"
    else:
      count = 0
      
      if w == 0:
        count += 1 if S[h][1]=="#" else 0
        if h != 0:
          count += 1 if S[h-1][1]=="#" else 0
          count += 1 if S[h-1][0]=="#" else 0
        if h+1 != H:
          count += 1 if S[h+1][1]=="#" else 0
          count += 1 if S[h+1][0]=="#" else 0
      else:
        count += S[h].count("#", w-1, w+2)
        if h != 0:
          count += S[h-1].count("#", w-1, w+2)
        if h+1 != H:
          count += S[h+1].count("#", w-1, w+2)
        
        
      output += str(count)
  print(f'{output}')

Submission status: Runtime Error

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

for h in range(H):
  output = ""
  for w in range(W):
    if S[h][w] == "#":
      output += "#"
    else:
      count = 0
      
      if w == 0:
        count += 1 if S[h][1]=="#" else 0
        if h != 0:
	        count += 1 if S[h-1][1]=="#" else 0
        if h+1 != H:
	        count += 1 if S[h+1][1]=="#" else 0
        
      else:
        count += S[h].count("#", w-1, w+2)
        if h != 0:
        	count += S[h-1].count("#", w-1, w+2)
        if h+1 != H:
       		count += S[h+1].count("#", w-1, w+2)
        
        
      output += str(count)
  print(f'{output}')

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
masu = [[0 for i in range(w)] for j in range(h)]
ans = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    line = input()
    for j in range(w):
        masu[i][j] = line[j]

for i in range(h):
    for j in range(w):
        if masu[i][j] == "#":
            ans[i-1][j-1] += 1
            ans[i][j-1] += 1
            ans[i+1][j-1] += 1
            ans[i-1][j] += 1
            ans[i+1][j] += 1
            ans[i-1][j+1] += 1
            ans[i][j+1] += 1
            ans[i+1][j+1] += 1
            
for i in range(h):
    for j in range(w):
        if masu[i][j] == "#":
            ans[i][j] = "#"

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

Submission status: Runtime Error

Solution code:
H, W = map(int,input().split(' '))
S = []
for i in range(H):
  S[i] = input()
for j in range(H):
  cnt = 0
  for k in range(W):
    if S[j][k] == '.'
      if S[j-1][k-1] == '#':
        cnt += 1
      if S[j-1][k] == '#':
        cnt += 1
      if S[j-1][k+1] == '#':
        cnt += 1
      if S[j][k-1] == '#':
        cnt += 1
      if S[j][k+1] == '#':
        cnt += 1
      if S[j+1][k-1] == '#':
        cnt += 1
      if S[j+1][k] == '#':
        cnt += 1
      if S[j][k+1] == '#':
        cnt += 1
      S[j][k].replace(cnt)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
sl=[]
for i in range(h):
    s=input()
    sl.append(s)
ans=[[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        if sl[i][j]==".":
            if i==0:
                if j==0:
                    if sl[0][1]=="#":
                        ans[i][j]+=1
                    if sl[1][0]=="#":
                        ans[i][j]+=1
                    if sl[1][1]=="#":
                        ans[i][j]+=1
                    
                elif j==w-1:
                    if sl[0][w-2]=="#":
                        ans[i][j]+=1
                    if sl[1][w-1]=="#":
                        ans[i][j]+=1
                    if sl[1][w-2]=="#":
                        ans[i][j]+=1
                else:
                    if sl[i][j-1]=="#":
                        ans[i][j]+=1
                    if sl[i][j+1]=="#":
                        ans[i][j]+=1
                    if sl[i+1][j-1]=="#":
                        ans[i][j]+=1
                    if sl[i+1][j]=="#":
                        ans[i][j]+=1
                    if sl[i+1][j+1]=="#":
                        ans[i][j]+=1

            elif i==h-1:
                if j==0:
                    if sl[h-2][0]=="#":
                        ans[i][j]+=1
                    if sl[h-1][1]=="#":
                        ans[i][j]+=1
                    if sl[h-2][1]=="#":
                        ans[i][j]+=1
                elif j==w-1:
                    if sl[h-1][w-2]=="#":
                        ans[i][j]+=1
                    if sl[h-2][w-1]=="#":
                        ans[i][j]+=1
                    if sl[h-2][w-2]=="#":
                        ans[i][j]+=1
                else:
                    if sl[h-1][j-1]=="#":
                        ans[i][j]+=1
                    if sl[h-1][j+1]=="#":
                        ans[i][j]+=1
                    if sl[h-2][j-1]=="#":
                        ans[i][j]+=1
                    if sl[h-2][j]=="#":
                        ans[i][j]+=1
                    if sl[h-2][j+1]=="#":
                        ans[i][j]+=1
            elif i!=0 and j==0:
                if sl[i-1][j]=="#":
                    ans[i][j]+=1
                if sl[i+1][j]=="#":
                    ans[i][j]+=1
                if sl[i][j+1]=="#":
                    ans[i][j]+=1
                if sl[i-1][j+1]=="#":
                    ans[i][j]+=1
                if sl[i+1][j+1]=="#":
                    ans[i][j]+=1
            elif i!=0 and j==w-1:
                if sl[i-1][j]=="#":
                    ans[i][j]+=1
                if sl[i+1][j]=="#":
                    ans[i][j]+=1
                if sl[i][j-1]=="#":
                    ans[i][j]+=1
                if sl[i-1][j-1]=="#":
                    ans[i][j]+=1
                if sl[i+1][j-1]=="#":
                    ans[i][j]+=1 
            else:
                if sl[i-1][j]=="#":
                    ans[i][j]+=1
                if sl[i-1][j-1]=="#":
                    ans[i][j]+=1
                if sl[i-1][j+1]=="#":
                    ans[i][j]+=1
                if sl[i][j-1]=="#":
                    ans[i][j]+=1
                if sl[i][j+1]=="#":
                    ans[i][j]+=1
                if sl[i+1][j-1]=="#":
                    ans[i][j]+=1
                if sl[i+1][j]=="#":
                    ans[i][j]+=1
                if sl[i+1][j+1]=="#":
                    ans[i][j]+=1
        else:
            ans[i][j]="#"
for i in range(h):
    for j in range(w):
        print(ans[i][j],end="")
    print()

Submission status: Runtime Error

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        cnt = 0
        for t in range(8):
            ni=i+dx[t]
            nj=j+dy[t]
            if ni<0 or h<=ni: continue
            if nj<0 or h<=nj: continue
            if s[ni][nj] == '#':
                cnt += 1
        
        s[i][j] = str(cnt)

for i in s:
    print(''.join(i))

Submission status: Runtime Error

Solution code:
Y_DIR[-1,0,1]
X_DIR[-1,0,1]
def count_bomb(y,x):
  cnt=0
  for yd in Y_DIR:
    yy=y+yd
    if not (0<=yy<h):
      continue
    for xd in X_DIR:
      xx=x+xd
      if not (0<=xx<w):
        continue
      if s[yy][xx]=="#":
        cnt+=1
  return cnt

h,w=map(int,input().split())
s=[]
for i in range(h):
  s.append(input())
for y in range(h):
  for x in range(w)
    if s[y][x] == ".":
      s[y][x]=str(count_bomb(y,x))

print("\n".join(s))

Submission status: Runtime Error

Solution code:
#6
h,w = map(int,input().split())
s = []
ans = [[] for i in range(h)]
for i in range(h):
    s.append(input())
    
cnt = 0
for i in range(h):
    for j in range(w):
        if s[i][j] !="#":
            if i == 0 :              #上底のコーナーケース
                if j == 0 :
                    for m in range(2):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:
                    for m in range(2):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:
                    for m in range(2):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            elif i == h-1 :              #下底のコーナーケース
                if j == 0 :
                    for m in range(h-2,h):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:
                    for m in range(h-2,h):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:
                    for m in range(h-2,h):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            else:
                if j == 0:             #上底下底以外の左端列
                    for m in range(i-1,i+2):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:           #上底下底以外の右端列
                    for m in range(i-1,i+2):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:                  #その他中央部
                    for m in range(i-1,i+2):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            ans[i].append(str(cnt))
            cnt = 0
        else:
            ans[i].append("#")
        
for i in ans:
    print("".join(i))

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    H, W = map(int, input().split())
    S = ['.' * W] + [input() for _ in range(H)] + ['.' * W]
    for i in range(1, H + 1):
        code = ""
        for j in range(W):
            if S[i][j] == '#':
                code += '#'
                continue
            if j == 0:
                string = S[i - 1][:2] + S[i][1] + S[i + 1][:2]
                cnt = string.count('#')
                code += str(cnt)
                continue
            if j == W - 1:
                string = S[i - 1][-2:] + S[i][-2] + S[i + 1][-2:]
                cnt = string.count('#')
                code += str(cnt)
                continue
            string = S[i - 1][j - 1:j + 2] + S[i][j - 1] + S[i][j + 1] + S[i + 1][j - 1:j + 2]
            cnt = string.count('#')
            code += str(cnt)
            continue
        print(code)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

for row in range(H):
    for col in range(W):
        if S[row][col] == '#':
            continue
        else:
            if row == 0:
                if col == 0:
                    S[row][col] = str([S[row][col+1], S[row+1][col], S[row+1][col+1]].count('#'))
                elif col == W - 1:
                    S[row][col] = str([S[row][col-1], S[row+1][col-1], S[row+1][col]].count('#'))
                else:
                    S[row][col] = str([S[row][col-1], S[row][col+1], S[row+1][col-1],
                               S[row+1][col], S[row+1][col+1]].count('#'))
            elif row == H - 1:
                if col == 0:
                    S[row][col] = str([S[row-1][col], S[row-1][col+1], S[row][col+1]].count('#'))
                elif col == W - 1:
                    S[row][col] = str([S[row-1][col-1], S[row-1][col], S[row][col-1]].count('#'))
                else:
                    S[row][col] = str([S[row-1][col-1], S[row-1][col], S[row-1][col+1],
                                   S[row][col-1], S[row][col+1]].count('#'))
            else:
                if col == 0:
                    S[row][col] = str([S[row-1][col], S[row-1][col+1],
                                   S[row][col+1],
                                   S[row+1][col], S[row+1][col+1]].count('#'))
                elif col == W - 1:
                    S[row][col] = str([S[row-1][col-1], S[row-1][col],
                                   S[row][col-1], 
                                   S[row+1][col-1], S[row+1][col]].count('#'))
                else:
                    S[row][col] = str([S[row-1][col-1], S[row-1][col], S[row-1][col+1],
                                   S[row][col-1], S[row][col+1],
                                   S[row+1][col-1], S[row+1][col], S[row+1][col+1]].count('#'))
for i in range(len(S)):
    print(''.join(S[i]))

Submission status: Runtime Error

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

for row in range(H):
    for col in range(W):
        if S[row][col] == '#':
            continue
        else:
            if row == 0:
                if col == 0:
                    S[row][col] = [S[row][col+1], S[row+1][col], S[row+1][col+1]].count('#')
                elif col == W - 1:
                    S[row][col] = [S[row][col-1], S[row+1][col-1], S[row+1][col]].count('#')
                else:
                    S[row][col] = [S[row][col-1], S[row][col+1], S[row+1][col-1],
                               S[row+1][col], S[row+1][col+1]].count('#')
            elif row == H - 1:
                if col == 0:
                    S[row][col] = [S[row-1][col], S[row-1][col+1], S[row][col+1]].count('#')
                elif col == W - 1:
                    S[row][col] = [S[row-1][col-1], S[row-1][col], S[row][col-1]].count('#')
                else:
                    S[row][col] = [S[row-1][col-1], S[row-1][col], S[row-1][col+1],
                               S[row][col-1], S[row][col+1]].count('#')
            else:
                if col == 0:
                    S[row][col] = [S[row-1][col], S[row-1][col+1],
                                   S[row][col+1],
                                   S[row+1][col], S[row+1][col+1]].count('#')
                elif col == W - 1:
                    S[row][col] = [S[row-1][col-1], S[row-1][col],
                                   S[row][col-1], 
                                   S[row+1][col-1], S[row+1][col]].count('#')
                else:
                    S[row][col] = [S[row-1][col-1], S[row-1][col], S[row-1][col+1],
                                   S[row][col-1], S[row][col+1],
                                   S[row+1][col-1], S[row+1][col], S[row+1][col+1]].count('#')
for i in range(len(S)):
    print(*S[i])

Submission status: Runtime Error

Solution code:
import copy
H,W=map(int,input().split())
List = []
for i in range (H):
  List.append(input())
resList = copy.deepcopy(List)

def bombCheck(x,y,ListX):  
  a = 0
  b = 0
  res = 0
  if ListX[x][y] == "#":
    return "#"
  for i in range(-1,2,1):
    a = x+i
    if a<0 or a >= W:
      pass
    else:
      for j in range(-1,2,1):
        b = y+j
        if b<0 or b >= H:
          pass
        else:
          if i == 0 and j ==0:
            pass
          else:
            if List[a][b] == "#":
              res +=1
            else:
              pass
  return str(res)

for i in range(H):
  resList[i] =""
  for j in range(W):
    resList[i] += bombCheck(i,j,List)
for i in resList:
  print(i)