Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.  

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).  

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.  

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.  

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310



Sample input:
3 5
.....
.#.#.
.....


Sample output:
11211
1#2#1
11211

Submission status: Runtime Error

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

board = []

for i in range(h):
    line = input()
    board.append(line)

new_board = []

for y in range(h):
    temp_text = ''
    if y == 0:
        for x in range(w):
            if board[y][x] == '#':
                temp_text = temp_text + '#'

            else:
                if x == 0:
                    check_text = board[y][x:x+2] + board[y+1][x:x+2]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

                elif x == w-1:
                    check_text = board[y][x-1:x+1] + board[y+1][x-1:x+1]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

                else:
                    check_text = board[y][x-1:x+2] + board[y+1][x-1:x+2]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

    elif y == h-1:
        for x in range(len(board[y])):
            if board[y][x] == '#':
                temp_text = temp_text + '#'

            else:
                if x == 0:
                    check_text = board[y-1][x:x+2] + board[y][x:x+2]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

                elif x == w - 1:
                    check_text = board[y-1][x-1:x+1] + board[y][x-1:x+1]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

                else:
                    check_text = board[y-1][x-1:x+2] + board[y][x-1:x+2]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

    else:
        for x in range(len(board[y])):
            if board[y][x] == '#':
                temp_text = temp_text + '#'

            else:
                if x == 0:
                    check_text = board[y-1][x:x+2] + board[y][x:x+2] + board[y+1][x:x+2]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

                elif x == w - 1:
                    check_text = board[y-1][x-1:x+1] + board[y][x-1:x+1] + board[y+1][x-1:x+1]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

                else:
                    check_text = board[y-1][x-1:x+2] + board[y][x-1:x+2] + board[y+1][x-1:x+2]
                    bomb_count = check_text.count('#')
                    temp_text = temp_text + str(bomb_count)

    new_board.append(temp_text)

for text in new_board:
    print(text)

Submission status: Runtime Error

Solution code:
a,b = list(map(int, input().split()))
s = [input() for i in range(a)]
num = []
for i in range(a):
    ss = s[i]
    numm = []
    for j in range(b):
        if  ss[j]== ".":
            numm.append(0)
        else:
            numm.append("#")
    num.append(numm)
#四隅の判定
if num[0][0] == 0:
    if num[0][1] == "#":
        num[0][0] += 1
    if num[1][0] == "#":
        num[0][0] += 1
    if num[1][1] == "#":
        num[0][0] += 1

if num[0][-1] == 0:
    if num[0][-2] == "#":
        num[0][-1] += 1
    if num[1][-1] == "#":
        num[0][-1] += 1
    if num[1][-2] == "#":
        num[0][-1] += 1

if num[-1][0] == 0:
    if num[-1][1] == "#":
        num[-1][0] += 1
    if num[-2][0] == "#":
        num[-1][0] += 1
    if num[-2][1] == "#":
        num[-1][0] += 1

if num[-1][-1] == 0:
    if num[-1][-2] == "#":
        num[-1][-1] += 1
    if num[-2][-1] == "#":
        num[-1][-1] += 1
    if num[-2][-2] == "#":
        num[-1][-1] += 1

for i in range(1,a-1):
    for j in range(1,b-1):
        if num[i][j] == 0:
            num[i][j] += num[i-1][j-1:j+2].count("#")
            num[i][j] += str(num[i][j-1]).count("#")
            num[i][j] += str(num[i][j+1]).count("#")
            num[i][j] += num[i+1][j-1:j+2].count("#")

for i in range(1,b-1):
    if num[0][i] == 0:
        num[0][i] += str(num[0][i-1]).count("#")
        num[0][i] += str(num[0][i+1]).count("#")
        num[0][i] += num[1][i-1:i+2].count("#")
    if num[-1][i] == 0:
        num[-1][i] += num[-2][i-1:i+2].count("#")
        num[-1][i] += str(num[-1][i-1]).count("#")
        num[-1][i] += str(num[-1][i+1]).count("#")

for i in range(1,a-1):
    if num[i][0] == 0:
        num[i][0] += str(num[i-1][0:2]).count("#")
        num[i][0] += str(num[i][1]).count("#")
        num[i][0] += num[i+1][0:2].count("#")
    if num[i][-1] == 0:
        num[i][-1] += num[i-1][-2:].count("#")
        num[i][-1] += str(num[i][-2]).count("#")
        num[i][-1] += str(num[i+1][-2:]).count("#")

for i in num:
    print("".join(list(map(str,i))))

Submission status: Runtime Error

Solution code:
## グリッドに関する処理
# ABC075B_Minesweeper

## 考え方：pandasに入れて、df.ilocによる行×列のスライスでうまくやる

## for test
# # 1
# height, width = 3, 5
# _s_list = ['.....', '.#.#.', '.....']

# # 2
# height, width = 3, 5
# _s_list = ['#####', '#####', '#####']

# 3
# height, width = 6, 6
# _s_list = ['#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']


## for submit
import pandas as pd
height, width = map(int, input().split())
_s_list = [input() for i in range(height)]
s_list = [list(ss) for ss in _s_list]  # 列×行に1文字ずつ区切ってリストへ追加

df = pd.DataFrame(s_list)
# print(df.iloc[0:3, 0:3])  # debug 行×列のスライス # マイナスはNG, 要素数を超えるのは問題なし
# print(df.iloc[0:3, 1:4])  # debug 行×列のスライス 3*5 から、2行目3列目の周囲を取得する

ans_list = []
for _h in range(height):
    for _w in range(width):
        # 周囲1マスずつを取得
        if _h <= 1:
            h_s = 0
        else:
            h_s = _h-1
        # h_e = _h + 2  # 一応、範囲外アクセスしないように修正
        if _h == height-1:
            h_e = _h+1
        else:
            h_e = _h+2
        
        if _w <= 1:
            w_s = 0
        else:
            w_s = _w - 1
        # w_e = _w + 2  # 一応、範囲外アクセスしないように修正
        if _w == width-1:
            w_e = _w+1
        else:
            w_e = _w+2
        
        # print(df.iloc[h_s:h_e, w_s:w_e]) # debug
        # 周囲1マスずつから、#の数を数えるための前処理
        df_bool = (df.iloc[h_s:h_e, w_s:w_e] == '#')
        # PythonでTrueは1、Falseは0とみなされるため、bool値のオブジェクトに対してsum()メソッドを呼べば条件を満たす要素の数が得られる。
        # print(str(sum(df_bool.sum())))
        
        # チェック対象の中心が#なら#, それ以外はsumした結果の数値を文字列として保存
        if df.iloc[_h, _w] == '#':
            ans_list.append('#')
        else:
            ans_list.append(str(sum(df_bool.sum())))
        
# 結果を出力 width行を1区切りとして1行ずつ出力
ans_str = ''
for i in range(len(ans_list)):
    ans_str += ans_list[i]
    if (i+1) % width == 0:
        print(ans_str)
        ans_str = ''

Submission status: Runtime Error

Solution code:
import pandas as pd
height, width = map(int, input().split())
_s_list = [input() for i in range(height)]
s_list = [list(ss) for ss in _s_list]

df = pd.DataFrame(s_list)

ans_list = []
for _h in range(height):
    for _w in range(width):
        if _h <= 1:
            h_s = 0
        else:
            h_s = _h-1
        h_e = _h + 2
        
        if _w <= 1:
            w_s = 0
        else:
            w_s = _w - 1
        w_e = _w + 2
        
        df_bool = (df.iloc[h_s:h_e, w_s:w_e] == '#')
        
        if df.iloc[_h, _w] == '#':
            ans_list.append('#')
        else:
            ans_list.append(str(sum(df_bool.sum())))
        
ans_str = ''
for i in range(len(ans_list)):
    ans_str += ans_list[i]
    if (i+1) % width == 0:
        print(ans_str)
        ans_str = ''

Submission status: Runtime Error

Solution code:
## グリッドに関する処理
# ABC075B_Minesweeper

## 考え方：pandasに入れて、df.ilocによる行×列のスライスでうまくやる

## for test
# # 1
# height, width = 3, 5
# _s_list = ['.....', '.#.#.', '.....']

# # 2
# height, width = 3, 5
# _s_list = ['#####', '#####', '#####']

# # 3
# height, width = 6, 6
# _s_list = ['#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']


## for submit
import pandas as pd
height, width = map(int, input().split())
_s_list = [input() for i in range(height)]
s_list = [list(ss) for ss in _s_list]  # 列×行に1文字ずつ区切ってリストへ追加

df = pd.DataFrame(s_list)
# print(df.iloc[0:3, 0:3])  # debug 行×列のスライス # マイナスはNG, 要素数を超えるのは問題なし
# print(df.iloc[0:3, 1:4])  # debug 行×列のスライス 3*5 から、2行目3列目の周囲を取得する

ans_list = []
for _h in range(height):
    for _w in range(width):
        # 周囲1マスずつを取得
        if _h <= 1:
            h_s = 0
        else:
            h_s = _h-1
        h_e = _h + 2
        
        if _w <= 1:
            w_s = 0
        else:
            w_s = _w - 1
        w_e = _w + 2
        
        # print(df.iloc[h_s:h_e, w_s:w_e]) # debug
        # 周囲1マスずつから、#の数を数えるための前処理
        df_bool = (df.iloc[h_s:h_e, w_s:w_e] == '#')
        # PythonでTrueは1、Falseは0とみなされるため、bool値のオブジェクトに対してsum()メソッドを呼べば条件を満たす要素の数が得られる。
        # print(str(sum(df_bool.sum())))
        
        # チェック対象の中心が#なら#, それ以外はsumした結果の数値を文字列として保存
        if df.iloc[_h, _w] == '#':
            ans_list.append('#')
        else:
            ans_list.append(str(sum(df_bool.sum())))
        
# 結果を出力 width行を1区切りとして1行ずつ出力
ans_str = ''
for i in range(len(ans_list)):
    ans_str += ans_list[i]
    if (i+1) % width == 0:
        print(ans_str)
        ans_str = ''

Submission status: Runtime Error

Solution code:
## グリッドに関する処理
# ABC075B_Minesweeper

## 考え方：pandasに入れて、df.ilocによる行×列のスライスでうまくやる

## for test
# 1
# height, width = 3, 5
# _s_list = ['.....','.#.#.','.....']

## for submit
import pandas as pd
height, width = map(int, input().split())
_s_list  = [input() for i in range(h)]
s_list = [list(ss) for ss in _s_list] # 列×行に1文字ずつ区切ってリストへ追加

df = pd.DataFrame(s_list)
#print(df.iloc[0:3, 0:3])  # debug 行×列のスライス # マイナスはNG, 要素数を超えるのは問題なし
# print(df.iloc[0:3, 1:4])  # debug 行×列のスライス 3*5 から、2行目3列目の周囲を取得する

ans_list = []
for _h in range(height):
    for _w in range(width):
        # 周囲1マスずつを取得
        if _h <= 1:
            h_s = 0
        else:
            h_s = _h-1
        h_e = _h + 2
        
        if _w <= 1:
            w_s = 0
        else:
            w_s = _w - 1
        w_e = _w + 2
        
        # print(df.iloc[h_s:h_e, w_s:w_e]) # debug
        # 周囲1マスずつから、#の数を数えるための前処理
        df_bool = (df.iloc[h_s:h_e, w_s:w_e] == '#')
        # PythonでTrueは1、Falseは0とみなされるため、bool値のオブジェクトに対してsum()メソッドを呼べば条件を満たす要素の数が得られる。
        # print(str(sum(df_bool.sum())))
        
        # チェック対象の中心が#なら#, それ以外はsumした結果の数値を文字列として保存
        if df.iloc[_h, _w] == '#':
            ans_list.append('#')
        else:
            ans_list.append(str(sum(df_bool.sum())))
        
# 結果を出力 width行を1区切りとして1行ずつ出力
ans_str = ''
for i in range(len(ans_list)):
    ans_str += ans_list[i]
    if (i+1) % width == 0:
        print(ans_str)
        ans_str = ''

Submission status: Runtime Error

Solution code:
## グリッドに関する処理
# ABC075B_Minesweeper

## 考え方：pandasに入れて、df.ilocによる行×列のスライスでうまくやる

## for test
# 1
#height, width = 3, 5
#_s_list = ['.....','.#.#.','.....']

## for submit
import pandas as pd
height, width = map(int, input().split())
_s_list  = [input() for i in range(h)]
s_list = [list(ss) for ss in _s_list] # 列×行に1文字ずつ区切ってリストへ追加

df = pd.DataFrame(s_list)
#print(df.iloc[0:3, 0:3])  # debug 行×列のスライス # マイナスはNG, 要素数を超えるのは問題なし
# print(df.iloc[0:3, 1:4])  # debug 行×列のスライス 3*5 から、2行目3列目の周囲を取得する

ans_list = []
for _h in range(height):
    for _w in range(width):
        # 周囲1マスずつを取得
        if _h <= 1:
            h_s = 0
        else:
            h_s = _h-1
        h_e = _h + 2
        
        if _w <= 1:
            w_s = 0
        else:
            w_s = _w - 1
        w_e = _w + 2
        
        # print(df.iloc[h_s:h_e, w_s:w_e]) # debug
        # 周囲1マスずつから、#の数を数えるための前処理
        df_bool = (df.iloc[h_s:h_e, w_s:w_e] == '#')
        # PythonでTrueは1、Falseは0とみなされるため、bool値のオブジェクトに対してsum()メソッドを呼べば条件を満たす要素の数が得られる。
        # print(str(sum(df_bool.sum())))
        
        # チェック対象の中心が#なら#, それ以外はsumした結果の数値を文字列として保存
        if df.iloc[_h, _w] == '#':
            ans_list.append('#')
        else:
            ans_list.append(str(sum(df_bool.sum())))
        
# 結果を出力 width行を1区切りとして1行ずつ出力
ans_str = ''
for i in range(len(ans_list)):
    ans_str += ans_list[i]
    if (i+1) % width == 0:
        print(ans_str)
        ans_str = ''

Submission status: Runtime Error

Solution code:
## グリッドに関する処理
# ABC075B_Minesweeper

## 考え方：pandasに入れて、df.ilocによる行×列のスライスでうまくやる

## for test
# 1
# height, width = 3, 5
# _s_list = ['.....','.#.#.','.....']

## for submit
import pandas as pd
h, w = map(int, input().split())
_s_list  = [input() for i in range(h)]
s_list = [list(ss) for ss in _s_list] # 列×行に1文字ずつ区切ってリストへ追加

df = pd.DataFrame(s_list)
#print(df.iloc[0:3, 0:3])  # debug 行×列のスライス # マイナスはNG, 要素数を超えるのは問題なし
# print(df.iloc[0:3, 1:4])  # debug 行×列のスライス 3*5 から、2行目3列目の周囲を取得する

ans_list = []
for _h in range(height):
    for _w in range(width):
        # 周囲1マスずつを取得
        if _h <= 1:
            h_s = 0
        else:
            h_s = _h-1
        h_e = _h + 2
        
        if _w <= 1:
            w_s = 0
        else:
            w_s = _w - 1
        w_e = _w + 2
        
        # print(df.iloc[h_s:h_e, w_s:w_e]) # debug
        # 周囲1マスずつから、#の数を数えるための前処理
        df_bool = (df.iloc[h_s:h_e, w_s:w_e] == '#')
        # PythonでTrueは1、Falseは0とみなされるため、bool値のオブジェクトに対してsum()メソッドを呼べば条件を満たす要素の数が得られる。
        # print(str(sum(df_bool.sum())))
        
        # チェック対象の中心が#なら#, それ以外はsumした結果の数値を文字列として保存
        if df.iloc[_h, _w] == '#':
            ans_list.append('#')
        else:
            ans_list.append(str(sum(df_bool.sum())))
        
# 結果を出力 width行を1区切りとして1行ずつ出力
ans_str = ''
for i in range(len(ans_list)):
    ans_str += ans_list[i]
    if (i+1) % width == 0:
        print(ans_str)
        ans_str = ''

Submission status: Runtime Error

Solution code:
S = []
for _ in range(h):
  S.append(input())

for i in range(h):
  ss = ''
  for j in range(w):
    if S[i][j] == '#':
      ss += '#'
    else:
      counter = 0
      for k in [-1, 0, 1]:
        for l in [-1, 0, 1]:
          if 0 <= i+k <= h-1 and 0 <= j+l <= w-1 and S[i+k][j+l] == '#':
            counter += 1
      ss += str(counter)
  print(ss)

Submission status: Runtime Error

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

M = [["."*W]]
M = [[".", *input(), "."] for i in range(H)]
M.insert(0,[*("."*(W+2))])
M.append([*("."*(W+2))])

cnt = 0
Ans = []
for i in range(1, H+1):
    for j in range(1, W+1):
        if M[i][j] == ".":
            for k in range(-1, 2):
                if M[i+k][j-1] == "#":
                    cnt += 1
                if M[i+k][j] == "#":
                    cnt += 1
                if M[i+k][j+1] == "#":
                    cnt += 1
            print(cnt, end = "")
            cnt = 0
        else:
            print("#", end = "")
    print()

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
fields = [['.'] * (W+2)] + [['.'] + list(input()) + ['.'] for _ in range(H)] + [['.'] * (W+2)]

for i in range(1, H+1):
  for j in range(1, W+1):
    if fields[i][j] == '#':
      continue
    else:
      mine_cnt = 0
      if fields[i][j-1] == '#':
        mine_cnt += 1
      if fields[i][j+1] == '#':
        mine_cnt += 1
      if fields[i-1][j] == '#':
        mine_cnt += 1
      if fields[i+1][j] == '#':
        mine_cnt += 1
      fields[i][j] = mine_cnt
for i in range(1 , H+1):
  row = ''.join(fields[i][1:W+1])
  print(row)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
fields = [['.'] * (W+2)] + [['.'] + list(input()) + ['.'] for _ in range(W)] + [['.'] * (W+2)]

for i in range(1, H+1):
  for j in range(1, W+1):
    if fields[i][j] == '#':
      continue
    else:
      mine_cnt = 0
      if fields[i][j-1] == '#':
        mine_cnt += 1
      if fields[i][j+1] == '#':
        mine_cnt += 1
      if fields[i-1][j] == '#':
        mine_cnt += 1
      if fields[i+1][j] == '#':
        mine_cnt += 1
      fields[i][j] = mine_cnt
for i in range(1 , H):
  row = ''.join(fields[i][1:W+1])
  print(row)

Submission status: Runtime Error

Solution code:
# いつもの探索でいいです 終了

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

for i in range(h):
  for j in range(w):
    if table[i][j] != "#":
      sub_h = [0] if h==0 else [-1, 0, 1] if 0<i<h-1 else [0, 1] if i==0 else [-1, 0]
      sub_w = [0] if w==0 else [-1, 0, 1] if 0<j<w-1 else [0, 1] if j==0 else [-1, 0]
      count = 0
      for sh in sub_h:
        for sw in sub_w:
          if sh == sw == 0:
            continue
          if table[i+sh][j+sw] == "#":
            count += 1
      table[i][j] = str(count)

for i in range(h):
  print("".join(table[i]))

Submission status: Runtime Error

Solution code:
# いつもの探索でいいです 終了

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

for i in range(h):
  for j in range(w):
    if table[i][j] != "#":
      sub_h = [-1, 0, 1] if 0<i<h-1 else [0, 1] if i==0 else [-1, 0]
      sub_w = [-1, 0, 1] if 0<j<w-1 else [0, 1] if j==0 else [-1, 0]
      count = 0
      for sh in sub_h:
        for sw in sub_w:
          if sh == sw == 0:
            continue
          if table[i+sh][j+sw] == "#":
            count += 1
      table[i][j] = str(count)

for i in range(h):
  print("".join(table[i]))

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
for i in range(H):
    for j in range(W):
        if s[i][j]=='#':
            continue
            #もし見ている文字が#だったら通過
        cnt=0
        for k in [-1,0,1]:
            if i+k<0 or H<=i+k:
                continue
                #iが端の時通過
            for l in [-1,0,1]:
                if j+l<0 or j+l>=W:
                    continue
                    #jが端の時通過
                cnt+=1*(S[i+k][j+l]=='#')
                #一個前一個上~一個下一個右までの#の数を足す
        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(input()) for i in range(H)]
for i in range(H):
    for j in range(W):
        if s[i][j]=='#':
            continue
            #もし見ている文字が#だったら通過
        cnt=0
        for k in [-1,0,1]:
            if i+k<0 or H<=i+k:
                continue
                #iが端の時通過
            for l in [-1,0,1]:
                if j+l<0 or j+l>=w:
                    continue
                    #jが端の時通過
                cnt+=1*(S[i+k][j+l]=='#')
                #一個前一個上~一個下一個右までの#の数を足す
        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(input()) for i in range(H)]
for i in range(H):
    for j in range(W):
        if s[i][j]=='#':
            continue
            #もし見ている文字が#だったら通過
        cnt=0
        for k in [-1,0,1]:
            if i+k<0 or H<=i+k:
                continue
                #iが端の時通過
            for l in [-1,0,1]:
                if j+l<0 or j+l>=w:
                    continue
                    #jが端の時通過
                cnt+=1*(s[i+k][j+l]=='#')
                #一個前一個上~一個下一個右までの#の数を足す
        s[i][j]=str(cnt)
        #見ていた数を足した数字に変更
for i in range(h):
    print("".join(s[i]))

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

h, w = MAP()

# a = [[0] * (w + 2)] * (h + 2)
s = [list(input()) for _ in range(h)]

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 s[i][j] == "#":
            continue
            # スルー
        num = 0
        for d in range(8):
            ni = i + dy[d]
            nj = j + dx[d]
            # niとnjで有効なのはsの定義域．
            # ni w 0 <= ni <= w - 1
            # nj h 0 <= nj <= h - 1
            
            if ni < 0 or w <= ni:
                continue
                # スルー
            if nj < 0 or h <= nj:
                continue
                # スールー
            if s[ni][nj] == "#":
                num += 1
            s[i][j] = str(num)
            # (num + "0")

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def mine(a):
    count = 0
    for i in range(len(a)):
        if a[i] == "#":
            count += 1
    return count

if h == 1 and w == 1:
    if s[0][0] == "#":
        print("#")
        sys.exit()
    else:
        print("0")
        sys.exit()

elif h==1:
    for j in range(w):
        if s[0][j] == "#":
            s_out[0] += "#"
        elif j == 0:
            s_out[0] += str(mine(s[0][1]))
        elif j == w-1:
            s_out[0] += str(mine(s[i][j-1]))
        else:
            s_out[0] += str(mine(s[i][j-1])+mine(s[i][j+1]))
    print(s_out[0])
    sys.exit()
    
elif w == 1:
    for i in range(h):
        if s[i][0] == "#":
            s_out[i] += "#"
        elif i == 0:
            s_out[i] += str(mine(s[1][0]))
        elif i == h-1:
            s_out[i] += str(mine(s[i-1][0]))
        else:
            s_out[i] += str(mine(s[i+1][0])+mine(s[i-1][0]))
    for i in s_out:
        print(i)
    sys.exit()

for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                s_out[i] += "#"
            elif i == 0 and j == 0:
                s_out[i] += str(mine(s[0][1])+mine(s[1][0:2]))
            elif i == 0 and j == w-1:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            elif i == 0:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
            elif i == h-1 and j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1]))
            elif i == h-1 and j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1]))
            elif i == h-1:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1]))
            elif j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1])+mine(s[i+1][j:j+2]))
            elif j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            else:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
for i in s_out:
    print(i)

Submission status: Runtime Error

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

def mine(a):
    count = 0
    for i in range(len(a)):
        if a[i] == "#":
            count += 1
    return count

for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                s_out[i] += "#"
            elif i == 0 and j == 0:
                s_out[i] += str(mine(s[0][1])+mine(s[1][0:2]))
            elif i == 0 and j == w-1:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            elif i == 0:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
            elif i == h-1 and j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1]))
            elif i == h-1 and j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1]))
            elif i == h-1:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1]))
            elif j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1])+mine(s[i+1][j:j+2]))
            elif j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            else:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
for i in s_out:
    print(i)

Submission status: Runtime Error

Solution code:
import numpy as np

def main():
    H, W = list(map(int, input().split()))
    s = [input() for i in ragne(H)]
    ans = []
    for row in s:
        ans.append(list(row))
    
    diffs = [(-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 s[y][x] == '#': continue
            count = 0
            for dx, dy in diffs:
                if (x + dx < 0) or (x + dx > W - 1) or (y + dy < 0) or (y + dy > H - 1):
                    continue
                if s[y + dy][x + dx] == '#':
                    count += 1
            ans[y][x] = str(count)
    
    for i in range(H):
        print(''.join(ans[i]))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

for i in range(h):
    for j in range(w):
        c=0
        for a in range(i-1,i+2):
            for b in range(j-1,j+2):
                c+=all((min(a,b)>=0,a<h,b<w))and s[a][b]=='#'
        if s[i][j]=='.':s[i][j]=c

for a in s:
    print(*a,sep='')

Submission status: Runtime Error

Solution code:
a,b=[int(input()) for i in range(2)]
c=[str(input()) for i in range(a)]
for i in range(a):
  c[i]="."+c[i]+"."
c=["."*(b+2)]+c +["."*(b+2)]
d=[["0"for i in range(b)]for j in range(a)]
for i in range(1,a+1):
  for j in range(1,b+1):
    f=0
    for k in range(-1,2):
      for l in range(-1,2):
        if c[i+k][j+l]=="#":
          f+=1
    d[i-1][j-1]=str(f)
for i in range(1,a+1):
  for j in range(1,b+1):
    if c[i][j]=="#":
      d[i-1][j-1]="#"
for i in range(a):
  dd=""
  for j in range(b):
    dd+=str(d[i][j])
  print(dd)

Submission status: Runtime Error

Solution code:
a,b=[int(input()) for i in range(2)]
c=[str(input()) for i in range(a)]
for i in range(a):
  c[i]="."+c[i]+"."
c=["."*(b+2)]+c +["."*(b+2)]
d=[[0 for i in range(b)] for j in range(a)]

for i in range(1,a+2):
  for j in range(1,b+2):
    for k in range(-1.2):
      for l in range(-1,2):
        if c[i+k[j+l]]=="#"
          d[i-1[j-1]]+=1
          
for i in range(1,a+2):
  for j in range(1,b+2):
    if c[i[j]]=="#":
      d[i-1[j-1]]="#"
      
print(d)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
grid = [[0 for w in range(W)] for h in range(H)]
sharps = []
for h in range(H):
  row = input()
  #print(row)
  for w,r in enumerate(row):
    #print(w,r)
    if r == "#":
      #grid[h][w] = "#"
      sharps.append([h,w])
      if h>=1:
        grid[h-1][w] += 1
        if w>=1:
          grid[h-1][w-1] += 1
        if w<=W-1:
          grid[h-1][w+1] += 1
      if h<=H-1:
        grid[h+1][w] += 1
        if w>=1:
          grid[h+1][w-1] += 1
        if w<=W-1:
          grid[h+1][w+1] += 1
      if w>=1:
        grid[h][w-1] +=1
      if w<=W-1:
        grid[h][w+1] +=1
for sharp in sharps:
  grid[sharp[0]][sharp[1]] = "#"
for g in grid:
  print("".join([str(v) for v in g]))

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
grid = [[0 for w in range(W)] for h in range(H)]
sharps = []
for h in range(H):
  row = input()
  print(row)
  for w,r in enumerate(row):
    #print(w,r)
    if r == "#":
      #grid[h][w] = "#"
      sharps.append([h,w])
      if h>=1:
        grid[h-1][w] += 1
        if w>=1:
          grid[h-1][w-1] += 1
        if w<=W-1:
          grid[h-1][w+1] += 1
      if h<=H-1:
        grid[h+1][w] += 1
        if w>=1:
          grid[h+1][w-1] += 1
        if w<=W-1:
          grid[h+1][w+1] += 1
      if w>=1:
        grid[h][w-1] +=1
      if w<=W-1:
        grid[h][w+1] +=1
for sharp in sharps:
  grid[sharp[0]][sharp[1]] = "#"
for g in grid:
  print("".join([str(v) for v in g]))

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
grid = [[0]*W]*H
for h in range(H):
  row = input.split()
  for w,r in enumerate(row):
    if r == "#":
      if h>=1:
        grid[h-1][w] += 1
        if w>=1:
          grid[h-1][w-1] += 1
        if w<=W-1:
          grid[h-1][w+1] += 1
      if h<=H-1:
        grid[h+1][w] += 1
        if w>=1:
          grid[h+1][w-1] += 1
        if w<=W-1:
          grid[h+1][w+1] += 1
      if w>=1:
        grid[h][w-1] +=1
      if w<=W-1:
        grid[h][w+1] +=1
  grid.append([0 if v == "." else "#" for v in input().split()])
for g in grid:
  print("".join([str(v) for v in g])