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 = list(map(int, input().split()))
s = [input() for i in range(h)]
ans = [[0] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        count = 0
        # print(i, j)
        if s[i][j] == '#':
            ans[i][j] = '#'
            continue
        if i == 0:
            if j == 0:
                if s[i][1] == '#':
                    count += 1
                if s[i+1][0] == '#':
                    count += 1
                if s[i+1][1] == '#':
                    count += 1
            elif j == w-1:
                if s[i][j-1] == '#':
                    count += 1
                if s[i+1][j] == '#':
                    count += 1
                if s[i+1][j-1] == '#':
                    count += 1
            else:
                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
        elif i == h-1:
            if j == 0:
                if s[i][1] == '#':
                    count += 1
                if s[i-1][0] == '#':
                    count += 1
                if s[i-1][1] == '#':
                    count += 1
            elif j == w-1:
                if s[i][j-1] == '#':
                    count += 1
                if s[i-1][j] == '#':
                    count += 1
                if s[i-1][j-1] == '#':
                    count += 1
            else:
                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
        else:
            if j == 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
            elif j == w-1:
                if s[i][j-1] == '#':
                    count += 1
                if s[i-1][j] == '#':
                    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
            else:
                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
                if s[i+1][j-1] == '#':
                    count += 1
                if s[i+1][j] == '#':
                    count += 1
                if s[i+1][j+1] == '#':
                    count += 1
        ans[i][j] = str(count)
for i in range(h):
    print(''.join(ans[i]))

Submission status: Runtime Error

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


def check_num(i, j):
    a = i - 1
    b = i + 1
    c = j - 1
    d = j + 1
    if i == 0:
        a = i
    elif i == h-1:
        b = i
    if j == 0:
        c = j
    elif j == w-1:
        d = j
    count = 0
    for p in range(a, b+1):
        for q in range(c, d+1):
            if p == i and q == j:
                continue
            if mine_area[p][q] == '#':
                count += 1
    return count


for _ in range(h):
    mine_line = list(input())
    mine_area.append(mine_line)

for i in range(h):
    for j in range(w):
        if mine_area[i][j] == '#':
            mine_area[i][j] = '#'
        else:
            mine_area[i][j] = check_num(i, j)

for i in range(h):
    for j in range(w):
        print(mine_area[i][j], end='')
        if j == w-1:
            print()

Submission status: Runtime Error

Solution code:
import numpy as np
h, w = map(int, input().rstrip("\n").split())

array = np.array([[" "] * (w + 2)] + [list(" " + input().rstrip("\n") + " ") for _ in range(h)] + [[" "] * (w + 2)])

list_ = []

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if array[i][j] == "#":
            list_.append("#")
        else:
            list_.append(str(sum(sum(array[i - 1:i + 2, j - 1:j + 2] == "#"))))
    list_.append("\n")
list_.pop()

print("".join(list_))

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):
  ans=[0]*W
  for i in range(W):
    if s[i][j]=="#":
      ans[j]=="#"
      continue
    count=0
    if i > 0:
      if s[i-1][j]=="#":
        count+=1
      if j > 0:
        if s[i-1][j-1]=="#":
          count+=1
      if j < W-1:
        if s[i-1][j+1]=5"#":
          count+=1
    if i<H-1:
      if s[i+1][j]=="#":
        count+=1
      if j >0:
        if s[i+1][j-1]=="#":
          count+=1
      if j<W-1:
        if s[i+1][j+1]=="#":
          count+=1
    if j>0:
      if s[i][j-1]==”#”:
        count+=1
    if j<W-1:
      if s[i][j+1]=="#":
        count+=1
    ans[j]=count
  print("".join(map(str,ans)))

Submission status: Runtime Error

Solution code:
import sys
h, w = map(int,input().split())
s = []
for i in range(h):
    s.append(list(input()))
ans = [[] for _ in range(h)]
if h == 1 and w == 1:
    print(s[0][0])
    sys.exit()
elif h == 1 and w == 2:
    if s[0][0] == ".":
        if s[0][1] == "#":
            ans[0].append(1)
        else:
            ans[0].append(0)
    else:
        ans[0].append("#")
    if s[0][1] == ".":
        if s[0][0] == "#":
            ans[0].append(1)
        else:
            ans[0].append(0)
    else:
        ans[0].append("#")
    print(ans[0][0] , end="")
    print(ans[0][1])
    sys.exit()
elif h == 2 and w == 1:
    if s[0][0] == ".":
        if s[1][0] == "#":
            ans[0].append(1)
        else:
            ans[0].append(0)
    else:
        ans[0].append("#")
    if s[1][0] == ".":
        if s[0][0] == "#":
            ans[1].append(1)
        else:
            ans[1].append(0)
    else:
        ans[1].append("#")
    print(ans[0][0])
    print(ans[1][0])
    sys.exit()


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

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

Submission status: Runtime Error

Solution code:
h, w = map(int,input().split())
s = []
for i in range(h):
    s.append(list(input()))
ans = [[] for _ in range(h)]
for i in range(h):
    if i == 0:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j+1] == "#":
                        count += 1
                    if s[i+1][j+1] == "#":
                        count += 1
                    if s[i+1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j-1] == "#":
                        count += 1
                    if s[i+1][j-1] == "#":
                        count += 1
                    if s[i+1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j-1] == "#":
                        count += 1
                    if s[i][j+1] == "#":
                        count += 1
                    if s[i+1][j-1] == "#":
                        count += 1
                    if s[i+1][j] == "#":
                        count += 1
                    if s[i+1][j+1] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
    elif i == h-1:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j+1] == "#":
                        count += 1
                    if s[i-1][j+1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j-1] == "#":
                        count += 1
                    if s[i-1][j-1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j-1] == "#":
                        count += 1
                    if s[i][j+1] == "#":
                        count += 1
                    if s[i-1][j-1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    if s[i-1][j+1] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
    else:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    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
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    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
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 0
                    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
                    if s[i-1][j-1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    if s[i-1][j+1] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")

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

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
list2=[[0 for i in range(w)]for j in range(h)]
list1 =  [input().split() for _ in range(w)]
dx=[-1,-1,-1,0,0,1,1,1]
dy=[-1,0,1,-1,1,-1,0,1]
for j in range(h):
  for i in range(w):
   if list1[j][i]=="#":
    for k in range(8):
     xk=i+dx[k]
     yk=j+dy[k]
     if xk>=0 and xk<w and yk>=0 and yk<h:
      list2[yk][xk]+=1
for j in range(h):
  for i in range(w):
    if list1[j][i]=="#":
      list2[j][i]="#"
    else:
      list2[j][i]=str(list2[j][i])
for j in range(h):
 print("".join(list2[j]))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
list2=[[0 for i in range(w)]for j in range(h)]
list1 =  [input().split() for _ in range(h)]
dx=[-1,-1,-1,0,0,1,1,1]
dy=[-1,0,1,-1,1,-1,0,1]
for j in range(h):
  for i in range(w):
   if list1[j][i]=="#":
    for k in range(8):
     xk=i+dx[k]
     yk=j+dy[k]
     if xk>=0 and xk<w and yk>=0 and yk<h:
      list2[yk][xk]+=1
for j in range(h):
  for i in range(w):
    if list1[j][i]=="#":
      list2[j][i]="#"
    else:
      list2[j][i]=str(list2[j][i])
for j in range(h):
 print("".join(list2[j]))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
list2=[[0 for i in range(w)]for j in range(h)]
list1 =  [for _ in range(h) input().split()[]
dx=[-1,-1,-1,0,0,1,1,1]
dy=[-1,0,1,-1,1,-1,0,1]
for j in range(h):
  for i in range(w):
   if list1[j][i]=="#":
    for k in range(8):
     xk=i+dx[k]
     yk=j+dy[k]
     if xk>=0 and xk<w and yk>=0 and yk<h:
      list2[yk][xk]+=1
for j in range(h):
  for i in range(w):
    if list1[j][i]=="#":
      list2[j][i]="#"
    else:
      list2[j][i]=str(list2[j][i])
for j in range(h):
 print("".join(list2[j]))

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
H,W=map(int,input().split())
boxes=list()
boxes[0]=[0]*W
boxes[H+1]=[0]*W
for i in range(H):
  boxes[i+1]=list()
  boxes[i+1][0]=0
  bowes[i+1][W+1]=0
  x=input()
  for j in range(W):
    if x[j]=='.':
      boxes[i+1][j+1]=0
    elif x[j]=='#':
      boxes[i+1][j+1]=1
answer=list()
for n in range(H):
  answer[n]=list()
  for k in range(W):
    count=0
    if boxes[n][k]==1:
      count+=1
    if boxes[n][k+1]==1:
      count+=1
    if boxes[n][k+2]==1:
      count+=1
    if boxes[n+1][k]==1:
      count+=1
    if boxes[n+1][k+2]==1:
      count+=1
    if boxes[n+2][k]==1:
      count+=1
    if boxes[n+2][k+1]==1:
      count+=1
    if boxes[n+2][k+2]==1:
      count+=1
    if count==0:
      answer[n][k]='#'
	else:
      answer[n][k]=str(count)
 for v in range(H):
  answe=''.join(answer[v])
  print(answe)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[]
c = [[0] * w for i in range(h)]
s.append(input())
for i in range(h):
    if i<h-1:
        s.append(input())
    for j in range(w):
        p=[]
        if s[i][j] == '#':
          c[i][j] = '#'
        else:
          if i ==0:
                if j==0:
                    p=s[i][j:j+2]+s[i+1][j:j+2]
                elif 0<j<w-1:
                    p=s[i][j-1:j+2]+s[i+1][j-1:j+2]
                else:
                    p=s[i][j-1:j+1]+s[i+1][j-1:j+1]
          elif 0<i<h-1:
                if j==0:
                    p = s[i-1][j:j+2]+s[i][j:j+2]+s[i+1][j:j+2]
                elif 0<j<w-1:
                    p = s[i-1][j-1:j+2]+s[i][j-1:j+2]+s[i+1][j-1:j+2]
                else:
                    p=s[i-1][j-1:j+1]+s[i][j-1:j+1]+s[i+1][j-1:j+1]
          else:
                if j==0:
                    p = s[i-1][j:j+2]+s[i][j:j+2]
                elif 0<j<w-1:
                    p = s[i-1][j-1:j+2]+s[i][j-1:j+2]
                else:
                    p=s[i-1][j-1:j+1]+s[i][j-1:j+1]
          c[i][j] = p.count('#')
       
for i in range(h):
  for j in range(w):
    print(c[i][j],end="")
  print('',sep='\n')

Submission status: Runtime Error

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

        if s[i][j] == '#':
            c[i][j] = '#'
        else:
            c[i][j] = p.count('#')
       
for i in range(h):
  for j in range(w):
    print(c[i][j],end="")
  print('',sep='\n')

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[]
c = [[0] * w for i in range(h)]
s.append(input())
for i in range(h):
    if i<h-1:
        s.append(input())
    for j in range(w):
        p=[]
        if i ==0 and j==0:
            p=s[i][j:j+2]+s[i+1][j:j+2]
        elif i==0 and 0<j<w-1:
            p=s[i][j-1:j+2]+s[i+1][j-1:j+2]
        elif i==0 and j==w-1:
            p=s[i][j-1:j+1]+s[i+1][j-1:j+1]
        elif 0<i<h-1 and j==0:
            p = s[i-1][j:j+2]+s[i][j:j+2]+s[i+1][j:j+2]
        elif i==h-1 and j==0:
            p = s[i-1][j:j+2]+s[i][j:j+2]
        elif 0<i<h-1 and j==w-1:
            p=s[i-1][j-1:j+1]+s[i][j-1:j+1]+s[i+1][j-1:j+1]
        elif i==h-1 and 0<j<w-1:
            p = s[i-1][j-1:j+2]+s[i][j-1:j+2]
        elif i==h-1 and j==w-1:
            p=s[i-1][j-1:j+1]+s[i][j-1:j+1]
        else:
            p = s[i-1][j-1:j+2]+s[i][j-1:j+2]+s[i+1][j-1:j+2]
        if s[i][j] == '#':
            c[i][j] = '#'
        else:
            c[i][j] = p.count('#')
       
for i in range(h):
  for j in range(w):
    print(c[i][j],end="")
  print('',sep='\n')

Submission status: Runtime Error

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

for i in range(h):
    for j in range(w):
        p=[]
        if i ==0 and j==0:
            p=s[i][j:j+2]+s[i+1][j:j+2]
        elif i==0 and 0<j<w-1:
            p=s[i][j-1:j+2]+s[i+1][j-1:j+2]
        elif i==0 and j==w-1:
            p=s[i][j-1:j+1]+s[i+1][j-1:j+1]
        elif 0<i<h-1 and j==0:
            p = s[i-1][j:j+2]+s[i][j:j+2]+s[i+1][j:j+2]
        elif i==h-1 and j==0:
            p = s[i-1][j:j+2]+s[i][j:j+2]
        elif 0<i<h-1 and j==w-1:
            p=s[i-1][j-1:j+1]+s[i][j-1:j+1]+s[i+1][j-1:j+1]
        elif i==h-1 and 0<j<w-1:
            p = s[i-1][j-1:j+2]+s[i][j-1:j+2]
        elif i==h-1 and j==w-1:
            p=s[i-1][j-1:j+1]+s[i][j-1:j+1]
        else:
            p = s[i-1][j-1:j+2]+s[i][j-1:j+2]+s[i+1][j-1:j+2]
        if s[i][j] == '#':
            c[i][j] = '#'
        else:
            c[i][j] = p.count('#')
       
for i in range(h):
  for j in range(w):
    print(c[i][j],end="")
  print('',sep='\n')

Submission status: Runtime Error

Solution code:
h,w = map(int, input().split())
m = []
for y in range(h):
  s = input()
  l = []
  for i in s:
    if i == '#':
      l.append(1)
    else:
      l.append(0)

print(m)
for y in range(h):
  s = ''
  for x in range(w):
    bomb = 0
    for i,j in ((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)):
      if y+j < 0 or x+i < 0 or x+i >= w or y+j >= h:
        continue
      if m[y+j][x+i] == 1:
        bomb += 1
    if m[y][x] == 1:
      bomb = '#'
    s += str(bomb)
  print(s)

Submission status: Runtime Error

Solution code:
H, W = map(int,input().split())
S = [list(input()) for i in range(H)]
ansm = [[""]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j]=="#":
            ansm[i][j]="#"
        else:
            if i>0 and j>0:
                if S[i-1][j-1]=="#":
                    cnt +=1
            if i>0:
                if S[i-1][j]=="#":
                    cnt+=1
            if i>0 and j<(W-1):
                if S[i-1][j+1]=="#":
                    cnt+=1
            if j>0:
                if S[i][j-1]=="#":
                    cnt+=1
            if j<W-1:
                if S[i][j+1]=="#":
                    cnt+=1
            if i<H-1 and j>0:
                if S[i+1][j-1]=="#":
                    cnt+=1
            if i<H-1:
                if S[i+1][j]=="#":
                    cnt+=1
            if i<H-1 and j<(W-1):
                if S[i+1][j+1]=="#":
                    cnt+=1
            ansm[i][j]=str(cnt)
ans = [""]*W
for i in range(H):
    for j in range(W):
        ans[i]=ans[i]+ansm[i][j]
for i in range(H):
    print(ans[i])

Submission status: Runtime Error

Solution code:
H, W = map(int,input().split())
S = [list(input()) for i in range(H)]
ansm = [[""]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j]=="#":
            ansm[i][j]="#"
        else:
            if i>0 and j>0:
                if S[i-1][j-1]=="#":
                    cnt +=1
            if i>0:
                if S[i-1][j]=="#":
                    cnt+=1
            if i>0 and j<(W-1):
                if S[i-1][j+1]=="#":
                    cnt+=1
            if j>0:
                if S[i][j-1]=="#":
                    cnt+=1
            if j<W-1:
                if S[i][j+1]=="#":
                    cnt+=1
            if i<H-1 and j>0:
                if S[i+1][j-1]=="#":
                    cnt+=1
            if i<H-1:
                if S[i+1][j]=="#":
                    cnt+=1
            if i<H-1 and j<(W-1):
                if S[i+1][j+1]=="#":
                    cnt+=1
            ansm[i][j]=str(cnt)
ans = [""]*3
for i in range(H):
    for j in range(W):
        ans[i]=ans[i]+ansm[i][j]
for i in range(H):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import itertools

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

for i in range(w):
    for j in range(h):
        cnt=0
        if s[i][j]!="#":
            for dx,dy in itertools.product([-1,0,1],repeat=2):
                x=i+dx
                y=j+dy
                if x<0 or x>=w:
                    continue
                if y<0 or y>=h:
                    continue
                if s[x][y]=="#":
                    cnt+=1
            s[i][j]=str(cnt)

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

Submission status: Runtime Error

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

for i in range(h):
    s[i].insert(0, '.')
    s[i].insert(-1,'.')

s.insert(0, ['.', '.','.', '.', '.', '.', '.'])
s.insert(-1,['.', '.','.', '.', '.', '.', '.'])


for i in range(1,h+1):
    for j in range(1,w+1):
        k = s[i-1][j-1:j+2].count('#')+s[i][j-1].count('#')+s[i][j+1].count('#')+s[i+1][j-1:j+2].count('#')
        if k == 0:
            ans.append('#')
        else:
            ans.append(str(k))

for i in range(h):
    print(''.join(ans[i*w:i*w+w]))