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:
#全日本汚いコード選手権Pythonの部

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

if S[0][1]=="#":
  L[0][0]+=1
if S[1][0]=="#":
  L[0][0]+=1
if S[1][1]=="#":
  L[0][0]+=1
if S[H-1][1]=="#":
  L[H-1][0]+=1
if S[H-2][0]=="#":
  L[H-1][0]+=1
if S[H-2][1]=="#":
  L[H-1][0]+=1
if S[0][W-2]=="#":
  L[0][W-1]+=1
if S[1][W-1]=="#":
  L[0][W-1]+=1
if S[1][W-2]=="#":
  L[0][W-1]+=1
if S[H-1][W-2]=="#":
  L[H-1][W-1]+=1
if S[H-2][W-1]=="#":
  L[H-1][W-1]+=1
if S[H-2][W-2]=="#":
  L[H-1][W-1]+=1

for w in range(1,W-1):
  if S[0][w-1]=="#":
    L[0][w]+=1
  if S[0][w+1]=="#":
    L[0][w]+=1
  if S[1][w-1]=="#":
    L[0][w]+=1
  if S[1][w]=="#":
    L[0][w]+=1
  if S[1][w+1]=="#":
    L[0][w]+=1
  if S[H-1][w-1]=="#":
    L[H-1][w]+=1
  if S[H-1][w+1]=="#":
    L[H-1][w]+=1
  if S[H-2][w-1]=="#":
    L[H-1][w]+=1
  if S[H-2][w]=="#":
    L[H-1][w]+=1
  if S[H-2][w+1]=="#":
    L[H-1][w]+=1

for h in range(1,H-1):
  if S[h-1][0]=="#":
    L[h][0]+=1
  if S[h+1][0]=="#":
    L[h][0]+=1
  if S[h-1][1]=="#":
    L[h][0]+=1
  if S[h][1]=="#":
    L[h][0]+=1
  if S[h+1][1]=="#":
    L[h][0]+=1
  if S[h-1][W-1]=="#":
    L[h][W-1]+=1
  if S[h+1][W-1]=="#":
    L[h][W-1]+=1
  if S[h-1][W-2]=="#":
    L[h][W-1]+=1
  if S[h][W-2]=="#":
    L[h][W-1]+=1
  if S[h+1][W-2]=="#":
    L[h][W-1]+=1

for w in range(1,W-1):
  for h in range(1,H-1):
    if S[h-1][w-1]=="#":
      L[h][w]+=1
    if S[h-1][w]=="#":
      L[h][w]+=1
    if S[h-1][w+1]=="#":
      L[h][w]+=1
    if S[h][w-1]=="#":
      L[h][w]+=1
    if S[h][w+1]=="#":
      L[h][w]+=1
    if S[h+1][w-1]=="#":
      L[h][w]+=1
    if S[h+1][w]=="#":
      L[h][w]+=1
    if S[h+1][w+1]=="#":
      L[h][w]+=1

C = [["" for w in range(W)] for h in range(H)]

for w in range(W):
  for h in range(H):
    if S[h][w]==".":
      C[h][w]=str(L[h][w])
    else:
      C[h][w]="#"

o = ""
for c in C:
  o = ""
  for cc in c:
    o += cc
  print(o)

Submission status: Runtime Error

Solution code:
#全日本汚いコード選手権Pythonの部

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

if S[0][1]=="#":
  L[0][0]+=1
if S[1][0]=="#":
  L[0][0]+=1
if S[1][1]=="#":
  L[0][0]+=1
if S[W-1][1]=="#":
  L[W-1][0]+=1
if S[W-2][0]=="#":
  L[W-1][0]+=1
if S[W-2][1]=="#":
  L[W-1][0]+=1
if S[0][H-2]=="#":
  L[0][H-1]+=1
if S[1][H-1]=="#":
  L[0][H-1]+=1
if S[1][H-2]=="#":
  L[0][H-1]+=1
if S[W-1][H-2]=="#":
  L[W-1][H-1]+=1
if S[W-2][H-1]=="#":
  L[W-1][H-1]+=1
if S[W-2][H-2]=="#":
  L[W-1][H-1]+=1

for w in range(1,W-1):
  if S[0][w-1]=="#":
    L[0][w]+=1
  if S[0][w+1]=="#":
    L[0][w]+=1
  if S[1][w-1]=="#":
    L[0][w]+=1
  if S[1][w]=="#":
    L[0][w]+=1
  if S[1][w+1]=="#":
    L[0][w]+=1
  if S[H-1][w-1]=="#":
    L[H-1][w]+=1
  if S[H-1][w+1]=="#":
    L[H-1][w]+=1
  if S[H-2][w-1]=="#":
    L[H-1][w]+=1
  if S[H-2][w]=="#":
    L[H-1][w]+=1
  if S[H-2][w+1]=="#":
    L[H-1][w]+=1

for h in range(1,H-1):
  if S[h-1][0]=="#":
    L[h][0]+=1
  if S[h+1][0]=="#":
    L[h][0]+=1
  if S[h-1][1]=="#":
    L[h][0]+=1
  if S[h][1]=="#":
    L[h][0]+=1
  if S[h+1][1]=="#":
    L[h][0]+=1
  if S[h-1][W-1]=="#":
    L[h][W-1]+=1
  if S[h+1][W-1]=="#":
    L[h][W-1]+=1
  if S[h-1][W-2]=="#":
    L[h][W-1]+=1
  if S[h][W-2]=="#":
    L[h][W-1]+=1
  if S[h+1][W-2]=="#":
    L[h][W-1]+=1

for w in range(1,W-1):
  for h in range(1,H-1):
    if S[h-1][w-1]=="#":
      L[h][w]+=1
    if S[h-1][w]=="#":
      L[h][w]+=1
    if S[h-1][w+1]=="#":
      L[h][w]+=1
    if S[h][w-1]=="#":
      L[h][w]+=1
    if S[h][w+1]=="#":
      L[h][w]+=1
    if S[h+1][w-1]=="#":
      L[h][w]+=1
    if S[h+1][w]=="#":
      L[h][w]+=1
    if S[h+1][w+1]=="#":
      L[h][w]+=1

C = [["" for w in range(W)] for h in range(H)]

for w in range(W):
  for h in range(H):
    if S[h][w]==".":
      C[h][w]=str(L[h][w])
    else:
      C[h][w]="#"

o = ""
for c in C:
  o = ""
  for cc in c:
    o += cc
  print(o)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
S = []
for i in range(H):
  S.append(list(input()))
  
if H == 1:
  if W == 1:
    if S[0][0] == '.':
      S[0][0] = str(0)
    print("".join(S[0]))
    exit()
  if W == 2:
    if S[0][0] == '.' and S[0][1] == '.':
      S[0][0] = str(0)
      S[0][1] = str(0)
    elif S[0][0] == '.' and S[0][1] == '#':
      S[0][0] = str(1)
    elif S[0][0] == '#' and S[0][1] == '.':
      S[0][1] = str(1)
    else:
      pass
    print("".join(S[0]))
    exit()
  
  if W > 2:
    for i in range(W):
      if S[0][i] == '.':
        if i == 0:
          S[0][i] = str(int(S[0][i+1] == '#'))
        elif 0 < i < W:
          S[0][i] = str(int(S[0][i-1] == '#') + int(S[0][i+1] == '#'))
        elif i == W-1:
          S[0][i] = str(int(S[0][i-1] == '#'))

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

    exit()
    
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      if i == 0:
        if j == 0:
          cnt = S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif 0<i<H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#')+S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#')+ S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif i == H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#') + S[i][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#') + S[i][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#')
          S[i][j] = str(cnt)

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

Submission status: Runtime Error

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

for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      if i == 0:
        if j == 0:
          cnt = S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif 0<i<H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#')+S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#')+ S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif i == H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#') + S[i][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#') + S[i][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#')
          S[i][j] = str(cnt)

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

Submission status: Runtime Error

Solution code:
# ABC075B - Minesweeper
def check_neighbors(i: int, j: int) -> int:
    # count the number of landmines adjacent to S[i][j]
    ret = 0
    for x, y in neighbors:
        a, b = i + x, j + y
        if 0 <= a < H and 0 <= b < W:
            ret += 1 if S[a][b] == "#" else 0
    return ret


def main():
    global S, neighbors
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    neighbors = [(-1, -1), (-1, 0), (-1, 1), (0, -1)]
    neighbors += [(0, 1), (1, -1), (1, 0), (1, 1)]
    A = [""] * H
    for i, s in enumerate(S):
        for j, a in enumerate(s):
            if a == "#":
                A[i] += "#"
            else:
                cnt = check_neighbors(i, j)
                A[i] += str(cnt)
    print(*A, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
mine_map = [list(input().split()) for x in range(H)]
for i in range(H):
  for j in range(W):
    count = 0
    if mine_map[i][j] == ".":
      for k in range(-1,2):
        for l in range(-1,2):
          if i+k >= 0 and i+k < H and j+l >= 0 and j+l < W:
            if mine_map[i+k][j+l] == "#":
              count += 1
      mine_map[i][j] = count
    print(mine_map[i][j],end="")
  print()

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
mine_map = [list(input().split()) for x in range(H)]
for i in range(H):
  for j in range(W):
    count = 0
    if mine_map[i][j] == ".":
      for k in range(-1,2):
        for l in range(-1,2):
          if i+k >= 0 and i+k < H and j+l >= 0 and j+l < W:
            if mine_map[i+k][j+l] == "#":
              count += 1
      mine_map[i][j] = count
    print(mine_map[i][j],end="")
  print("")

Submission status: Runtime Error

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

d = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            cnt = 0
            for r in range(8):
                x = i + d[r][0]
                y = j + d[r][1]
                if 0 <= x < W and 0 <= y < H and S[x][y] == '#':
                    cnt = cnt + 1
            S[i][j] = str(cnt)

for i in range(H):
    print(''.join(S[i]))

Submission status: Runtime Error

Solution code:
h = int(input())
w = int(input())
s = [list(input() for i in range(h))]
t = s.copy()


number = []
cnt = 0

for i in range(h) :
  if 1 <= i and i <= h-2 :
    for j in range(w) :
      if j >= 1 and j < w-1 :
        number = [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]]
        cnt = number.count('#')
        t[i][j] = cnt
        
      elif j == 0 :
        number = [s[i-1][0],s[i+1][0],s[i-1][1],s[i][1],s[i+1][1]]
        cnt = number.count('#')
        t[i][0] = cnt
      
      elif j == w-1 :
        number = [s[i-1][w-1],s[i+1][w-1],s[i-1][w-2],s[i][w-2],s[i+1][w-2]]
        cnt = number.count('#')
        t[i][w-1] = cnt       
  
  elif i == 0 :
    for j in range(w) :
      if 1 <= j and j <= w-2 :
        number = [s[0][j-1],s[0][j+1],s[1][j-1],s[1][j],s[1][j+1]]
        cnt = number.count('#') 
        t[0][j] = cnt
      
      elif j == 0 :
        number = [s[0][1],s[1][0],s[1][1]]
        cnt = number.count('#') 
        t[0][0] = cnt
        
      elif j == w-1 :
        number = [s[0][w-2],s[1][w-2],s[1][w-1]]
        cnt = number.count('#') 
        t[0][w-1] = cnt        
  
  elif i == h-1 :
    for j in range(w) :
      if 1 <= j and j <= w-2 :
        number = [s[h-1][j-1],s[h-1][j+1],s[h-2][j-1],s[h-2][j],s[h-2][j+1]]
        cnt = number.count('#')
        t[h-1][j] = cnt
                
      elif j == 0 :
        number = [s[h-1][1],s[h-2][1],s[h-2][0]]
        cnt = number.count('#') 
        t[h-1][0] = cnt
        
      elif j == w-1 :
        number = [s[h-2][w-1],s[h-1][w-2],s[h-2][w-2]]
        cnt = number.count('#') 
        t[h-1][w-1] = cnt

for i in range(h):
  for j in range(w):
    if s[i][j] == '#':
      t[i][j] = '#'
    
  
a0 = map(str,t[0])
a1 = map(str,t[1])
a2 = map(str,t[2])
print(''.join(a0))
print(''.join(a1))
print(''.join(a2))

Submission status: Runtime Error

Solution code:
h = int(input())
w = int(input())
s = list(input() for i in range(1,h+1)
number = []
cnt = 0

for i in range(1,h+1) :
  if i != 1 and i != h :
    for j in range(1,w+1) :
      if j != 1 and j != w :
        number = [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]]
        cnt = number.count('#')
        s[i][j] = cnt
  
  elif i == 1 :
    for j in range(1,w+1) :
      if j != 1 and j != w :
        number = [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]]
        cnt = number.count('#') 
        s[i][j] = cnt
      
      elif j == 1 :
        number = [s[1][2],s[2][1],s[2][2]]
        cnt = number.count('#') 
        s[1][1] = cnt
        
      elif j == w :
        number = [s[1][w-1],s[2][w-1],s[2][w]]
        cnt = number.count('#') 
        s[1][w] = cnt        
  
  elif i == h :
    for j in range(1,w+1) :
      if j == 1 :
        number = [s[h-1][1],s[h-1][2],s[h][2]]
        cnt = number.count('#') 
        s[h][1] = cnt
        
      elif j == w :
        number = [s[h-1][w-1],s[h-1][w],s[h][w-1]]
        cnt = number.count('#') 
        s[h][w] = cnt
        
print(''.join(s))

Submission status: Runtime Error

Solution code:
N=int(input())
v=[int(x) for x in input().split()]
cnt=0
while cnt<N-1:
    v.sort()
    v[0]=(v[0]+v.pop(1))/2
    cnt+=1
print(v[0])

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[input() for i in range(h)]
for i in range(h):
  l=""
  for j in range(w):
    if s[i][j]=="#":
      l+="#"
    else:
      l+=str(sum(t[max(0,j-1):min(w,j+2)].count("#") for t in t[max(0,i-1):min(h,i+2)]))
    print(l)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '#':
            if w == 1:
                if i == 0:
                    count = s[1][0].count('#')
                elif i == h-1:
                    count = s[-2][0].count('#')
                else:
                    count = s[i-1][0].count('#') + s[i+1][0].count('#')
            elif h == 1:
                if j == 0:
                    count = m[1].count('#')
                elif j == w-1:
                    count = m[-2].count('#')
                else:
                    count = m[j-1].count('#') + m[j+1].count('#')
            else:
                if i == 0:
                    if j == 0:
                        count = s[i+1][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[i+1][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
                elif i == h-1:
                    if j == 0:
                        count = s[-2][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[-2][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i-1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
                else:
                    if j == 0:
                        count = s[i-1][:2].count('#') + s[i+1][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[i-1][-2:].count('#') + s[i+1][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i-1][j-1:j+2].count('#') + s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            m = m.replace(m[j], str(count), 1)
    print(m)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '#':
            if i == 0:
                if j == 0:
                    count = s[i+1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i+1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            elif i == h-1:
                if j == 0:
                    count = s[-2][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[-2][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i-1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            else:
                if j == 0:
                    count = s[i-1][:2].count('#') + s[i+1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i-1][-2:].count('#') + s[i+1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i-1][j-1:j+2].count('#') + s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            m = m.replace(m[j], str(count), 1)
    print(m)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '#':
            if i == 0:
                if j == 0:
                    count = s[i+1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i+1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i+1][j-1:j+2].count('#') + m[j+1].count('#') + m[j-1].count('#')
            elif i == h-1:
                if j == 0:
                    count = s[i-1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i-1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i-1][j-1:j+2].count('#') + m[j+1].count('#') + m[j-1].count('#')
            else:
                if j == 0:
                    count = s[i-1][:2].count('#') + s[i+1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i-1][-2:].count('#') + s[i+1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i-1][j-1:j+2].count('#') + s[i+1][j-1:j+2].count('#') + m[j-1].count('#') + m[j+1].count('#')
            m = m.replace(m[j], str(count), 1)
    print(*m, sep='')

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = [[0] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == '.':
            if i == 0:
                if s[i+1][j] == '#':
                    count += 1
                if j == 0:
                    if s[i][j+1] == '#':
                        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
                else:
                    if s[i][j+1] == '#':
                        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
            elif i == h-1:
                if s[i-1][j] == '#':
                    count += 1
                if j == 0:
                    if s[i][j+1] == '#':
                        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
                else:
                    if s[i][j+1] == '#':
                        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
            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+1] == '#':
                        count += 1
                    if s[i+1][j] == '#':
                        count += 1
                elif 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
                    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
            ans[i][j] = str(count)
        else:
            ans[i][j] = '#'

for a in ans:
    print(''.join(map(str, a)))

Submission status: Runtime Error

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

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'a
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if not (I < 0 or H <= I):
                    for J in (j-1, j, j+1):H, W = map(int, input().split())

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if not (I < 0 or H <= I):
                    for J in (j-1, j, j+1):
                        if not (J < 0 or W <= J):
                            if INPUT[I][J] == '#':
                                OUTPUT[i][j] += 1


for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

                        if not (J < 0 or W <= J):
                            if INPUT[I][J] == '#':
                                OUTPUT[i][j] += 1


for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Submission status: Runtime Error

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

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'a
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if not (I < 0 or H <= I):
                    for J in (j-1, j, j+1):
                        if not (J < 0 or W <= J):
                            if INPUT[I][J] == '#':
                                OUTPUT[i][j] += 1


for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
 
INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()
 
for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'
            continue
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if (I < 0 or l <= H):
                    continue
                for J in (j-1, j, j+1):
                    if (J < 0 or W <= J):
                        continue
                    if INPUT[I][J] == '#':
                        OUTPUT[i][j] += 1
 
 
for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
 
INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()
 
for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if not (I < 0 or l <= H):
                    for J in (j-1, j, j+1):
                        if not (J < 0 or W <= J):
                            if INPUT[I][J] == '#':
                                OUTPUT[i][j] += 1
 
 
for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Submission status: Runtime Error

Solution code:
gyou,retu=map(int,input().split())
l=[input() for _ in range(gyou)]
d=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for g in range(gyou):
  preans=str()
  for r in range(retu):
    s=0
    if l[g][r]=="#":
      preans += "#"
    else:
      for x in range(8):
        a,b=*d[x]
        try:
          if l[g+a][r+b]=="#" and g+a>=0 and r+b>=0:
            s+=1
        except IndexError:
          s+=0
      preans += str(s)
  print(preans)

Submission status: Runtime Error

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

def hantei(count,k,l):
    if s[k][l]=="#":
        count+=1
    return count

for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            count=0
            if i ==0:
                if j==0:
                    for k,l in zip([i,i+1,i+1],[j+1,j,j+1]):
                        count=hantei(count,k,l)
                elif j==w-1:
                    for k,l in zip([i,i+1,i+1],[j-1,j-1,j]):
                        count=hantei(count,k,l)
                else:
                    for k,l in zip([i,i,i+1,i+1,i+1],[j-1,j+1,j-1,j,j+1]):
                        count=hantei(count,k,l)

            elif i==h-1:
                if j==0:
                    for k,l in zip([i-1,i-1,i],[j,j+1,j+1]):
                        count=hantei(count,k,l)
                elif j==w-1:
                    for k,l in zip([i,i-1,i-1],[j-1,j-1,j]):
                        count=hantei(count,k,l)
                else:
                    for k,l in zip([i,i,i-1,i-1,i-1],[j-1,j+1,j-1,j,j+1]):
                        count=hantei(count,k,l)

            elif j==0:
                for k,l in zip([i-1,i-1,i,i+1,i+1],[j,j+1,j+1,j,j+1]):
                        count=hantei(count,k,l)
            elif j==w-1:
                for k,l in zip([i-1,i-1,i,i+1,i+1],[j-1,j,j-1,j-1,j]):
                        count=hantei(count,k,l)
            else:
                for k,l in zip([i-1,i-1,i-1,i,i,i+1,i+1,i+1],[j-1,j,j+1,j-1,j+1,j-1,j,j+1]):
                            count=hantei(count,k,l)

            s[i][j]=str(count)

for i in range(h):
    ss=""
    for j in range(w):
        ss+=s[i][j]
    print(ss)

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
S = [ input() for _ in range(H) ]
for h in range(H):
  L = ''
  for w in range(W):
    if S[h][w] == '#':
      L += '#'
    else:
      x = 0
      for dh in [ -1, 0, 1]:
        for dw in [-1, 0, 1]:
          if 0 <= h + dh < H and 0 <= w + dw < W and S[h+dh][w+dw] == '#':
            x += 1
      L += str(x)
  print(l)

Submission status: Runtime Error

Solution code:
for i in range(h):
    for j in range(w):
        if s[i][j] == "0":
            for k in range(8):
                if 0 <= j+y[k] < w and 0 <= i+x[k] < h and s[i+x[k]][j+y[k]] == "#":
                    s[i][j] = str(int(s[i][j])+1)
                    
for i in range(h):
    print("".join(s[i]))

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = map(int, input().split())
A = []
for i in range(H):
  A.append(input())
  
for i in range(H):
  for j in range(W):
    if A[i][j] == '#':
      print('#', end='')
    count = 0
    for k in range(min(0, i-1), max(i+1, H-1)+1):
      for l in range(min(0, j-1), max(j+1, W-1)+1):
        if A[k][l] == '#':
          count += 1
    print(count, end='')
  print()

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
S = [input() for i in range(H)]
A = [["#"]*W for i in range(H)]

for i in range(W):
    for j in range(H):
        if S[j][i]==".":
            if i ==0 and j ==0:
                A[j][i] =str(S[0][0:2].count("#")+S[1][0:2].count("#"))
            elif i ==W-1 and j ==0:
                A[j][i] =str(S[0][W-2:W].count("#")+S[1][W-2:W].count("#"))
            elif i ==0 and j ==H-1:
                A[j][i] =str(S[H-2][0:2].count("#")+S[H-1][0:2].count("#"))
            elif i ==W-1 and j ==H-1:
                A[j][i] =str(S[H-2][W-2:W].count("#")+S[H-1][W-2:W].count("#"))
            elif i ==0:
                A[j][i] =str(S[j-1][0:2].count("#")+S[j][0:2].count("#")+S[j+1][0:2].count("#"))
            elif i ==W-1:
                A[j][i] =str(S[j-1][W-2:W].count("#")+S[j][W-2:W].count("#")+S[j+1][W-2:W].count("#"))
            elif j ==0:
                A[j][i] =str(S[0][i-1:i+2].count("#")+S[1][i-1:i+2].count("#"))
            elif j ==H-1:
                A[j][i] =str(S[H-2][i-1:i+2].count("#")+S[H-1][i-1:i+2].count("#"))
            else:
                A[j][i] =str(S[j-1][i-1:i+2].count("#")+S[j][i-1:i+2].count("#")+S[j+1][i-1:i+2].count("#"))
           
                
for i in range(H):
    print("".join(A[i]))