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:
import sys
input = sys.stdin.readline

h, m = map(int, input().split())
s = [''] * h
ans = [[0] * m for i in range(h)]
for i in range(h):
    s[i] = input()
for i in range(1, h - 1):
    for j in range(1, m - 1):
        cnt = 0
        if s[i][j] == '.':
            if s[i - 1][j - 1] == '#':
                cnt += 1
            if s[i - 1][j] == '#':
                cnt += 1
            if s[i - 1][j + 1] == '#':
                cnt += 1
            if s[i][j - 1] == '#':
                cnt += 1
            if s[i + 1][j + 1] == '#':
                cnt += 1
            if s[i][j + 1] == '#':
                cnt += 1
            if s[i + 1][j - 1] == '#':
                cnt += 1
            if s[i + 1][j] == '#':
                cnt += 1
        else:
            cnt = 10
        ans[i][j] += cnt

for i in range(1, m - 1):
    cnt_1, cnt_2 = 0, 0
    if s[0][i] == '.':
        if s[0][i - 1] == '#':
            cnt_1 += 1
        if s[1][i + 1] == '#':
            cnt_1 += 1
        if s[0][i + 1] == '#':
            cnt_1 += 1
        if s[1][i - 1] == '#':
            cnt_1 += 1
        if s[1][i] == '#':
            cnt_1 += 1
    else:
        cnt_1 = 10
    if s[h - 1][i] == '.':
        if s[h - 1][i - 1] == '#':
            cnt_2 += 1
        if s[h - 2][i + 1] == '#':
            cnt_2 += 1
        if s[h - 1][i + 1] == '#':
            cnt_2 += 1
        if s[h - 2][i - 1] == '#':
            cnt_2 += 1
        if s[h - 2][i] == '#':
            cnt_2 += 1
    else:
        cnt_2 = 10
    ans[0][i] = cnt_1
    ans[h - 1][i] = cnt_2
for i in range(1, h - 1):
    cnt_1, cnt_2 = 0, 0
    if s[i][0] == '.':
        if s[i - 1][0] == '#':
            cnt_1 += 1
        if s[i - 1][1] == '#':
            cnt_1 += 1
        if s[i + 1][1] == '#':
            cnt_1 += 1
        if s[i][1] == '#':
            cnt_1 += 1
        if s[i + 1][0] == '#':
            cnt_1 += 1
    else:
        cnt_1 = 10
    if s[i][h - 1] == '.':
        if s[i - 1][m - 1] == '#':
            cnt_2 += 1
        if s[i - 1][m - 2] == '#':
            cnt_2 += 1
        if s[i + 1][m - 2] == '#':
            cnt_2 += 1
        if s[i][m - 2] == '#':
            cnt_2 += 1
        if s[i + 1][m - 1] == '#':
            cnt_2 += 1
    else:
        cnt_2 = 10
    ans[i][0] = cnt_1
    ans[i][m - 1] = cnt_2
    cnt_1, cnt_2 = 0, 0
if s[0][0] == '.':
    if s[1][0] == '#':
        ans[0][0] += 1
    if s[0][1] == '#':
        ans[0][0] += 1
    if s[1][1] == '#':
        ans[0][0] += 1
else:
    ans[0][0] = 10
if s[0][m - 1] == '.':
    if s[1][m - 1] == '#':
        ans[0][m - 1] += 1
    if s[0][m - 2] == '#':
        ans[0][m - 1] += 1
    if s[1][m - 2] == '#':
        ans[0][m - 1] += 1
else:
    ans[0][m - 1] = 10
if s[h - 1][0] == '.':
    if s[h - 2][0] == '#':
        ans[h - 1][0] += 1
    if s[h - 1][1] == '#':
        ans[h - 1][0] += 1
    if s[h - 2][1] == '#':
        ans[h - 1][0] += 1
else:
    ans[h - 1][0] = 10
if s[h - 1][m - 1] == '.':
    if s[h - 2][m - 1] == '#':
        ans[h - 1][m - 1] += 1
    if s[h - 1][m - 2] == '#':
        ans[h - 1][m - 1] += 1
    if s[h - 2][m - 2] == '#':
        ans[h - 1][m - 1] += 1
else:
    ans[h - 1][m - 1] = 10
for i in range(h):
    for j in range(m):
        if ans[i][j] == 10:
            print('#', end='')
        else:
            print(ans[i][j], end='')
    print()

Submission status: Runtime Error

Solution code:
6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    h, w = map(int, input().split())
    s = [[i for i in input()] for _ in range(w)]

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

    def put(s, i, j):
        if 0 <= i-1:
            s[i-1][j] += 1
        if 0 <= i-1 and 0 <= j-1:
            s[i-1][j-1] += 1
        if 0 <= j-1:
            s[i][j-1] += 1
        if i+1 < h:
            s[i+1][j] += 1
        if i+1 < h and j+1 < w:
            s[i+1][j+1] += 1
        if j+1 < w:
            s[i][j+1] += 1
        if 0 <= i-1 and j+1 < w:
            s[i-1][j+1] += 1
        if 0 <= j-1 and i+1 < h:
            s[i+1][j-1] += 1

        return s
    
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans = put(ans, i, j)
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans[i][j] = '#'
    
    ans_str = ''
    for i in range(h):
        for j in range(w):
            ans_str += str(ans[i][j])
        ans_str += '\n'
    ans_str = ans_str[:-1]
    print(ans_str)
    



class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 5
.....
.#.#.
....."""
        output = """11211
1#2#1
11211"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 5
#####
#####
#####"""
        output = """#####
#####
#####"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#..."""
        output = """#####3
#8#7##
####5#
4#65#2
#5##21
#4#310"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
ans = [[0]*5 for i in range(h)]
for i in range(h):
         for j in range(w):
                  if s[i][j] == "#":
                           for k in range(max(0,i-1),min(h,(i+1)+1)):
                                    for l in range(max(0,j-1),min(w,(j+1)+1)):
                                             ans[k][l] += 1
for i in range(h):
         for j in range(w):
                  if s[i][j] == "#":
                           ans [i][j] = "#"
for i in range(h):
         answer = list(map(str,ans[i]))
         answer=''.join(answer)
         print(answer)

Submission status: Runtime Error

Solution code:
def masucnt(masu,i,j):
  s="."
  try:
    s=masu[i][j]
  except:
    s=""
  return 1 if s=="#" else 0

h,w=map(int,input().split())
masu=[""]*h

for i in range(h):
  masu[i]=input().strip()

for i in range(h):
  for j in range(len(masu[i]))
    n=0
    s=masu[i][j]
    if s=="#"
      print("#",end="")
    else:
      n+=masucnt(masu,i-1,j-1)
      n+=masucnt(masu,i-1,j)
      n+=masucnt(masu,i-1,j+1)
      n+=masucnt(masu,i,j-1)
      n+=masucnt(masu,i,j+1)
      n+=masucnt(masu,i+1,j-1)
      n+=masucnt(masu,i+1,j)
      n+=masucnt(masu,i+1,j+1)
      print(n,end="")
  print("")

Submission status: Runtime Error

Solution code:
6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Submission status: Runtime Error

Solution code:
'''
8方向走査

'.' は 空き、 '#' は爆弾の1行から複数行の「文字列」が与えられるので
Minesweeperのように、空きのマスに周囲8方向にある爆弾の数を埋める。
コンテスト中に実装すると、間違えることがあるので、dy や dx それらを使う部分は
予めスニペットなどで直ぐに読み出せるように準備しておく。
8方向走査の他、4方向走査、2方向走査も出題されるので準備しておく。
「文字列」 は 作成後に変更することができない。

ここでは、「文字列」 を 文字 の 「リスト」 とすることで 変更を可能にしている。
行継続文字はバックスラッシュ(円記号)

例えば if 条件 が長くなった場合、バックスラッシュを入れることで
その後の改行が無視されて行が継続していると見なされる。
上記のプログラム
if ny < 0 or H <= ny\
or nx < 0 or W <= nx:
バックスラッシュ無しで改行の場合、SyntaxError になる。
''(空文字).join(「リスト」)

「リスト」の 各要素 を 区切り文字 なし で結合して出力することができる。

' '(半角スペース).join(「リスト」)とすれば
「リスト」の 各要素 を 区切り文字 半角スペース で結合して出力することができる。
'''

# 下、右、上、左、右下、右上、左下、左上
dy = [1, 0, -1, 0, 1, -1, 1, -1]
dx = [0, 1, 0, -1, 1, 1, -1, -1]

H, W = map(int, input().split())
field = []

for i in range(H):
    field.append(list(input()))

for y in range(H):
    for x in range(W):

        if field[y][x] == '#':
            continue

        cnt = 0

        for k in range(8):
            ny = y + dy[k]
            nx = x + dx[k]

            if ny < 0 or H <= ny:
                    or nx < 0 or W <= nx:
                continue

            if field[ny][nx] == '#':
                cnt += 1

        field[y][x] = str(cnt)

for y in range(H):
    print("".join(field[y]))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
field=[["."]*(W+2)]
for i in range(H):
    field.append(["."])
    field[-1].extend(list(input()))　#ここらへんミスりそうでこわひ
    field[-1].append(".")
field.append(["."]*(W+2))
print(field)
for i in range(1,H+1):
    for j in range(1,W+1):
        if field[i][j]=="#":
            print("#",end="")
        else:
            cnt=0
            for dy in range(-1,2):
                for dx in range(-1,2):
                    if field[i+dy][j+dx]=="#":
                        cnt+=1
            print(cnt,end="")
    print("")

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
#左右に'.'を余分に付けて標準入力からリストを作成
grid = [list('.'+input()+'.') for i in range(H)]
#上下にも付けてやる
grid.insert(0, list('.' * (W + 2)))　#上
grid.append(list('.' * (W + 2)))　#下
#答えを入れるリスト
ans = []

#付け足した行列はカウントしない
for i in range(1, H + 1):
  for j in range(1, W + 1):
    if grid[i][j] == '#':
      ans.append('#')
    else:
      #8方向にある'#'の数をカウント
      #付け足したのは'.'なので条件分岐は不要
      #str型で出力しなければjoinできない
      ans.append(str((grid[i-1][j-1:j+2]+grid[i][j-1:j+2]+grid[i+1][j-1:j+2]).count('#')))
  #一行終わったので改行を入れる
  ans.append('\n')
print(''.join(ans)) #リストをつないで出力

Submission status: Runtime Error

Solution code:
import math
import itertools
import statistics
#n = int(input())
#n, d = list(input().split())
h, w = list(map(int, input().split()))
s = []
for i in range(h):
    s.append(input())

count = 0

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

Submission status: Runtime Error

Solution code:
import math
import itertools
import statistics
#n = int(input())
#n, d = list(input().split())
h, w = list(map(int, input().split()))
s = []
for i in range(h):
    s.append(input())

count = 0

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

Submission status: Runtime Error

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

check_i = []
check_j = []
for i in range(H):
    if i == 0:
        check_i = [0, 1]
    elif i == H-1:
        check_i = [i-1, i]
    else:
        check_i = [i-1, i, i+1]
    for j in range(W):
        if S[i][j] == "#":
            continue

        if j == 0:
            check_j = [0, 1]
        elif j == W-1:
            check_j = [j-1, j]
        else:
            check_j = [j-1, j, j+1]
        
        count = 0
        for k in check_i:
            for l in check_j:
                if S[k][l] == "#":
                    count += 1
        S[i][j] = str(count)

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=["!"*(m+2)]+["!"+input()+"!" for i in range(n)]+["!"*(m+2)]
ans=["0"*m for c in range(n)] 
for i in range(n):
  for d in range(m):
    if l[i+1][d+1]==".":
      ret=0
      for a,b in[[1,1],[1,0],[0,1],[-1,-1],[-1,0],[0,-1],[1,-1],[-1,1]] :
        if l[i+1+a][d+1+b]=="#":
          ret+=1
        ans[i][d]=str(ret)
    else:ans[i][d]="#"
for i in range(n):
  print(ans[i])

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
b=[]
for i in range(n):
  b.append(input())
ans=[[0]*m for i in range(n)]
d=[-1,0,1]
for i in range(n):
  for j in range(m):
    if b[i][j]=='#':
      for x in d:
        for y in d:
          X=i+x
          Y=j+y
          if 0 <= X < n and 0 <= Y < m:
            ans[X][Y]+=1
import sys
for i in range(n):
  for j in range(m):
    if b[i][j]=='#':
      sys.stdout.write('#')
    else:
      sys.stdout.write(ans[i][j])
  sys.stdout.write('\n')

Submission status: Runtime Error

Solution code:
import math
import string


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//math.factorial(n-r)*math.factorial(r)


h, w = map(int, input().split())
a = [None]*h
for i in range(h):
    a[i] = input()
# print(a)

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

for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            print('#', end="")
        else:
            count = 0
            for k in range(8):
                x = i+dx[k]  # (i,j)の位置からk移動させたときのx座標
                y = j+dy[k]  # (i,j)の位置からk移動させたときのy座標
                if (x <= 0 and x < h)and(y <= 0 and y < w)and(a[x][y] == '#'):
                    # x,yが盤面の中に入っていて'#'のとき
                    continue
                if a[x][y] == '#':
                    count += 1
            print(count, end="")
    print('\n', end="")

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
hwl = []
for _ in range(h):
  l = list(input())
  hwl.append(l)
  
for i in range(h):
  for j in range(w):
    if hwl[i][j] == ".":
      cnt = 0
      if i != 0 and j !=0 and hwl[i-1][j-1] == "#":
        cnt +=1
      
      if i != 0 and hwl[i-1][j] == "#":
        cnt +=1
      
      if i != 0 and hwl[i-1][j+1] == "#":
        cnt +=1
        
      if j !=0 and hwl[i][j-1] == "#":
        cnt +=1
      
      if j < w-1 and hwl[i][j+1] == "#":
        cnt +=1
        
      if i < h-1 and j != 0 and hwl[i+1][j-1] == "#":
        cnt +=1
      
      if i <h-1 and hwl[i+1][j] == "#":
        cnt +=1
        
      if i < h-1 and j < w-1 and hwl[i+1][j+1] == "#":
        cnt +=1
        
      hwl[i][j] = str(cnt)

  print("".join(hwl[i]))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
lis=[["."]*(W+2)]
for i in range(H):
    lis+=[["."]+list(input())+["."]]
lis += [["."]*(W+2)]
for w in range(1, H+1):
    for h in range(1, W+1):
        x=0
        if lis[h][w] == ".":
            if lis[h+1][w-1] == "#":
                x += 1
            if lis[h+1][w] == "#":
                x += 1
            if lis[h+1][w+1] == "#":
                x += 1
            if lis[h][w-1] == "#":
                x += 1
            if lis[h][w+1] == "#":
                x += 1
            if lis[h-1][w-1] == "#":
                x += 1
            if lis[h-1][w] == "#":
                x += 1
            if lis[h-1][w+1] == "#":
                x += 1
            lis[h][w] = x
lis=lis[1:-1]
for i in lis:
    print(*i[1:-1], sep="")
print("")

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
lis=[["."]*(W+2)]
for i in range(H):
    lis+=[["."]+list(input())+["."]]
lis += [["."]*(W+2)]
for w in range(1, H+1):
    for h in range(1, W+1):
        x=0
        if lis[h][w] == ".":
            if lis[h+1][w-1] == "#":
                x += 1
            if lis[h+1][w] == "#":
                x += 1
            if lis[h+1][w+1] == "#":
                x += 1
            if lis[h][w-1] == "#":
                x += 1
            if lis[h][w+1] == "#":
                x += 1
            if lis[h-1][w-1] == "#":
                x += 1
            if lis[h-1][w] == "#":
                x += 1
            if lis[h-1][w+1] == "#":
                x += 1
            lis[h][w] = x
lis=lis[1:-1]
for i in lis:
    print(*i[1:-1], sep="")
print()

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
lis=[["."]*(W+2)]
for i in range(H):
    lis+=[["."]+list(input())+["."]]
lis += [["."]*(W+2)]
for w in range(1, H+1):
    for h in range(1, W+1):
        x=0
        if lis[h][w] == ".":
            if lis[h+1][w-1] == "#":
                x += 1
            if lis[h+1][w] == "#":
                x += 1
            if lis[h+1][w+1] == "#":
                x += 1
            if lis[h][w-1] == "#":
                x += 1
            if lis[h][w+1] == "#":
                x += 1
            if lis[h-1][w-1] == "#":
                x += 1
            if lis[h-1][w] == "#":
                x += 1
            if lis[h-1][w+1] == "#":
                x += 1
            lis[h][w] = x
lis=lis[1:-1]
for i in lis:
    print(*i[1:-1], sep="")

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
lis=[["."]*(W+2)]
for i in range(H):
    lis+=[["."]+list(input())+["."]]
lis += [["."]*(W+2)]
print(lis)
for w in range(1, H+1):
    for h in range(1, W+1):
        x=0
        if lis[h][w] == ".":
            if lis[h+1][w-1] == "#":
                x += 1
            if lis[h+1][w] == "#":
                x += 1
            if lis[h+1][w+1] == "#":
                x += 1
            if lis[h][w-1] == "#":
                x += 1
            if lis[h][w+1] == "#":
                x += 1
            if lis[h-1][w-1] == "#":
                x += 1
            if lis[h-1][w] == "#":
                x += 1
            if lis[h-1][w+1] == "#":
                x += 1
            lis[h][w] = x
lis=lis[1:-1]
for i in lis:
    print(*i[1:-1], sep="")

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
a = ["." for i in range(w+2)]
dot_map = [a]
for _ in range(h):
    l = list(input())
    l.insert(0, ".")
    l.append(".")
    dot_map.append(l)
dot_map.append(a)

for i in range(1, h+1):
    for j in range(1,w+1):
        if dot_map[i][j] == ".":
            counter = 0
            if dot_map[i-1][j-1] == "#":
                counter += 1
            if dot_map[i-1][j] == "#":
                counter += 1
            if dot_map[i-1][j+1] == "#":
                counter += 1
            if dot_map[i][j-1] == "#":
                counter += 1
            if dot_map[i][j+1] == "#":
                counter += 1
            if dot_map[i+1][j-1] == "#":
                counter += 1
            if dot_map[i+1][j] == "#":
                counter += 1
            if dot_map[i+1][j+1] == "#":
                counter += 1
            dot_map[i][j] = counter

for i in range(1, h+1):
    ans = dot_map[i]
    print("".join(ans[1:w+1]))

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
a = ["." for i in range(w+2)]
dot_map = [a]
for _ in range(h):
    l = list(input())
    l.insert(0, ".")
    l.append(".")
    dot_map.append(l)
dot_map.append(a)

for i in range(1, h+1):
    for j in range(1,w+1):
        if dot_map[i][j] == ".":
            counter = 0
            if dot_map[i-1][j-1] == "#":
                counter += 1
            if dot_map[i-1][j] == "#":
                counter += 1
            if dot_map[i-1][j+1] == "#":
                counter += 1
            if dot_map[i][j-1] == "#":
                counter += 1
            if dot_map[i][j+1] == "#":
                counter += 1
            if dot_map[i+1][j-1] == "#":
                counter += 1
            if dot_map[i+1][j] == "#":
                counter += 1
            if dot_map[i+1][j+1] == "#":
                counter += 1
            dot_map[i][j] = counter

for i in range(1, h+1):
    ans = dot_map[i]
    print("".join(ans[1:w+1]))

Submission status: Runtime Error

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

for i in range(h):
    for j in range(w):
        if hwl[i][j] == ".":
            cnt = 0
            if i != 0 and j != 0 and hwl[i-1][j-1] == "#":
                cnt += 1
            if i != 0 and hwl[i-1][j] == "#":
                cnt += 1
            if i != 0 and j < w-1 and hwl[i-1][j+1] == "#":
                cnt += 1
            if j != 0 and hwl[i][j-1] == "#":
                cnt += 1
            if j < w-1 and hwl[i][j+1] == "#":
                cnt += 1
            if i < h-1 and j != 0 and hwl[i+1][j-1] == "#":
                cnt += 1
            if i < h-1 and hwl[i+1][j] == "#":
                cnt += 1
            if i < h-1 and j < h-1 and hwl[i+1][j+1] == "#":
                cnt += 1
            hwl[i][j] = str(cnt)
    print("".join(hwl[i]))

Submission status: Runtime Error

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

for i in range(h):
    for j in range(w):
        if hwl[i][j] == ".":
            cnt = 0
            if i != 0 and j != 0 and hwl[i-1][j-1] == "#":
                cnt += 1
            if i != 0 and hwl[i-1][j] == "#":
                cnt += 1
            if i != 0 and j < w-1 and hwl[i-1][j+1] == "#":
                cnt += 1
            if j != 0 and hwl[i][j-1] == "#":
                cnt += 1
            if hwl[i][j] == "#":
                cnt += 1
            if j < w-1 and hwl[i][j+1] == "#":
                cnt += 1
            if i < h-1 and j != 0 and hwl[i+1][j-1] == "#":
                cnt += 1
            if i < h-1 and hwl[i+1][j] == "#":
                cnt += 1
            if i < h-1 and j < h-1 and hwl[i+1][j+1] == "#":
                cnt += 1
            hwl[i][j] = str(cnt)
    print("".join(hwl[i]))

Submission status: Runtime Error

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

dx = [0,1,1,1,0,-1,-1,-1]
dy = [1,1,0,-1,-1,-1,0,1]
for i in range(H):
    for j in range(W):
        for k in range(8):
            nx = j + dx[k]
            ny = i + dy[k]
            if nx >= 0 and ny >= 0 and ny < H and nx < W:
                if S[ny][nx] == "#":
                    S[i][j] += 1
for i in range(H):
    l = map(str,S[i])
    ans = "".join(l)
    print(ans)