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 = [list(input()) for _ in range(h)]
ans = [[""]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if i == 0:
            if s[i][j] == ".":
                ans[i][j] = s[i][max(0, j - 1):min(w + 1, j + 2)].count("#") + s[i + 1][
                                                                               max(0, j - 1):min(w + 1, j + 2)].count(
                    "#")
            else:
                ans[i][j] = "#"
        elif i == h-1:
            if s[i][j] == ".":
                ans[i][j] = s[i - 1][max(0, j - 1):min(w + 1, j + 2)].count("#") + s[i][max(0, j - 1):min(w + 1,
                                                                                                          j + 2)].count(
                    "#")
            else:
                ans[i][j] = "#"
        else:
            if s[i][j] == ".":
                ans[i][j] = s[i - 1][max(0, j - 1):min(w + 1, j + 2)].count("#") + s[i][max(0, j - 1):min(w + 1,
                                                                                                          j + 2)].count(
                    "#") + s[i + 1][max(0, j - 1):min(w + 1, j + 2)].count("#")
            else:
                ans[i][j] = "#"

[print(*k,sep="") for k in ans]

Submission status: Runtime Error

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

for i in range(h):
    for j in range(w):
        if i == 0:
            if s[i][j] == ".":
                ans[i][j] = s[i][max(0, j - 1):min(w + 1, j + 2)].count("#") + s[i + 1][
                                                                               max(0, j - 1):min(w + 1, j + 2)].count(
                    "#")
            else:
                ans[i][j] = "#"
        elif i == h-1:
            if s[i][j] == ".":
                ans[i][j] = s[i - 1][max(0, j - 1):min(w + 1, j + 2)].count("#") + s[i][max(0, j - 1):min(w + 1,
                                                                                                          j + 2)].count(
                    "#")
            else:
                ans[i][j] = "#"
        else:
            if s[i][j] == ".":
                ans[i][j] = s[i - 1][max(0, j - 1):min(w + 1, j + 2)].count("#") + s[i][max(0, j - 1):min(w + 1,
                                                                                                          j + 2)].count(
                    "#") + s[i + 1][max(0, j - 1):min(w + 1, j + 2)].count("#")
            else:
                ans[i][j] = "#"

[print(*k) for k in ans]

Submission status: Runtime Error

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

ans = [[0]*W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            ans[i][j] += 11

#横方向の確認
for i in range(H):
    for j in range(W - 1):
        if ans[i][j+1] > 10:
            ans[i][j] += 1
    for j in range(1, W):
        if ans[i][j-1] > 10:
            ans[i][j] += 1

#縦方向の確認
for j in range(1, W-1):
    for i in range(H-1):
        if ans[i+1][j-1] > 10:
            ans[i][j] += 1
        if ans[i+1][j] > 10:
            ans[i][j] += 1
        if ans[i+1][j+1] > 10:
            ans[i][j] += 1

    for i in range(1, H):
        if ans[i-1][j-1] > 10:
            ans[i][j] += 1
        if ans[i-1][j] > 10:
            ans[i][j] += 1
        if ans[i-1][j+1] > 10:
            ans[i][j] += 1

#j = 0
for i in range(H-1):
    if ans[i+1][0] > 10:
        ans[i][0] += 1
    if ans[i+1][1] > 10:
        ans[i][0] += 1
for i in range(1, H):
    if ans[i-1][0] > 10:
        ans[i][0] += 1
    if ans[i-1][1] > 10:
        ans[i][0] += 1

#j = W-1
for i in range(H-1):
    if ans[i+1][W-1] > 10:
        ans[i][W-1] += 1
    if ans[i+1][W-2] > 10:
        ans[i][W-1] += 1
for i in range(1, H):
    if ans[i-1][W-1] > 10:
        ans[i][W-1] += 1
    if ans[i-1][W-2] > 10:
        ans[i][W-1] += 1

for i in range(H):
    for j in range(W):
        if ans[i][j] > 10:
            ans[i][j] = "#"
    ans[i] = map(str, ans[i])
    ans[i] = "".join(ans[i])

for a in ans:
    print(a)

Submission status: Runtime Error

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

S = [list(input()) for _ in range(H)]
dydx = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]

def check(h, w):
  count = 0
  for dy, dx in dydx:
    if 0 <= h + dy < H and 0 <= w + dx < W and S[h+dy][w+dx] == '#':
      count += 1
  return count
      
  

for h in range(H):
  for w in range(W):
    if S[h][w] == '.':
      S[h][w] = check(h, w)
  print("".join(S[h])

Submission status: Runtime Error

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            if i == 0 and j == 0:
                t = 0
                if s[i][j+1] == '#':
                    t += 1
                if s[i+1][j] == '#':
                    t += 1
                if s[i+1][j+1] == '#':
                    t += 1
                s[i][j] = str(t)
            elif i == 0 and j == w - 1:
                t = 0
                if s[i][j-1] == '#':
                    t += 1
                if s[i+1][j-1] == '#':
                    t += 1
                if s[i+1][j] == '#':
                    t += 1
                s[i][j] = str(t)
            elif i == h - 1 and j == 0:
                t = 0
                if s[i-1][j] == '#':
                    t += 1
                if s[i-1][j+1] == '#':
                    t += 1
                if s[i][j+1] == '#':
                    t += 1
                s[i][j] = str(t)
            elif i == h - 1 and j == w - 1:
                t = 0
                if s[i-1][j-1] == '#':
                    t += 1
                if s[i-1][j] == '#':
                    t += 1
                if s[i][j-1] == '#':
                    t += 1
                s[i][j] = str(t)
            elif i == 0:
                t = 0
                if s[i][j-1] == '#':
                    t += 1
                if s[i][j+1] == '#':
                    t += 1
                if s[i+1][j-1] == '#':
                    t += 1
                if s[i+1][j] == '#':
                    t += 1
                if s[i+1][j+1] == '#':
                    t += 1
                s[i][j] = str(t)
            elif j == 0:
                t = 0
                if s[i-1][j] == '#':
                    t += 1
                if s[i-1][j+1] == '#':
                    t += 1
                if s[i][j+1] == '#':
                    t += 1
                if s[i+1][j] == '#':
                    t += 1
                if s[i+1][j+1] == '#':
                    t += 1
                s[i][j] = str(t)
            elif i == h - 1:
                t = 0
                if s[i-1][j-1] == '#':
                    t += 1
                if s[i-1][j] == '#':
                    t += 1
                if s[i-1][j+1] == '#':
                    t += 1
                if s[i][j-1] == '#':
                    t += 1
                if s[i][j+1] == '#':
                    t += 1
                s[i][j] = str(t)
            elif j == w - 1:
                t = 0
                if s[i-1][j-1] == '#':
                    t += 1
                if s[i-1][j] == '#':
                    t += 1
                if s[i][j-1] == '#':
                    t += 1
                if s[i+1][j-1] == '#':
                    t += 1
                if s[i+1][j] == '#':
                    t += 1
                s[i][j] = str(t)
            else:
                t = 0
                if s[i-1][j-1] == '#':
                    t += 1
                if s[i-1][j] == '#':
                    t += 1
                if s[i-1][j+1] == '#':
                    t += 1
                if s[i][j-1] == '#':
                    t += 1
                if s[i][j+1] == '#':
                    t += 1
                if s[i+1][j-1] == '#':
                    t += 1
                if s[i+1][j] == '#':
                    t += 1
                if s[i+1][j+1] == '#':
                    t += 1
                s[i][j] = str(t)

for i in range(h):
    print(''.join(s[i]))

Submission status: Runtime Error

Solution code:

H, W = map(int, input().split())
M = []
for h in range(H):
    col = []
    s = input()
    for w in range(W):
        if s[w] == '.':
            col.append(0)
        else:
            col.append(np.inf)
    M.append(col)



for h in range(H):
    for w in range(W):
        flag_h0= True
        flag_hM = True
        flag_w0 = True
        flag_wM = True
        if M[h][w] == np.inf:
            
            if h==0:
                flag_h0= False
            if h == H - 1:
                flag_hM = False
            if w== 0:
                flag_w0 = False
            if w== W-1:
                flag_wM = False
            
            if flag_h0:
                M[h-1][w] += 1
                if  flag_w0:
                    M[h-1][w-1] += 1
                if flag_wM:
                    M[h-1][w+1] += 1
            
            if flag_w0:
                M[h][w-1] += 1
            if flag_wM:
                M[h][w+1] += 1
            
            if flag_hM:
                M[h+1][w] += 1

                if flag_w0:
                    M[h+1][w-1] += 1
                if flag_wM:
                    M[h+1][w+1] += 1

                    
for h in range(H):
    tmp_ = ''
    for w in range(W):
        if M[h][w] < np.inf:
            tmp_ += str(M[h][w])
        else:
            tmp_  += '#'
    print(tmp_)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
grid = [['']*W for i in range(H)]
grid=['#####.','#.#.##','####.#','.#..#.','#.##..','#.#...']
s = list([['.']*W for i in range(H)])

for i in range(H):
    grid[i] = input()
    s[i] = list(grid[i])

for h in range(H):
    for w in range(W):
        count = 0
        if s[h][w] != '#':
            try:
                if s[h-1][w-1] == '#' and h!=0 and w!=0:
                    count+=1
            except:
                None
            try:
                if s[h-1][w] == '#' and h!=0:
                    count+=1
            except:
                None
            try:
                if s[h-1][w+1] == '#' and h!=0:
                    count+=1
            except:
                None
            try:
                if s[h][w-1] == '#' and w!=0:
                    count+=1
            except:
                None
            try:
                if s[h][w+1] == '#':
                    count+=1
            except:
                None
            try:
                if s[h+1][w-1] == '#':
                    count+=1
            except:
                None
            try:
                if s[h+1][w] == '#':
                    count+=1
            except:
                None
            try:
                if s[h+1][w+1] == '#':
                    count+=1
            except:
                None
            s[h][w] = str(count)

for h in range(H):
    print(''.join(s[h]))

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):
    if s[i][j] == "#": ans[i][j] = "#"
      for i2 in range(i-1, i+2):
        if i2 < 0 or h-1 < i2: continue
        for j2 in range(j-1, j+2):
          if (j2 < 0 or w-1 < j2): continue
          elif ans[i2][j2] == "#": continue
          ans[i2][j2] += 1
for i in ans:
  print("".join(map(str,i)))

Submission status: Runtime Error

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

Rmin = 0
Rmax = 0
Cmin = 0
Cmax = 0

#行方向への探索
for r in range(H):
    #列方向への検索
    for c in range(W):
        #爆弾の場合、周りを加算
        if S[r][c] == ".":
            S[r][c] = 0
        elif S[r][c] == "#":
            if r == 0 :
                Rmin = r
                Rmax = r+2
            elif r == H-1:
                Rmin = r-1
                Rmax = r+1
            else:
                Rmin = r-1
                Rmax = r+2

            if c == 0:
                Cmin = c
                Cmax = c+2
            elif c == W-1:
                Cmin = c-1
                Cmax = c+1
            else:
                Cmin = c-1
                Cmax = c+2
            for i in range(Rmin,Rmax):
                for t in range(Cmin,Cmax):
                    if S[i][t] == "." or S[i][t] == 0:
                        S[i][t] = 1
                    elif not S[i][t] == "#":
                        S[i][t] += 1

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

Submission status: Runtime Error

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

Rmin = 0
Rmax = 0
Cmin = 0
Cmax = 0

#行方向への探索
for r in range(H):
    #列方向への検索
    for c in range(W):
        #爆弾の場合、周りを加算
        if S[r][c] == ".":
            S[r][c] = 0
        elif S[r][c] == "#":
            if r == 0 :
                Rmin = r
                Rmax = r+2
            elif r == H-1:
                Rmin = r-1
                Rmax = r+1
            else:
                Rmin = r-1
                Rmax = r+2

            if c == 0:
                Cmin = c
                Cmax = c+2
            elif c == W-1:
                Cmin = c-1
                Cmax = c+1
            else:
                Cmin = c-1
                Cmax = c+2
            for i in range(Rmin,Rmax):
                for t in range(Cmin,Cmax):
                    if S[i][t] == "." or S[i][t] == 0:
                        S[i][t] = 1
                    elif not S[i][t] == "#":
                        S[i][t] += 1

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

Submission status: Runtime Error

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

Rmin = 0
Rmax = 0
Cmin = 0
Cmax = 0

#行方向への探索
for r in range(H):
    #列方向への検索
    for c in range(W):
        #爆弾の場合、周りを加算
        if S[r][c] == ".":
            S[r][c] = 0
        elif S[r][c] == "#":
            if r == 0 :
                Rmin = r
                Rmax = r+2
            elif r == H-1:
                Rmin = r-1
                Rmax = r+1
            else:
                Rmin = r-1
                Rmax = r+2

            if c == 0:
                Cmin = c
                Cmax = c+2
            elif c == W-1:
                Cmin = c-1
                Cmax = c+1
            else:
                Cmin = c-1
                Cmax = c+2
            for i in range(Rmin,Rmax):
                for t in range(Cmin,Cmax):
                    if S[i][t] == "." or S[i][t] == 0:
                        S[i][t] = 1
                    elif not S[i][t] == "#":
                        S[i][t] += 1

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

Submission status: Runtime Error

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

for y in range(h):
  for x in range(w):
    if s[y][x]=='.':
      cnt=0
      for dy,dx in [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]:
        ny=y+dy
        nx=x+dx
        if 0<=ny<h and 0<=nx<w and s[ny][nx]=='#':
          cnt+=1
      s[y][x]=str(cnt)

print(''.join(s))

Submission status: Runtime Error

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

l=[]
for i in range(H):
    l.append(list(input()))

for i in range(H):
    for j in range(W):
    # 地雷数のカウンタ
        c=0

    # '#'の場合はなにもしない
    if l[i][j]=='#':
        pass

    # '#'以外の場合
    else:
      # 左上
        if 0<i and 0<j and l[i-1][j-1]=='#':
            c+=1
      # 上
        if 0<i and l[i-1][j]=='#':
            c+=1
      # 右上
        if 0<i and j<W-1 and l[i-1][j+1]=='#':
            c+=1
      # 左
        if 0<j and l[i][j-1]=='#':
            c+=1
      # 右
        if j<W-1 and l[i][j+1]=='#':
            c+=1
      # 左下
        if i<H-1 and 0<j and l[i+1][j-1]=='#':
            c+=1
      # 下
        if i<H-1 and l[i+1][j]=='#':
            c+=1
      # 右下
        if i<H-1 and j<W-1 and l[i+1][j+1]=='#':
            c+=1

      # 地雷隣接数を記載
      l[i][j]=str(c)

# 出力
for i in range(H):
    print(''.join(l[i]))

Submission status: Runtime Error

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

l=[]
for i in range(H):
    l.append(list(input()))

for i in range(H):
    for j in range(W):
    # 地雷数のカウンタ
    c=0

    # '#'の場合はなにもしない
    if l[i][j]=='#':
        pass

    # '#'以外の場合
    else:
      # 左上
      if 0<i and 0<j and l[i-1][j-1]=='#':
        c+=1
      # 上
      if 0<i and l[i-1][j]=='#':
        c+=1
      # 右上
      if 0<i and j<W-1 and l[i-1][j+1]=='#':
        c+=1
      # 左
      if 0<j and l[i][j-1]=='#':
        c+=1
      # 右
      if j<W-1 and l[i][j+1]=='#':
        c+=1
      # 左下
      if i<H-1 and 0<j and l[i+1][j-1]=='#':
        c+=1
      # 下
      if i<H-1 and l[i+1][j]=='#':
        c+=1
      # 右下
      if i<H-1 and j<W-1 and l[i+1][j+1]=='#':
        c+=1

      # 地雷隣接数を記載
      l[i][j]=str(c)

# 出力
for i in range(H):
    print(''.join(l[i]))

Submission status: Runtime Error

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

for k,v in enumerate(S):
  for kk,vv in enumerate(list(v)):
    checkTarget = []
    if vv == "#":
      print("#", end="" )
    else:
      if k == 0 and kk == 0:
        checkTarget = [                         
                                                   S[k][kk+1],
                                     S[k+1][kk], S[k+1][kk+1]]

      elif k == 0 and kk > 0 and kk < len(v)-1 and H==1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        ]

      elif k == 0 and kk > 0 and kk < len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]
      elif k == 0 and kk == len(v)-1 and H==1:
        checkTarget = [ 
                        S[k][kk-1],             
                        ]

      elif k == 0 and kk == len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]
      elif k > 0 and k < len(S) -1 and kk==0 and W==1:
        checkTarget = [       S[k-1][kk],
                                         
                              S[k+1][kk]]

      elif k > 0 and k < len(S) -1 and kk==0:
        checkTarget = [       S[k-1][kk], S[k-1][kk+1],
                                         S[k][kk+1],
                              S[k+1][kk], S[k+1][kk+1]]

      elif k > 0 and k < len(S) -1 and kk==len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk],
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]

      elif k == len(S) -1 and kk == 0 and W==1:
        checkTarget = [   S[k-1][kk]
                                     ]
      elif k == len(S) -1 and kk == 0:
        checkTarget = [   S[k-1][kk], S[k-1][kk+1],
                                        S[k][kk+1]
                                                  ]
      elif k == len(S) -1 and kk> 0 and  kk < len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],                S[k][kk+1]
                                                                ]

      elif k == len(S) -1 and kk == len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], 
                        S[k][kk-1]            
                                                               ]

      else:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]

      print(len( [ i for i in checkTarget if i == "#"]  ), end="")
  print("", end="\n" if k != len(S)-1 else "" )

Submission status: Runtime Error

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

for k,v in enumerate(S):
  for kk,vv in enumerate(list(v)):
    checkTarget = []
    if vv == "#":
      print("#", end="" )
    else:
      if k == 0 and kk == 0:
        checkTarget = [                         
                                                   S[k][kk+1],
                                     S[k+1][kk], S[k+1][kk+1]]

      elif k == 0 and kk > 0 and kk < len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]
      elif k == 0 and kk == len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]
      elif k > 0 and k < len(S) -1 and kk==0 and W==1:
        checkTarget = [       S[k-1][kk],
                                         
                              S[k+1][kk]]

      elif k > 0 and k < len(S) -1 and kk==0:
        checkTarget = [       S[k-1][kk], S[k-1][kk+1],
                                         S[k][kk+1],
                              S[k+1][kk], S[k+1][kk+1]]

      elif k > 0 and k < len(S) -1 and kk==len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk],
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]

      elif k == len(S) -1 and kk == 0 and W==1:
        checkTarget = [   S[k-1][kk]
                                     ]
      elif k == len(S) -1 and kk == 0:
        checkTarget = [   S[k-1][kk], S[k-1][kk+1],
                                        S[k][kk+1]
                                                  ]
      elif k == len(S) -1 and kk> 0 and  kk < len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],                S[k][kk+1]
                                                                ]

      elif k == len(S) -1 and kk == len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], 
                        S[k][kk-1]            
                                                               ]

      else:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]

      print(len( [ i for i in checkTarget if i == "#"]  ), end="")
  print("", end="\n" if k != len(S)-1 else "" )

Submission status: Runtime Error

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

for k,v in enumerate(S):
  for kk,vv in enumerate(list(v)):
    checkTarget = []
    if vv == "#":
      print("#", end="" )
    else:
      if k == 0 and kk == 0:
        checkTarget = [                         
                                                   S[k][kk+1],
                                     S[k+1][kk], S[k+1][kk+1]]

      elif k == 0 and kk > 0 and kk < len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]
      elif k == 0 and kk == len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]
      elif k > 0 and k < len(S) -1 and kk==0:
        checkTarget = [       S[k-1][kk], S[k-1][kk+1],
                                         S[k][kk+1],
                              S[k+1][kk], S[k+1][kk+1]]

      elif k > 0 and k < len(S) -1 and kk==len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk],
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]

      elif k == len(S) -1 and kk == 0:
        checkTarget = [   S[k-1][kk], S[k-1][kk+1],
                                        S[k][kk+1]
                                                  ]
      elif k == len(S) -1 and kk> 0 and  kk < len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],                S[k][kk+1]
                                                                ]

      elif k == len(S) -1 and kk == len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], 
                        S[k][kk-1]            
                                                               ]

      else:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]

      print(len( [ i for i in checkTarget if i == "#"]  ), end="")
  print("", end="\n" if k != len(S)-1 else "" )

Submission status: Runtime Error

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

ans=[[0]*w for i in range(h)]
s=[[""]*w for i in range(h)]
for i in range(h):
  s[i]=input()
  for j in range(w):
    if (s[i][j]=="#"):
      ans[i][j]="#"
      if (i-1>=0 and j-1>=0):
        ans[i-1][j-1]+=1
      if (i-1>=0):
        ans[i-1][j]+=1
      if (i-1>=0 and j+1<w):
        ans[i-1][j+1]+=1
      if (j-1>=0):
        ans[i][j-1]+=1
      if (j+1<w):
        ans[i][j+1]+=1
      if (i+1<h and j-1>=0):
        ans[i+1][j-1]+=1
      if (i+1<h):
        ans[i+1][j]+=1
      if (i+1<h and j+1<w):
      	ans[i+1][j+1]+=1
    

for i in range(h):
  answer=""
  for j in range(w):
    answer+=str(ans[i][j])
  print(answer)

Submission status: Runtime Error

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

s = [input() for i in range(h)]

def checkMine(c):
    if c == "#":
        return 1
    return 0


def dfs(x, y):
    nummines = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            nx, ny = x + j, y + i
            if not (nx == x and nx == y):
                if (0 <= nx and nx < len(s[i]) and 0 <= ny and ny < len(s)):
                    nummines += checkMine(s[ny][nx])
    return nummines
for i in range(len(s)):
    for j in range(w):
        nummines = dfs(j, i)
        if (nummines > 0):
            s[i] = s[i].replace(".", str(nummines), 1)

for string in s:
    print(string)

Submission status: Runtime Error

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

s = []
for i in range(h):
    s += input()

def checkMine(c):
    if c == "#":
        return 1
    return 0


def dfs(x, y):
    nummines = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            nx, ny = x + j, y + i
            if not (nx == x and nx == y):
                if (0 <= nx and nx < len(s[i]) and 0 <= ny and ny < len(s)):
                    nummines += checkMine(s[ny][nx])
    return nummines

for i in range(len(s)):
    for j in range(len(s[0])):
        nummines = dfs(j, i)
        if (nummines > 0):
            replaced = s[i].replace(".", str(nummines), 1)
            s[i] = replaced
        
for elem in s:
    print(elem)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
f=[]
f.append("."*(w+2))
for i in range(h):
  f.append("."+input()+".")
a=[]*h[]*w
for i in range(1,h+1):
  for j in range(1,w+1):
    if f[i][j]=".":
      dumy=f[i-1][j-1]+f[i-1][j]+f[i-1][j+1]+f[i][j-1]+f[i][j]+f[i][j+1]+f[i+1][j-1]+f[i+1][j]+f[i+1][j+1]
      a[i-1][j-1]=count(dumy.index("#"))
    else:
      a[i-1][j-1]="#"
      
for i in range(h):
  print(a[i])

Submission status: Runtime Error

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            ans[i][j] = "#"
        else:
            if 0 < i and i < H-1:
                li = [i-1, i, i+1]
            elif i == 0:
                li = [i, i+1]
            else:
                li = [i-1, i]
            if 0 < j and j < W-1:
                lj = [j-1, j, j+1]
            elif j == 0:
                lj = [j, j+1]
            else:
                lj = [j-1, j]
            ans[i][j] = str(sum([S[lii][ljj]=="#" for lii in li for ljj in lj]))

for i in range(H):
    s = "".join(ans[i])
    print(s)

Submission status: Runtime Error

Solution code:
# numpyを使用した場合
import numpy as np

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
f = np.zeros((h+2, w+1), dtype='int64')
d = np.ones((3, 3), dtype='int64')

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            f[i:i+3, j:j+3] += d 

for i in range(h):
    ans = ''
    for j in range(w):
        if s[i][j] == '#':
            ans += '#'
        else:
            ans += str(f[i+1, j+1])
    print(ans)