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: Wrong Answer

Solution code:
import sys

a = sys.stdin.readline().split(' ')
data = sys.stdin.readlines()

S = int(a[0])
H = int(a[1])

for i in range(len(data)):
    data[i] = [j.replace('\n', '') for j in data[i]]

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

if S == 1 and H == 1:
    if data[0][0] == ".":
        data[0][0] = str(0)

for i in range(S):
    for j in range(H):
        if data[i][j] == '#':
            continue
        else:
            count = 0
            for k in range(8):
                x = j + dx[k]
                y = i + dy[k]
                if x < 0 or x >=H or y < 0 or y >= S:
                    continue
                elif data[y][x] == "#":
                    count = count+1
                data[i][j] = str(count)

for i in range(len(data)):
    print(str(data[i]).replace('[', '').replace(']', '').replace(',', '').replace("'", ''))

Submission status: Wrong Answer

Solution code:
H, W=map(int, input().split())
A= [list(input()) for _ in range(H)]
B = [[0]*W for _ in range(H)]
nx = [1,1,1,0,0,-1,-1,-1]
ny = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(H):
  for j in range(W):
    if A[i][j] == "#":
      B[i][j] = "#"
    else:
      c = 0
      for k in range(8):
        newx = i+nx[k]
        newy = j+ny[k]
        if 0<= newx <= H-1 and 0<= newy <= W-1:
          if A[newx][newy] == "#":
            c += 1
      B[i][j] = c
for i in range(H):
  print(*B[i])

Submission status: Wrong Answer

Solution code:
H, W=map(int, input().split())
A= [list(input()) for _ in range(H)]
B = [[0]*W for _ in range(H)]
nx = [1,1,1,0,0,-1,-1,-1]
ny = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(H):
  for j in range(W):
    if A[i][j] == "#":
      B[i][j] = "#"
    else:
      c = 0
      for k in range(8):
        newx = i+nx[k]
        newy = j+ny[k]
        if 0<= newx <= H-1 and 0<= newy <= W-1:
          if A[newx][newy] == "#":
            c += 1
      B[i][j] = c
for i in range(H):
  print(*B[i])

Submission status: Wrong Answer

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

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

for i in range(h):
    for j in range(w):
        if board[i][j] == "#":continue

        num = 0
        for k in range(8):
            ni = i + dy[k]
            nj = j + dx[k]

            if ni < 0 or h <= ni: continue
            if nj < 0 or w <= nj: continue
            if board[ni][nj] == '#':num+=1
        
        board[i][j] = num

for i in range(h):print(board[i])

Submission status: Wrong Answer

Solution code:
def incre(h, w, a, i, j):
    if 0 <= i <= h-1 and 0 <= j <= w-1 and a[i][j] != '#':
        a[i][j] += 1


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

a = [[0]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            a[i][j] = '#'

for i in range(h):
    for j in range(w):
        if (s[i][j] == '#'):
            if i == 0:
                if j == 0:
                    incre(h, w, a, i, j+1)
                    incre(h, w, a, i+1, j)
                    incre(h, w, a, i+1, j+1)
                elif 0 < j < w - 1:
                    incre(h, w, a, i, j-1)
                    incre(h, w, a, i, j+1)
                    incre(h, w, a, i+1, j-1)
                    incre(h, w, a, i+1, j)
                    incre(h, w, a, i+1, j+1)

                elif j == w-1:
                    incre(h, w, a, i, j-1)
                    incre(h, w, a, i-1, j-1)
                    incre(h, w, a, i+1, j)
            elif 0 < i < h - 1:
                if j == 0:
                    incre(h, w, a, i-1, j)
                    incre(h, w, a, i-1, j+1)
                    incre(h, w, a, i, j+1)
                    incre(h, w, a, i+1, j)
                    incre(h, w, a, i+1, j+1)
                elif 0 < j < w - 1:
                    incre(h, w, a, i-1, j-1)
                    incre(h, w, a, i-1, j)
                    incre(h, w, a, i-1, j+1)
                    incre(h, w, a, i, j-1)
                    incre(h, w, a, i, j+1)
                    incre(h, w, a, i+1, j-1)
                    incre(h, w, a, i+1, j)
                    incre(h, w, a, i+1, j+1)
                elif j == w-1:
                    incre(h, w, a, i-1, j)
                    incre(h, w, a, i-1, j-1)
                    incre(h, w, a, i, j-1)
                    incre(h, w, a, i+1, j-1)
                    incre(h, w, a, i+1, j)
            elif i == h-1:
                if j == 0:
                    incre(h, w, a, i-1, j)
                    incre(h, w, a, i-1, j+1)
                    incre(h, w, a, i, j+1)
                elif 0 < j < w - 1:
                    incre(h, w, a, i-1, j-1)
                    incre(h, w, a, i-1, j)
                    incre(h, w, a, i-1, j+1)
                    incre(h, w, a, i, j-1)
                    incre(h, w, a, i, j+1)
                elif j == w-1:
                    incre(h, w, a, i-1, j-1)
                    incre(h, w, a, i-1, j)
                    incre(h, w, a, i, j-1)

[print(*i, sep="") for i in a]

Submission status: Wrong Answer

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0

    # 左と右
    if index % col != 0:# 0列でない
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:# 末列でない
        if newLine[index + 1] == "#":
            count += 1
    # 上と下
    if index >= col: #首行でない
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row - 1):#末行でない
        if newLine[index + col] == "#":
            count += 1

    # 上の左と右 0 <= index + col + 1 < len(newLine) and
    if index >= col and index % col != 0: #首行でない かつ　0列でない
        if newLine[index - col - 1] == "#":
            count += 1
    if index >= col and index % col != col - 1: #首行でない かつ　末列でない
        if newLine[index - col + 1] == "#":
            count += 1
    # 下の左と右
    if index < col * (row - 1) and index % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if index < col * (row - 1) and index % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(row,col,lines):

    
    for index, value in enumerate(lines):
        if value == ".":
            lines = check(lines, index, row, col)
    for i in range(0, len(lines), col):
        print("".join(lines[i:i + col]))


if __name__ == '__main__':
    row, col = map(int, input().split())
    lines = list(str(input()) for i in range(row))
    main(row,col,lines)

Submission status: Wrong Answer

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

if H ==1 and W ==1:
    if S[0][0] == ".":
        Answer[0][0] = 0
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()


elif H == 1:
    if S[0][0] == ".":
        if S[0][1] == "#":
            Answer[0][0] = 1
        else:
            Answer[0][0] = 0
    if S[0][W-1] == ".":
        if S[0][W-2] == "#":
            Answer[0][W-1] = 1
        else:
            Answer[0][W-1] = 0
    for i in range(1, W-1):
        count = 0
        if S[0][i] == ".":
            if S[0][i-1] == "#":
                count += 1
            if S[0][i+1] == "#":
                count += 1
            Answer[0][i] = count
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()

elif W == 1:
    if S[0][0] == ".":
        if S[1][0] == "#":
            Answer[0][0] = 1
        else:
            Answer[0][0] = 0
    if S[H-1][0] == ".":
        if S[H-2][0] == "#":
            Answer[H-1][0] = 1
        else:
            Answer[H-1][0] = 0
    for i in range(1, H-1):
        count = 0
        if S[i][0] == ".":
            if S[i-1][0] == "#":
                count += 1
            if S[i+1][0] == "#":
                count += 1
            Answer[i][0] = count
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()

if S[0][0] == ".":
    count = 0
    if S[0][1] == "#":
        count += 1
    if S[1][0] == "#":
        count += 1
    if S[1][1] == "#":
        count += 1
    Answer[0][0] = count

if S[0][W-1] == ".":
    count = 0
    if S[0][W-2] == "#":
        count += 1
    if S[1][W-1] == "#":
        count += 1
    if S[1][W-2] == "#":
        count += 1
    Answer[0][W-1] = count

if S[H-1][0] == ".":
    count = 0
    if S[H-1][1] == "#":
        count += 1
    if S[H-2][0] == "#":
        count += 1
    if S[H-2][1] == "#":
        count += 1
    Answer[H-1][0] = count

if S[H-1][W-1] == ".":
    count = 0
    if S[H-1][W-2] == "#":
        count += 1
    if S[H-2][W-1] == "#":
        count += 1
    if S[H-2][W-2] == "#":
        count += 1
    Answer[H-1][W-1] = count

for i in range(1, W-1):
    count = 0
    if S[0][i] == ".":
        if S[0][i-1] == "#":
            count += 1
        if S[0][i+1] == "#":
            count += 1
        if S[1][i] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        Answer[0][i] = count

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

for i in range(1, H-1):
    count = 0
    if S[i][0] == ".":
        if S[i][1] == "#":
            count += 1
        if S[i-1][0] == "#":
            count += 1
        if S[i-1][1] == "#":
            count += 1
        if S[i+1][0] == "#":
            count += 1
        if S[i+1][1] == "#":
            count += 1
        Answer[i][0] = count

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

for i in range(1, H-1):
    for j in range(1, W-1):
        count = 0
        if S[i][j] == ".":
            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
            Answer[i][j] = count

for rooms in Answer:
    room=[str(a) for a in rooms]
    room = "".join(room)
    print(room)

Submission status: Wrong Answer

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

if H ==1 and W ==1:
    if S[0][0] == ".":
        Answer[0][0] = 0
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()


elif H == 1:
    if S[0][1] == "#" and S[0][0] == ".":
        Answer[0][0] = 1
    if S[0][W-2] == "#" and S[0][W-1] == ".":
        Answer[0][W-1] = 1
    for i in range(1, W-1):
        count = 0
        if S[0][i] == ".":
            if S[0][i-1] == "#":
                count += 1
            if S[0][i+1] == "#":
                count += 1
        Answer[0][i] == count
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()

elif W == 1:
    if S[1][0] == "#" and S[0][0] == ".":
        Answer[0][0] = 1
    if S[W-2][0] == "#" and S[W-1][0] == ".":
        Answer[W-1][0] = 1
    for i in range(1, W-1):
        count = 0
        if S[i][0] == ".":
            if S[i-1][0] == "#":
                count += 1
            if S[i+1][0] == "#":
                count += 1
        Answer[i][0] == count
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()

if S[0][0] == ".":
    count = 0
    if S[0][1] == "#":
        count += 1
    if S[1][0] == "#":
        count += 1
    if S[1][1] == "#":
        count += 1
    Answer[0][0] = count

if S[0][W-1] == ".":
    count = 0
    if S[0][W-2] == "#":
        count += 1
    if S[1][W-1] == "#":
        count += 1
    if S[1][W-2] == "#":
        count += 1
    Answer[0][W-1] = count

if S[H-1][0] == ".":
    count = 0
    if S[H-1][1] == "#":
        count += 1
    if S[H-2][0] == "#":
        count += 1
    if S[H-2][1] == "#":
        count += 1
    Answer[H-1][0] = count

if S[H-1][W-1] == ".":
    count = 0
    if S[H-1][W-2] == "#":
        count += 1
    if S[H-2][W-1] == "#":
        count += 1
    if S[H-2][W-2] == "#":
        count += 1
    Answer[H-1][W-1] = count

for i in range(1, W-1):
    count = 0
    if S[0][i] == ".":
        if S[0][i-1] == "#":
            count += 1
        if S[0][i+1] == "#":
            count += 1
        if S[1][i] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        Answer[0][i] = count

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

for i in range(1, H-1):
    count = 0
    if S[i][0] == ".":
        if S[i][1] == "#":
            count += 1
        if S[i-1][0] == "#":
            count += 1
        if S[i-1][1] == "#":
            count += 1
        if S[i+1][0] == "#":
            count += 1
        if S[i+1][1] == "#":
            count += 1
        Answer[i][0] = count

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

for i in range(1, H-1):
    for j in range(1, W-1):
        count = 0
        if S[i][j] == ".":
            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
            Answer[i][j] = count

for rooms in Answer:
    room=[str(a) for a in rooms]
    room = "".join(room)
    print(room)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
S=[]
for _ in range(h):
    s=list(map(str,input()))
    S.append(s)
def check(x,y):
    if S[x][y]=='#':
        return 1
    else:
        return 0
for i in range(h):
    for j in range(w):
        if S[i][j]=='.':
            count=0
            for ii in range(i-1,i+2):
                for jj in range(j-1,j+2):
                    if ii>=0 and jj>=0 and ii<=h-1 and jj<=w-1:
                        if ii!=i or jj!=j:
                            count+=check(ii,jj)
            S[i][j]=count
for i in range(h):
  print(*S[i])

Submission status: Wrong Answer

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

ans=[]
for row in range(len(L)):
    LINE=[]
    for col in range(len(L[row])):
        print('row=',row,'col=',col,L[row][col])
        if L[row][col]=='.':
            cnt=0
            for shiftX, shiftY in [(i,j) for i in [-1,0,1] for j in [-1,0,1] if i!=0 or j!=0]:
                if 0<=row+shiftY<h and 0<=col+shiftX<w:
                    if L[row+shiftY][col+shiftX]=='#':
                        cnt+=1
            LINE.append(str(cnt))
        else:
            LINE.append('#')
    ans.append(LINE)

for a in ans:
    print(''.join(a))

Submission status: Wrong Answer

Solution code:
def check(i,j):
    c=0
    try:
        if i!=0 and j!=0:
            if S[i-1][j-1]=="#":
                c+=1
    except:
        pass
    try:
        if S[i-1][j]=="#":
            c+=1
    except:
        pass
    try:
        if i!=0:
            if S[i-1][j+1]=="#":
                c+=1
    except:
        pass
    try:
        if j!=0:
            if S[i][j-1]=="#":
                c+=1
    except:
        pass
    try:
        if S[i][j+1]=="#":
            c+=1
    except:
        pass
    try:
        if j!=0:
            if S[i+1][j-1]=="#":
                c+=1
    except:
        pass

    try:
        if S[i+1][j]=="#":
            c+=1
    except:
        pass
                            
    try:
        if S[i+1][j+1]=="#":
            c+=1
    except:
        pass
    return c

h,w=map(int,input().split())
S=[input() for i in range(h)] 
#print(S)
for i in range(h):
    s=""
    for j in range(w):
        #print(i,j)
        if S[i][j]=="#":
            s+="#"
        else:
            count=check(i,j)
            #print(count)
            s+=str(count)
    print(s)

Submission status: Wrong Answer

Solution code:
def check(i,j):
    c=0
    try:
        if S[i-1][j-1]=="#":
            c+=1
    except:
        pass
    finally:
        try:
            if S[i-1][j]=="#":
                c+=1
        except:
            pass
        finally:
            try:
                if S[i-1][j+1]=="#":
                    c+=1
            except:
                pass
            finally:
                try:
                    if S[i][j-1]=="#":
                        c+=1
                except:
                    pass
                finally:
                    try:
                        if S[i][j+1]=="#":
                            c+=1
                    except:
                        pass
                    finally:
                        try:
                            if S[i+1][j-1]=="#":
                                c+=1
                        except:
                            pass
                        finally:
                            try:
                                if S[i+1][j]=="#":
                                    c+=1
                            except:
                                pass
                            finally:
                                try:
                                    if S[i+1][j+1]=="#":
                                        c+=1
                                except:
                                    pass
    return c

h,w=map(int,input().split())
S=[input() for i in range(h)] 
#print(S)
for i in range(h):
    s=""
    for j in range(w):
        #print(i,j)
        if S[i][j]=="#":
            s+="#"
        else:
            count=check(i,j)
            #print(count)
            s+=str(count)
    print(s)

Submission status: Wrong Answer

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

for _ in range(h):
    l = list(input())
    grid.append(l)

def search(a,b):
    if grid[a][b] == "#":
        return "#"

    mine = 0

    if 0 <= a-1 and 0 <= b-1:
        if grid[a-1][b-1] == "#":
            mine += 1

    if 0 <= a-1:
        try:
            if grid[a-1][b] == "#":
                mine += 1
        except:
            pass

        try:
            if grid[a-1][b+1] == "#":
                mine += 1
        except:
            pass

    if 0 <= b-1:
        if grid[a][b-1] == "#":
                mine += 1

        try:
            if grid[a+1][b-1] == "#":
                mine += 1
        except:
            pass

    try:
        if grid[a][b+1] == "#":
            mine += 1
    except:
        pass


    try:
        if grid[a+1][b] == "#":
            mine += 1
    except:
        pass

    try:
        if grid[a+1][b+1] == "#":
            mine += 1
    except:
        pass

    return mine

ans = []
for i in range(h):
    l = []
    for j in range(w):
        print (i,j,grid[i][j],search(i,j))
        l.append(search(i,j))
    ans.append(l)

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

Submission status: Wrong Answer

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

for _ in range(h):
    l = list(input())
    grid.append(l)

def search(a,b):
    if grid[a][b] == "#":
        return "#"

    mine = 0
    try:
        if grid[a-1][b-1] == "#":
            mine += 1
    except:
        pass

    try:
        if grid[a-1][b] == "#":
            mine += 1
    except:
        pass

    try:
        if grid[a-1][b+1] == "#":
            mine += 1
    except:
        pass

    try:
        if grid[a][b-1] == "#":
            mine += 1
    except:
        pass

    try:
        if grid[a][b+1] == "#":
            mine += 1
    except:
        pass

    try:
        if grid[a+1][b-1] == "#":
            mine += 1
    except:
        pass

    try:
        if grid[a+1][b] == "#":
            mine += 1
    except:
        pass

    try:
        if grid[a+1][b+1] == "#":
            mine += 1
    except:
        pass

    return mine

ans = []
for i in range(h):
    l = []
    for j in range(w):
        l.append(search(i,j))
    ans.append(l)

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

Submission status: Wrong Answer

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

d = [['#']*W]*H
cnt = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
#            continue
#            print(i,j)
            d[i][j] = '#'
        else:
            if i-1>=0 and S[i-1][j] == '#':
                cnt += 1
            if i-1>=0 and j-1>=0 and S[i-1][j-1] == '#':
                cnt += 1
            if i-1>=0 and j+1<W and S[i-1][j+1] == '#':
                cnt += 1
            if i-1>=0 and S[i][j-1] == '#':
                cnt += 1
            if j+1<W and S[i][j+1] == '#':
                cnt += 1
            if i+1<H and S[i+1][j] == '#':
                cnt += 1
            if i+1<H and j-1>=0 and S[i+1][j-1] == '#':
                cnt += 1
            if i+1<H and j+1<W and S[i+1][j+1] == '#':
                cnt += 1
            S[i][j] = str(cnt)
        cnt = 0

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

Submission status: Wrong Answer

Solution code:
def replace(s, r, c):
    print("target  (%d, %d)" % (r, c))
    for row in range(r - 1, r + 2):
        for column in range(c - 1, c + 2):
            if row < 0 or len(s) <= row:
                continue

            if column < 0 or len(s[row]) <= column:
                continue

            value = s[row][column]
            if value != "#":
                value = value + 1 if value != "." else 1
                s[row][column] = value

h, w = map(int, input().split(" "))
s = []
for _ in range(h):
    row = input()
    tmp = []
    for i in range(w):
        tmp.append(row[i])
    s.append(tmp)

for row in range(h):
    for column in range(w):
        if s[row][column] == "#":
            replace(s, row, column)

for row in range(h):
    res = ""
    for column in range(w):
        res += str(s[row][column])
    print(res)

Submission status: Wrong Answer

Solution code:
def replace(s, r, c):
    for row in range(r - 1, r + 2):
        for column in range(c - 1, c + 2):
            if len(s) <= row:
                continue

            if len(s[row]) <= column:
                continue

            s.index
            value = s[row][column]
            if value != "#":
                value = value + 1 if value != "." else 1
                s[row][column] = value

h, w = map(int, input().split(" "))
s = []
for _ in range(h):
    row = input()
    tmp = []
    for i in range(w):
        tmp.append(row[i])
    s.append(tmp)

for row in range(h):
    for column in range(w):
        if s[row][column] == "#":
            replace(s, row, column)

for row in range(h):
    res = ""
    for column in range(w):
        res += str(s[row][column])
    print(res)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

directions = [
    (-1, 1),(0, 1),(1, 1),
    (-1, 0),(1, 0),
    (-1, -1),(0, -1),(1, -1)
    ]

new_masu = []
for h in range(H):
    tmp = []
    for w in range(W):
        if masu[h][w] == "#":
            tmp.append("#")
            continue

        bombs = 0
        for direction in directions:
            x = w + direction[0]
            y = h + direction[1]
            try:
                if masu[y][x] == "#":
                    bombs += 1
            except Exception:
                pass
        tmp.append(bombs)
    new_masu.append(tmp)

for line in new_masu:
    print(*line, sep="")

Submission status: Wrong Answer

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


str_list.append(["."]*w)
str_list.insert(0,["."]*w)
for i in range(h+2):
    str_list[i].append(".")
    str_list[i].insert(0,".")

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

baku = 0

for i in range(h):
    for j in range(w):
        if str_list[i+1][j+1] == "*":
            ans[i][j] = "*"
            continue
        
        baku = 0
        for a in range(3):
            for b in range(3):

                if a == 1 and b==1:
                    continue
                if str_list[i+a][j+b]=="*":
                    baku += 1
        
        ans[i][j] = baku
            

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

    print(a)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
grid = [['.']*(w+2)] + [['.'] + [s for s in input()] + ['.'] for i in range(h)] + [['.']*(w+2)]
print(grid)
ans = [[0]*w for i in range(h)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if grid[i][j] == '.':
            for dx in range(-1,2):
                for dy in range(-1,2):
                    if grid[i+dx][j+dy] == '#':
                        ans[i-1][j-1] += 1
        else:
            ans[i-1][j-1] = '#'

for i in range(h):
    for j in range(w):
        ans[i][j] = str(ans[i][j])

for i in range(h):
    ans[i] = ''.join(ans[i])

for i in range(h):
    print(ans[i])

Submission status: Wrong Answer

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

for i in range(0, H):
    s.append(input())

for y in range(0, H):
    for x in range(0, W):
        c=0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                xx = x + dx
                yy = y + dy
                if 0<xx and xx<W and 0<yy and yy<H:
                    if s[yy][xx]=="#":
                        c+=1

        if s[y][x]=="#":
            print("#", end='')
        else:
            print(c, end='')
    print("")

Submission status: Wrong Answer

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

for i in range(0, H):
    s.append(input())

for y in range(0, H):
    for x in range(0, W):
        c=0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                xx = x + dx
                yy = y + dy
                if 0<xx and xx<W and 0<yy and yy<H:
                    if s[yy][xx]=="#":
                        c+=1

        if s[y][x]=="#":
          	print("#", end='')
        else:
            print(c, end='')
    print("")

Submission status: Wrong Answer

Solution code:
import numpy as np


def main():
    h, w = map(int, input().split())
    ss = np.array([list(input()) for _ in range(h)])
    ans = np.zeros((h, w))
    xs, ys = np.where(ss == '#')
    for x, y in zip(xs, ys):
        coord = np.array([[x, y]])
        surround = np.array([[-1, -1], [-1, 0], [-1, +1], [0, -1], [0, +1], [+1, -1], [+1, 0], [+1, +1]])
        mines = np.tile(coord, (8, 1)) - surround
        for mx, my in mines:
            if mx < 0 or my < 0 or mx >= h or my >= w:
                continue
            ans[mx, my] += 1
    ans = ans.astype(np.int16).tolist()
    for x, y in zip(xs, ys):
        ans[x][y] = '#'

    for line in ans:
        print(*line)


if __name__ == "__main__":
    main()