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

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

print(s)

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            c = 0
            if  i-1 >= 0 and i-1 <= h-1 and j-1 >= 0 and j-1 <= w-1:
               if s[i-1][j-1] == '#':
                 c += 1
            if  i >= 0 and i <= h-1 and j-1 >= 0 and j-1 <= w-1:
                if s[i][j-1] == '#':
                     c += 1
            if   i+11 >= 0 and i+1 <= h-1 and j-1 >= 0 and j-1 <= w-1:
                if s[i+1][j-1] == '#':
                    
                    c += 1
            if i-1 >= 0 and i-1 <= h-1 and j >= 0 and j <= w-1:
                if s[i-1][j] == '#' :
                    c += 1
            if   i >= 0 and i <= h-1 and j >= 0 and j <= w-1:
                if s[i][j] == '#':
                    c += 1
            if  i+1 >= 0 and i+1 <= h-1 and j >= 0 and j <= w-1:
                if s[i+1][j] == '#':
                    c += 1
            if i-1 >= 0 and i-1 <= h-1 and j+1 >= 0 and j+1 <= w-1:
                if s[i-1][j+1] == '#' :
                    c += 1
            if   i >= 0 and i <= h-1 and j+1 >= 0 and j+1 <= w-1:
                if s[i][j+1] == '#':
                    c += 1
            if  i+1 >= 0 and i+1 <= h-1 and j+1 >= 0 and j+1 <= w-1:
                if s[i+1][j+1] == '#':
                    c += 1
            s[i][j] = str(c)

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

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s_list = [input() for i in range(h)]
ans_list = []
for i in range(h):
    ans_line = []
    for j in range(w):
        ans_line.append('#')
    for j in range(w):
        count = 0
        if s_list[i][j] == '#':
            continue
        for dy in range(-1, 2):
            for dx in range(-1, 2):
                if dy == 0 and dx == 0:
                    continue
                ny = dy + i
                nx = dx + j
                if ny < 0 or nx < 0 or ny >= h or nx >= w:
                    continue
                if s_list[ny][nx] == '#':
                    count += 1
        ans_line[j] = count
    ans_list.append(ans_line)

print(ans_list)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
s_list = []
for i in range(H):
    s_list.append("."+ input()+ ".")
s_list.append("."*(W+2))
s_list.insert(0,"."*(W+2))

result = []
for i in range(1, H+1):
    line = []
    for j in range(1, W+1):
        count = 0
        count += 0 if s_list[i][j-1] == "." else 1
        count += 0 if s_list[i+1][j-1] == "." else 1
        count += 0 if s_list[i+1][j] == "." else 1
        count += 0 if s_list[i+1][j+1] == "." else 1
        count += 0 if s_list[i][j+1] == "." else 1
        count += 0 if s_list[i-1][j+1] == "." else 1
        count += 0 if s_list[i-1][j] == "." else 1
        count += 0 if s_list[i-1][j-1] == "." else 1
        line.append(str(count) if count >0 else "#")
    result.append(line)
for i in range(H):
    print("".join(result[i]))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
diff = [-1, 0, 1]
for i in range(H):
	for j in range(W):
		count = 0
		for x in diff:
			for y in diff:
				if 0 < i + x < H and 0 < j + y < W and S[i+x][j+y] == '#':
					count += 1
		if S[i][j] == '.':
			S[i][j] = count
for s  in S:
	print(''.join(map(str, s)))

Submission status: Wrong Answer

Solution code:
from itertools import product
H, W = map(int, input().split())
a = []
for i in range(H): a.append(list(input()))
for i in range(H):
    for j in range(W):
        b = 0
        if a[i][j] == '#':continue
        for k,l in product([-1,0,1], repeat=2):
            if k == l == 0:continue
            h = i + k
            w = j + l
            if h < 0 or h >= H: continue
            if w < 0 or w >= W: continue
            if a[h][w] == '#':b+=1
        a[i][j] = b

for i in range(H):
    print(*a[i])

Submission status: Wrong Answer

Solution code:
H,W=map(int,raw_input().split())
s=[raw_input() for r in xrange(H)]
# s[h][w]
dx = [1,1,1,0,-1,-1,-1,0]
dy = [0,1,1,1,0,-1,-1,-1]
for r in xrange(H):
  buf = []
  for c in xrange(W):
    if s[r][c] == '#':
      buf.append('#')
    else:
      cnt = 0
      for i in xrange(8):
        ri = r + dx[i]
        ci = c + dy[i]
        if (0 <= ri < H) and (0 <= ci < W):
          if s[ri][ci] == '#':
            cnt += 1
      buf.append(str(cnt))
  print ''.join(buf)

Submission status: Wrong Answer

Solution code:
H,W=[int(i) for i in input().split()]
A=[]
for i in range(H):
    s=input()
    S=list(s)
    A.append(S)
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            break
        else:            
            s=0
            for k in range(-1,2):
                for l in range(-1,2):
                    if i+k>=0 and i+k<=H-1 and j+l>=0 and j+l<=W-1:
                        if A[i+k][j+l]=="#":
                            s+=1
                            A[i][j]=s
for i in range(H):
    for j in range(W):
        print(A[i][j],end=" ")
    print()

Submission status: Wrong Answer

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

for i in range(h):
    row = ''
    for j in range(w):
        cnt = 0
        if s[i][j] == '#':
            row += '#'
            continue
        if i > 0 and j > 0 and s[i-1][j-1] == '#':
            cnt += 1
        if i > 0 and s[i-1][j] == '#':
            cnt += 1
        if i > 0 and j < w - 1 and s[i-1][j+1] == '#':
            cnt += 1
        if j > 0 and s[i][j-1] == '#':
            cnt += 1
        if j < w - 1 and s[i][j+1] == '#':
            cnt += 1
        if i < h - 1 and s[i+1][j-1] == '#':
            cnt += 1
        if i < h - 1 and s[i+1][j] == '#':
            cnt += 1
        if  i < h - 1 and j < w - 1 and s[i+1][j+1] == '#':
            cnt += 1
        row += str(cnt)
    print(row)

Submission status: Wrong Answer

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



# 左右に要素を追加
for i in range(H): # 左右に要素を追加
    S[i] = ['.'] + S[i] + ['.']

# 上下に要素を追加
S = [['.' for i in range(W + 2)]] + S + [['.' for i in range(W + 2)]]

S_dummy = S

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == '.':
            tmp = 0
            if S[i - 1][j - 1] == '#':
                tmp += 1
            if S[i - 1][j] == '#':
                tmp += 1
            if S[i - 1][j + 1] == '#':
                tmp += 1
            if S[i][j - 1] == '#':
                tmp += 1
            if S[i][j + 1] == '#':
                tmp += 1
                tmp += 1
            if S[i + 1][j - 1] == '#':
                tmp += 1
            if S[i + 1][j] == '#':
                tmp += 1
            if S[i + 1][j + 1] == '#':
                tmp += 1
            S_dummy[i][j] = tmp

for i in range(1, H + 1):
    for j in range(1, W + 1):
        print(S_dummy[i][j], end = "")
    print('')

Submission status: Wrong Answer

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

nS = [['' for i in range(W)] for j in range(H)]

cpC = [1, 0, -1, 0, 1, -1, -1, 1]
cpR = [0, 1, 0, -1, 1, 1, -1, -1]


for i in range(H):
    for j in range(W):
        counter = 0
        if S[i][j] == '#':
            nS[i][j] = '#'
            continue

        for r,c in zip(cpR,cpC):
            try:
                if S[i+r][j+c] == '#':
                    counter += 1
            except:
                continue
        
        nS[i][j] = counter

for i in range(H):
    for j in range(W):
        print(nS[i][j], end="")
    print('')

Submission status: Wrong Answer

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

nS = [['' for i in range(W)] for j in range(H)]

cpR = [1, 0, -1, 0, 1, -1, -1, 1]
cpC = [0, 1, 0, -1, 1, 1, -1, -1]


for i in range(H):
    for j in range(W):
        counter = 0
        if S[i][j] == '#':
            nS[i][j] = '#'
            continue

        for r,c in zip(cpR,cpC):
            try:
                if S[i+r][j+c] == '#':
                    counter += 1
            except:
                continue
        
        nS[i][j] = counter

for i in range(H):
    for j in range(W):
        print(nS[i][j], end="")
    print('')

Submission status: Wrong Answer

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

result = ""
vektor = [
    [-1, -1],
    [-1, 0], 
    [-1, 1], 
    [0, -1], 
    [0, 1],
    [1, -1],
    [1, 0], 
    [1, 1]
    ]

for i in range(H):
    for j in range(W):
        if P[i][j] == "#":
            result += "#"
        else:
            count = 0
            for k in range(8):
                try:
                    if i+vektor[k][0] >= 0 and j+vektor[k][1] >= 0 and P[i+vektor[k][0]][j+vektor[k][1]] == "#":
                        count += 1
                except IndexError:
                    pass
            result += str(count)

print(result)

start = 0
for i in range(0, H*W+1, W):
    print(result[start:i])
    start = i

Submission status: Wrong Answer

Solution code:
W,H=map(int,input().split())
arr = [input() for j in range(W)]
ans = [['0' for i in range(H)] for j in range(W)]

for i in range(0, W):
    for j in range(0, H):
        bomb_num = 0
        if arr[i][j] == '#':
            ans[i][j] = '#'
            break
        for k in (-1, 0, 1):
            for l in (-1, 0, 1):
                if (-1<i+k<W) and (-1<j+l<H):
                    if arr[i+k][j+l] == '#':
                        bomb_num += 1
                        
        ans[i][j] = str(bomb_num)
            
for i in range(W):
    for j in range(H):
        print(ans[i][j], end = '')
    print('\n')

Submission status: Wrong Answer

Solution code:
W,H=map(int,input().split())
arr=[[0 for i in range(H)] for j in range(W)]
ans=[[0 for i in range(H)] for j in range(W)]

for i in range(W):
    gyou=input()
    for j in range(H):
        arr[i][j]=gyou[j:j+1]


for i in range(W):
    for j in range(H):
        if arr[i][j]=="#":
            ans[i][j]="#"
        else:    
            for k in range(-1,2):
                for l in range(-1,2):
                    try:
                        if arr[i+k][l+j]=="#":
                            ans[i][j]+=1
                    except:
                        pass
for i in range(W):
    ans1=map(str,ans[i])
    ans2="".join(ans1)
    
    print(ans2)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
dx=[1,1,0,0,1,-1,-1,-1]
dy=[0,1,1,-1,-1,1,0,-1]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]==".":
            for k in range(8):
                x=i+dx[k]
                y=j+dy[k]
                if 0<x and x<h and 0<=y and y<w and s[x][y]=="#":
                    cnt+=1
            s[i][j]=str(cnt)
for i in range(h):
    print(*s[i],sep="")

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
s = [list(input())for _ in range(H)]
grid = [(-1,-1),(-1,1),(1,-1),(1,1),(1,0),(0,1),(-1,0),(0,-1)]
for y in range(H):
    for x in range(W):
        count = 0
        if s[y][x] == '#':
            print('#', end='')
        else:
            for g in grid:
                try:
                    ss = s[y+g[0]][x+g[1]]
                except IndexError:
                    continue
                if x == 0 and g == (-1, 0):
                    continue

                if ss == '#':
                    count += 1
            print(count,end='')
    print()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
s = [list(input())for _ in range(H)]
grid = [(-1,-1),(-1,1),(1,-1),(1,1),(1,0),(0,1),(-1,0),(0,-1)]
for y in range(H):
    for x in range(W):
        count = 0
        if s[y][x] == '#':
            print('#', end='')
        else:
            for g in grid:
                try:
                    ss = s[y+g[0]][x+g[1]]
                except IndexError:
                    continue
                if ss == '#':
                    count += 1
            print(count,end='')
    print()

Submission status: Wrong Answer

Solution code:
import sys

s_args = sys.stdin.read().splitlines()

rows = []
rows.append([0] * (len(s_args[1])+2))
for s_arg in s_args[1:]:
    row = []
    for cell in s_arg:
        if cell == '.':
            row.append(0)
        else:
            row.append(-100000)
    rows.append([0] + row + [0])
rows.append([0] * (len(s_args[1])+2))

for (i, row) in enumerate(rows):
    if i == 0 or i == len(rows) - 1:
        continue
    for (j, cell) in enumerate(row):
        if i == 0 or i == len(row) - 1:
            continue
        if cell < 0:
            rows[i-1][j-1] += 1
            rows[i-1][j] += 1
            rows[i-1][j+1] += 1
            rows[i][j-1] += 1
            rows[i][j+1] += 1
            rows[i+1][j-1] += 1
            rows[i+1][j] += 1
            rows[i+1][j+1] += 1
            
for (i, row) in enumerate(rows):
    if i == 0 or i == len(rows) - 1:
        continue
    for (j, cell) in enumerate(row):
        if j == 0 or j == len(row) - 1:
            continue
        if cell < 0:
            print('#', end='')
        else:
            print(str(cell), end='')
    print('')

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [input() for i in range(h)]
print(s)
way = ((1,1),(1,0),(1,-1),(0,1),(0,-1),(-1,0),(-1,1),(-1,-1))
ans = [[0 for j in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            ans[i][j] = "#"
            continue
        for k in way:
            if not (0 <= i+k[0] < h and 0 <= j+k[1] < w):
                continue
            if s[i+k[0]][j+k[1]] == "#":
                ans[i][j] += 1

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

Submission status: Wrong Answer

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


ans = []
for i in range(H):
    ans.append([])
    row = input()
    for j in row:
        ans[-1].append(j)

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

for y in range(H):
    for x in range(W):
        if ans[y][x] == '#':
            continue
        c = 0
        for dx, dy in dxy:
            if x+dx<0 or x+dx>W-1 or y+dy<0 or y+dy>H-1:
                continue
            if ans[y+dy][x+dx] == '#':
                c += 1
            ans[y][x] = c
        
for row in ans:
    print(''.join(list(map(str, row))))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lis = []
for i in range(n):
    a = list(input())
    for l in range(m):
        lis.append(a[l])
for t in range(n * m):
    if lis[t] == ".":
        cou = 0
          # 左斜め上(下式で最上段か左端に位置しているlis[t]を除く)
        if (t +1) > m and (t +1) %m != 1:  #　　 　　？
            if lis[t-(m+1)] == "#":  #        　　　　 ↖
                cou += 1  #                    　　　　  〇
          # 上(下式で最上段に位置しているlis[t]を除く)
        if (t +1) > m:  #                      　　 　　 ？
            if lis[t - m] == "#":  #       　 　　 　　　↑
                cou += 1  #                    　　 　　 〇
          # 右斜め上(下式で最上段か右端に位置しているlis[t]を除く)
        if (t +1) > m and (t +1) %m != 0:  #    　　　　   ？
            if lis[t -(m-1)] == "#":  #        　　 　　  ↗
              cou += 1  #                      　　 　　 〇
          # 左(左端に位置しているlis[t]を除く)
        if (t +1) %m != 1:
            if lis[t -1] == "#":  #         　　　　 ？←〇
                cou += 1
          # 右(下式で右端に位置しているlis[t]を除く)
        if (t +1) %m != 0:
            if lis[t+1]== "#":  #        　　     　　   〇→？
                cou += 1
          # 下(下式で底に位置しているlis[t]を除く)
        if (t +1 +m) <= n * m:  #           　　    　　 〇
            if lis[t +m] == "#":  #         　　    　　 ↓
                cou += 1  #                  　　   　　 ？
          # 左斜め下(下式で左端か底に位置しているlis[t]を除く)
        if (t +1 +m) <= n * m and (t +1) %m != 1:  # 　　　　〇
            if lis[t +(m-1)]== "#":  #            　　　　  ↙
                cou += 1  #                      　　　　  ？
          # 右斜め下(下式で右端か底に位置しているlis[t]を除く)
        if (t +1 +m) <= n * m and (t +1) %m != 0:  # 　　　　〇
            if lis[t +(m+1)]== "#":  #               　　　　  ↘
                cou += 1  #                            　　　　  ？

        lis[t] = cou

for p in range(n):
    li = []
    for h in range(m):
        li.append(lis[(p * m) + h])
    print("".join(map(str,li)))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lis = []
for i in range(n):
    a = list(input())
    for l in range(m):
        lis.append(a[l])
for t in range(n * m):
    if lis[t] == ".":
        cou = 0
          # 左斜め上
        if (t +1) > m and (t +1) %m != 1:
            if lis[t-(m+1)] == "#":
                cou += 1
          # 上
        if (t +1) > m:
            if lis[t - m] == "#":
                cou += 1
          # 右斜め上
        if (t +1) > m and (t +1) %m != 0:
            if lis[t -(m-1)] == "#":
              cou += 1
          # 左
        if (t +1) %m != 1:
            if lis[t -1] == "#":
                cou += 1
          # 右
        if (t +1) %m != 0:
            if lis[t+1]== "#":
                cou += 1
          # 下
        if (t +1 +m) <= n * m:
            if lis[t +m] == "#":
                cou += 1
          # 左斜め下
        if (t +1 +m) <= n * m and (t +1) %m != 1:
            if lis[t +(m-1)]== "#":
                cou += 1
          # 右斜め下
        if (t +1 +m) <= n * m and (t +1) %m != 0:
            if lis[t +(m+1)]== "#":
                cou += 1

        lis[t] = cou

for p in range(n):
    li = []
    for h in range(m):
        li.append(lis[(p * m) + h])
    print("".join(map(str,li)))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lis = []
for i in range(n):
    a = list(input())
    for l in range(m):
        lis.append(a[l])
for t in range(n * m):
    if lis[t] == ".":
        cou = 0

        if (t +1) > m and (t +1) %m != 1:
            if lis[t-(m+1)] == "#":
                cou += 1

        if (t +1) > m:
            if lis[t - m] == "#":
                cou += 1

        if (t +1) > m and (t +1) %m != 0:
            if lis[t -(m-1)] == "#":
              cou += 1

        if (t +1) %m != 1:
            if lis[t -1] == "#":
                cou += 1

        if (t +1) %m != 0:
            if lis[t+1]== "#":
                cou += 1

        if (t +1 +m) <= n * m:
            if lis[t +m] == "#":
                cou += 1

        if (t +1 +m) <= n * m and (t +1) %m != 1:
            if lis[t +(m-1)]== "#":
                cou += 1

        if (t +1 +m) <= n * m and (t +1) %m != 0:
            if lis[t+(m+1)]== "#":
                cou += 1

        lis[t] = cou

for p in range(n):
    li = []
    for h in range(m):
        li.append(lis[(p * m) + h])
    print("".join(map(str,li)))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lis = []
for i in range(n):
    a = list(input())
    for l in range(m):
        lis.append(a[l])
for t in range(n * m):
    if lis[t] == ".":
        cou = 0
        try:
            if lis[t-(m+1)] == "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t - m] == "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t -(m-1)] == "#":
              cou += 1
        except:
            cou = cou
        try:
            if lis[t -1] == "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t+1]== "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t +m] == "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t +(m-1)]== "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t+(m+1)]== "#":
                cou += 1
        except:
            cou = cou
        lis[t] = cou

for p in range(n):
    li = []
    for h in range(m):
        li.append(lis[(p * m) + h])
    print("".join(map(str,li)))

Submission status: Wrong Answer

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] == ".":
            S[i][j] = 0
if H == 1 and W == 1:
    print(S[0][0])
elif H == 1:
    if S[0][0] == "#":
        if S[0][1]!="#": S[0][1]+= 1
    if S[0][W-1]== "#":
        if S[0][W-2]!="#": S[0][W-2]+= 1
    for i in range(1,H-1):
        if S[0][i-1]!="#": S[0][i-1]+= 1
        if S[0][i+1]!="#": S[0][i+1]+= 1
    
    for i in range(H):
        print("".join(map(str,S[i][:])))


elif W == 1:
    if S[0][0] == "#":
        if S[1][0]!="#": S[1][0]+= 1
    if S[H-1][0]== "#":
        if S[H-2][0]!="#": S[H-2][0]+= 1
    for i in range(1,W-1):
        if S[i-1][0]!="#": S[i-1][0]+= 1
        if S[i+1][0]!="#": S[i+1][0]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

else:
    #4隅の処理
    if S[0][0] == "#":
        if S[1][0] !="#": S[1][0]+= 1
        if S[0][1] !="#": S[0][1]+= 1
        if S[1][1] !="#": S[1][1]+= 1
    if S[0][W-1] == "#":
        if S[0][W-2] !="#": S[0][W-2]+= 1
        if S[1][W-2] !="#": S[1][W-2]+= 1
        if S[1][W-1] !="#": S[1][W-1]+= 1
    if S[H-1][0] == "#":
        if S[H-2][0] !="#": S[H-2][0]+= 1
        if S[H-1][1] !="#": S[H-1][1]+= 1
        if S[H-2][1] !="#": S[H-2][1]+= 1
    if S[H-1][W-1] == "#":
        if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
        if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
        if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
    #端の処理
    for i in range(1,H-1):
        if S[i][0] == "#":
            if S[i-1][0] !="#": S[i-1][0]+= 1
            if S[i+1][0] !="#": S[i+1][0]+= 1
            if S[i][1] !="#": S[i][1]+= 1
            if S[i-1][1] !="#": S[i-1][1]+= 1
            if S[i+1][1] !="#": S[i+1][1]+= 1

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

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

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

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "#":
                if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
                if S[i-1][j] !="#": S[i-1][j]+= 1
                if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
                if S[i][j-1] !="#": S[i][j-1]+= 1
                if S[i][j+1] !="#": S[i][j+1]+= 1
                if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
                if S[i+1][j] !="#": S[i+1][j]+= 1
                if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))
# # def replace(i,j,S):
#     yp = i + 1
#     ym = i - 1
#     xp = j + 1
#     xm = j - 1
#     count = 0 
#     if ym >= 0:
#         if S[ym][j] == "#":
#             count += 1
#         if S[yp][j] == "#":
#             count += 1
#     return S
# # print(S)
# print(S)

Submission status: Wrong Answer

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] == ".":
            S[i][j] = 0
if H == 1 and W == 1:
    print(S[0][0])
elif H == 1:
    if S[0][0] == "#":
        if S[0][1]!="#": S[0][1]+= 1
    if S[0][W-1]== "#":
        if S[0][W-2]!="#": S[0][W-2]+= 1
        print("test")
    for i in range(1,H-1):
        if S[0][i-1]!="#": S[0][i-1]+= 1
        if S[0][i+1]!="#": S[0][i+1]+= 1
    
    for i in range(H):
        print("".join(map(str,S[i][:])))


elif W == 1:
    if S[0][0] == "#":
        if S[1][0]!="#": S[1][0]+= 1
    if S[H-1][0]== "#":
        if S[H-2][0]!="#": S[H-2][0]+= 1
    for i in range(1,W-1):
        if S[i-1][0]!="#": S[i-1][0]+= 1
        if S[i+1][0]!="#": S[i+1][0]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

else:
    #4隅の処理
    if S[0][0] == "#":
        if S[1][0] !="#": S[1][0]+= 1
        if S[0][1] !="#": S[0][1]+= 1
        if S[1][1] !="#": S[1][1]+= 1
    if S[0][W-1] == "#":
        if S[0][W-2] !="#": S[0][W-2]+= 1
        if S[1][W-2] !="#": S[1][W-2]+= 1
        if S[1][W-1] !="#": S[1][W-1]+= 1
    if S[H-1][0] == "#":
        if S[H-2][0] !="#": S[H-2][0]+= 1
        if S[H-1][1] !="#": S[H-1][1]+= 1
        if S[H-2][1] !="#": S[H-2][1]+= 1
    if S[H-1][W-1] == "#":
        if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
        if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
        if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
    #端の処理
    for i in range(1,H-1):
        if S[i][0] == "#":
            if S[i-1][0] !="#": S[i-1][0]+= 1
            if S[i+1][0] !="#": S[i+1][0]+= 1
            if S[i][1] !="#": S[i][1]+= 1
            if S[i-1][1] !="#": S[i-1][1]+= 1
            if S[i+1][1] !="#": S[i+1][1]+= 1

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

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

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

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "#":
                if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
                if S[i-1][j] !="#": S[i-1][j]+= 1
                if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
                if S[i][j-1] !="#": S[i][j-1]+= 1
                if S[i][j+1] !="#": S[i][j+1]+= 1
                if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
                if S[i+1][j] !="#": S[i+1][j]+= 1
                if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))
# # def replace(i,j,S):
#     yp = i + 1
#     ym = i - 1
#     xp = j + 1
#     xm = j - 1
#     count = 0 
#     if ym >= 0:
#         if S[ym][j] == "#":
#             count += 1
#         if S[yp][j] == "#":
#             count += 1
#     return S
# # print(S)
# print(S)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
for x in range(h):
  print("".join([("#" if s[x][y] == "#" else str(sum([ss[max(0,x-1):min(w,x+2)].count("#") for ss in s[max(0,y-1):min(h,y+2)]]))) for y in range(w)]))

Submission status: Wrong Answer

Solution code:
num = [int(x) for x in input().split()]
array = []
for i in range(num[0]):
    array.append(list(input()))


for j in range(num[0]):
    for k in range(num[1]):
        cnt = 0
        if array[j][k] != "#":
            try:
                if array[j-1][k] == "#":
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j-1][k-1] == "#":
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j-1][k+1] == "#":
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j][k-1] == "#":
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j][k+1] == "#":
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j+1][k] == "#":
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j+1][k-1] == "#":
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j+1][k+1] == "#":
                    cnt += 1
            except IndexError:
                pass
            array[j][k] = str(cnt)

for i in array:
    print("".join(i))