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: Accepted

Solution code:
HW = list(map(int,input().split()))
H=HW[0]
W=HW[1]
S = [list(input()) for i in range(H)]
cnt=0
for i in range(H):
    for j in range(W):
            if S[i][j]=='#':
                continue
            try:
                if S[i-1][j-1]=='#' and i-1>=0 and j-1>=0:
                    cnt=cnt+1            
            except:
                None
                          
            try:
                if S[i-1][j]=='#' and i-1>=0:
                    cnt=cnt+1
            except:
                None
            try:
                if S[i-1][j+1]=='#' and i-1>=0:
                    cnt=cnt+1
            except:
                None
            try:
                if S[i][j-1]=='#' and j-1>=0:
                    cnt=cnt+1
            except:
                None
                
            try:
                if S[i][j+1]=='#':
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j-1]=='#' and j-1>=0:
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j]=='#':
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j+1]=='#':
                    cnt=cnt+1
            except:
                None
            S[i][j]=cnt
            cnt=0

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

Submission status: Accepted

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

s = []
for si in range(h):
  s.append(list(input()))

def up_left(s, x, y):
  if x - 1 < 0 or y - 1 < 0:
    return 0
  return 1 if s[y - 1][x - 1] == '#' else 0

def up(s, x, y):
  if y - 1 < 0:
    return 0
  return 1 if s[y - 1][x] == '#' else 0

def up_right(s, x, y, w):
  if y - 1 < 0 or x + 1 >= w:
    return 0
  return 1 if s[y - 1][x + 1] == '#' else 0

def left(s, x, y):
  if x - 1 < 0:
    return 0
  if s[y][x-1] == '#':
    return 1
  return 0

def right(s, x, y, w):
  if x + 1 >= w:
    return 0
  return 1 if s[y][x + 1] == '#' else 0

def down_left(s, x, y, h):
  if y + 1 >= h or x - 1 < 0:
    return 0
  return 1 if s[y + 1][x - 1] == '#' else 0

def down(s, x, y, h):
  if y + 1 >= h:
    return 0
  return 1 if s[y + 1][x] == '#' else 0

def down_right(s, x, y, h, w):
  if y + 1 >= h or x + 1 >= w:
    return 0
  return 1 if s[y + 1][x + 1] == '#' else 0
  
rr = []
for y_i, sy in enumerate(s):
  r = []
  for x_i, x in enumerate(sy):
    bomb_count = 0
    if x == '#':
      r.append(x)
    else:
      # 1
      bomb_count += up_left(s, x_i, y_i)
      bomb_count += up(s, x_i, y_i)
      bomb_count += up_right(s, x_i, y_i, w)
      bomb_count += left(s, x_i, y_i)
      bomb_count += right(s, x_i, y_i, w)
      bomb_count += down_left(s, x_i, y_i, h)
      bomb_count += down(s, x_i, y_i, h)
      bomb_count += down_right(s, x_i, y_i, h, w)
      r.append(str(bomb_count))
  rr.append(r)
    
for ri in rr:
  print(''.join(ri))

Submission status: Accepted

Solution code:
from itertools import product

H, W = list(map(int, input().split()))
S = [[0]*(W+2)] + [[0] + list(input()) + [0] for _ in range(H)] + [[0]*(W+2)]
D = list(product([-1,0,1], repeat=2))

for h, w in product(range(1,H+1), range(1,W+1)):
    if S[h][w]!='.' : continue
    n = 0
    for dx, dy in D:
        if S[h+dy][w+dx]=='#': n+=1
    S[h][w] = str(n)

for s in S[1:-1]:
    print(''.join(s[1:-1]))

Submission status: Accepted

Solution code:
#テストファイルで実行する時
import sys
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')

def mine(s,i,j):
	
	ans = 0
	t = s[i-1][j-1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i-1][j]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i-1][j+1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i][j-1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i][j+1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i+1][j-1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i+1][j]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i+1][j+1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1	

	return ans


h,w = map(int,input().split())
s = [str('*'+input())+'*' for i in range(h)]

test = ''
for i in range(w+2):
	test = test + '*' 

#リストsの頭にtestを追加
s.insert( 0,test)

#リストsの最後にtestを追加
s.insert(len(s),test)

#h行w列の2次元リストの初期化
s2 = [[0] * w for i in range(h)]
						
for i in range(1,h+1):
	for j in range(1,w+1):
		if s[i][j]=='*':
			pass
		elif s[i][j]=='#' :
			s2[i-1][j-1]='#'
		else:
			s2[i-1][j-1] = str(mine(s,i,j))

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [ input() for _ in range(h) ]
l = [[0]*(w+2) for _ in range(h+2)]
for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      for y in range(i, i+3):
        for x in range(j, j+3):
          if l[y][x] != "#":
            l[y][x] += 1
      l[i+1][j+1] = "#"
for i in range(1, h+1):
  print(*l[i][1:w+1], sep="")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
slis=[]
a=["."]*(w+2)
slis.append(a)
for i in range(h):
	s=list("."+input()+".")
	slis.append(s)
slis.append(a)
for i in range(1,h+1):
	for j in range(1,w+1):
		a=slis[i-1][j-1:j+2]+slis[i][j-1:j+2]+slis[i+1][j-1:j+2]
		if slis[i][j] != "#":
			slis[i][j]=a.count("#")
for i in range(1,h+1):
	print(*slis[i][1:w+1],sep="")

Submission status: Accepted

Solution code:
import numpy as np

H,W = map(int,input().split())
S = []
for i in range(H):
    s = list(map(lambda x:x=='#', input()))
    S.append(s)

S = np.array(S)

for i in range(H):
    for j in range(W):
        if S[i,j]:
            print('#',end='')
        else:
            print(np.sum(S[max(0,i-1):i+2, max(0,j-1):j+2]),end='')
    print()

Submission status: Accepted

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


for i in range(H):
    for j in range(W):
        if S[i][j]:
            print('#',end='')
        else:
            t = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if 0<=i+a<H and 0<=j+b<W and S[i+a][j+b]:
                        t+=1
            print(t,end='')
    print()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
board = [list(input()) for _ in range(h)]
x = (-1,0,1,0,-1,1,-1,1)
y = (0,-1,0,1,-1,-1,1,1)
for i in range(h):
  for j in range(w):
    if board[i][j] == "#":
      continue
    else:
      cnt = 0
      for k in range(8):
        if 0 > i + y[k] or i + y[k] >= h or 0 > j + x[k] or j + x[k] >= w:
          continue
        else:
          if board[i + y[k]][j + x[k]] == "#":
            cnt += 1
      board[i][j] = str(cnt)
for i in range(h):
  print("".join(board[i]))

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H,W = MI()
    grid = [":"*(W+2)] + [":"+ST()+":" for _ in range(H)] + [":"*(W+2)]
    # print(grid)
    for i in range(1,H+1):
        for j in range(1,W+1):
            # print(i,j,grid[i][j])
            if grid[i][j] == ".":
                cnt=0
                for dx in range(-1,2):
                    for dy in range(-1,2):
                        cnt += grid[i+dy][j+dx]=="#"
                print(cnt,end="")
            elif grid[i][j] == "#":
                print("#",end="")

        print()
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np


def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    A = np.zeros([H + 2, W + 2], bool)

    for h in range(H):
        for w in range(W):
            if S[h][w] == "#":
                A[h + 1, w + 1] = True

    B = np.zeros([H, W], np.int32)
    for h in range(H):
        for w in range(W):
            B[h, w] = np.count_nonzero(A[h:h + 3, w:w + 3])

    B = B.tolist()
    for h in range(H):
        for w in range(W):
            if S[h][w] == "#":
                B[h][w] = "#"

    for h in range(H):
        b = list(map(str, B[h]))
        print("".join(b))


main()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
Array = [list(map(str,input())) for _ in range(h)]
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 Array[i][j]=='#':
      continue
    num=0
    for d in range(8):
      ni = i+dy[d]
      nj = j+dx[d]
      if ni<0 or h<=ni:
        continue
      if nj<0 or w<=nj:
        continue
      if Array[ni][nj]=='#':
        num=num+1
    Array[i][j]=str(num)
    
for i in Array:
  print(*i, sep='')

Submission status: Accepted

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

move = [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]
for i in range(h):
    ans = []
    for j in range(w):
        cnt = "#"
        if S[i][j] == "#":
            ans.append("#")
            continue
        cnt = 0
        for mx,my in move:
            x,y = i+mx,j+my
            if 0 > x or x >= h or 0 > y or y >= w:continue
            if S[x][y] == "#":
                cnt += 1
        ans.append(str(cnt))
    print("".join(ans))

Submission status: Accepted

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

zero = []
for i in range(W+2):
    zero += '-'
S.append(zero)
for i in range(H):
    tmp = ['-']
    tmp += (list(str(input())))
    tmp += '-'
    S.append(tmp)
S.append(zero)
ans = []
for h in (range(H)):
    tmp = []
    for w in (range(W)):
        if S[h+1][w+1] == '#':
            tmp.append('#')
        else:
            count = 0
            for i in range(3):
                for j in range(3):
                    if S[h+i][w+j] == '#':
                        count += 1
            tmp.append(str(count))
    ans.append(tmp)

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

Submission status: Accepted

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

for i in range(H):
    S[i] = list(input())


def count_bomb_num(S, h, w):
    bomb_num = 0

    for i in range(-1, 2):
        ni = h + i
        if ni < 0 or H <= ni:
            continue
        for j in range(-1, 2):
            nj = w + j
            if nj < 0 or W <= nj:
                continue
            if S[ni][nj] == "#":
                bomb_num += 1

    return bomb_num


for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        S[h][w] = str(count_bomb_num(S, h, w))

for s in S:
    print("".join(s))

Submission status: Accepted

Solution code:
import itertools
H,W = map(int,input().split())
grid = []
for i in range(H):
    a = input()
    l = []
    for i in a:
        if(i == "."):
            i = 0
        l.append(i)
    grid.append(l)
for i in range(H):
    for j in range(W):
        if(grid[i][j] != "#"):
            continue
        for dx,dy in itertools.product([-1,0,1],repeat=2):
            x = i + dx
            y = j + dy
            if(x<0 or H<=x):
                continue
            if(y<0 or W <= y):
                continue
            if grid[x][y] == "#":
                continue
            grid[x][y] += 1
for Row in grid:
    print(*Row,sep="")

Submission status: Accepted

Solution code:
import numpy as np

# 行、列の番号を受けて周りの地雷の数を返す
def mnum(i,j):
    Acand=np.asarray([[r,c] for r in range(i-1,i+2) for c in range(j-1,j+2)])
    condh=np.logical_and(Acand[:,0]>=0,Acand[:,0]<H)
    condw=np.logical_and(Acand[:,1]>=0,Acand[:,1]<W)
    cond=np.logical_and(condh,condw)
    A=Acand[cond,:] # 周りの8マス
    mcount=0 # 地雷の数

    for a in A: # 各周りのマスについて、地雷だったら数をインクリメントする
        mcount+=int(S[a[0],a[1]]=="#")

    return mcount

H,W=list(map(int,input().split(" ")))
S=np.full((H,W),".",np.str)
MC=np.zeros((H,W),np.str) # 地雷の数

# 入力してゆく
for s in range(H):
    S[s,:]=list(input())

# 周り8マスの地雷の数を取得してゆく
for i in range(H):
    for j in range(W):
        if S[i,j]==".": # 空きマスの場合
            MC[i,j]=str(mnum(i,j)) # 地雷の数
        elif S[i,j]=="#": # 地雷マスの場合
            MC[i,j]="#" # 地雷

    print("".join(MC[i,:]))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    h, w = MI()
    t = [["."] * (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 t[i][j] == "#": continue
            cnt=0
            for ii in range(i-1,i+2):
                for jj in range(j-1,j+2):
                    if t[ii][jj]=="#":cnt+=1
            t[i][j]=cnt

    for tk in t[1:1+h]:
        print(*tk[1:1+w],sep="")

main()

Submission status: Accepted

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

S = [[x for x in str(input())] for _ in range(h)]
S.append(['/']*w)
S.insert(0, ['/']*w)

for i in range(h+2):
    S[i].append('/')
    S[i].insert(0, '/')

ans = [[] for _ in range(h)]

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

for k in range(h):
    print(''.join(ans[k]))

Submission status: Accepted

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

s = [input() for _ in range(h)]

padded = []

for i in range(h + 2):
    if i == 0 or i == h + 1:
        padded.append("." * (w + 2))
    else:
        padded.append("." + s[i - 1] + ".")
# print(padded)

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

for i in range(1, h + 1):
    for j in range(1, w + 1):
        c = 0
        if padded[i][j] == "#":
            out[i - 1][j - 1] = "#"
        else:
            for k in range(8):
                dx = [-1, 0, 1, -1, 1, -1, 0, 1]
                dy = [-1, -1, -1, 0, 0, 1, 1, 1]
                c += padded[i + dx[k]][j + dy[k]] == "#"
            out[i - 1][j - 1] = c
        # print(i, j, out)
# print(out)
for i in range(h):
    print(*out[i], sep="")

Submission status: Accepted

Solution code:
import numpy as np

def check_surrounding(array, h, w):
    count = 0
    if array[h, w] == "#":
        count+=1
    if array[h, w+1] == "#":
        count+=1
    if array[h, w+2] == "#":
        count+=1
    if array[h+1, w] == "#":
        count+=1
    if array[h+1, w+2] == "#":
        count+=1
    if array[h+2, w] == "#":
        count+=1
    if array[h+2, w+1] == "#":
        count+=1
    if array[h+2, w+2] == "#":
        count+=1
        
    return count


H, W = map(int, input().split())
grid = np.array([["0"]*(W+2)]*(H+2))
for h in range(H+2):
    if (h == 0) or (h==(H+1)):
        for w in range(W+2):
            grid[h, w] = "-"
        continue
    inputs =  input()
    for w in range(W+2):
        if (w == 0) or (w==(W+1)):
            grid[h, w] = "-"
        else:
            grid[h, w] = inputs[w-1]
            
# print(grid)

for h in range(H):
    for w in range(W):
        if grid[h+1, w+1] == "#":
            print("#", end="")
        else:
            print(check_surrounding(grid, h, w), end="")
    print()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
masu=[[None for i in range(W+2)] for j in range(H+2)]
score=[[None for i in range(W)] for j in range(H)]
for i in range(1,H+1):
    S=list(input())
    for j in range(len(S)):
        masu[i][j+1]=S[j]

for i in range(1,H+1):
    for j in range(1,W+1):
        if masu[i][j]=="#": 
            score[i-1][j-1]="#"
            continue
        count_score=0
        if masu[i-1][j-1]=="#":count_score+=1
        if masu[i-1][j]=="#":count_score+=1
        if masu[i-1][j+1]=="#":count_score+=1
        if masu[i][j-1]=="#":count_score+=1
        if masu[i][j+1]=="#":count_score+=1
        if masu[i+1][j-1]=="#":count_score+=1
        if masu[i+1][j]=="#":count_score+=1
        if masu[i+1][j+1]=="#":count_score+=1

        score[i-1][j-1]=count_score
for i in score:
    print("".join([str(_) for _ in i]))

Submission status: Accepted

Solution code:
from sys import stdin
import math
import bisect
import heapq
import numpy as np
from math import factorial
inf = 10**10

h,w = [int(x) for x in stdin.readline().rstrip().split()]

li = [["." for i in range(w+2)]for j in range(h+2)]
for i in range(1,h+1):
    s = stdin.readline().rstrip()
    for j in range(1,w+1):
        li[i][j] = s[j-1]

lin = [[0 for i in range(w)]for j in range(h)]

for i in range(1,h+1):
    for j in range(1,j+1):
        if li[i][j] == "#":
            lin[i-1][j-1] = "#"
        else:
            point = 0
            if li[i-1][j-1] == "#":
                point += 1
            if li[i-1][j] == "#":
                 point += 1
            if li[i-1][j+1] == "#":
                 point += 1
            if li[i][j-1] == "#":
                 point += 1
            if li[i][j+1] == "#":
                 point += 1
            if li[i+1][j-1] == "#":
                 point += 1
            if li[i+1][j] == "#":
                 point += 1
            if li[i+1][j+1] == "#":
                 point += 1
            lin[i-1][j-1] = str(point)

for i in lin:
    s = "".join(i)
    print(s)

Submission status: Accepted

Solution code:
import copy
h,w = map(int,input().split())
ls = []
for i in range(h):
    s = list(input())
    ls.append(s)
    
ls2 = copy.copy(ls)

for i in range(h):
    for j in range(w):
        ans = 0
        if ls[i][j] == "#":
          ans = "#"
        else:
          if j+1 <= w-1:#right
            if ls[i][j+1] == "#":
                ans += 1
            if i+1 <= h-1:
              if ls[i+1][j+1] == "#":
                ans += 1
            if i-1 >= 0:
              if ls[i-1][j+1] == "#":
                ans += 1
          if i+1 <= h-1:#down
            if ls[i+1][j] == "#":
                ans += 1
          if j-1 >= 0:#left
            if ls[i][j-1] == "#":
                ans += 1
            if i+1 <= h-1:
              if ls[i+1][j-1] == "#":
                ans += 1
            if i-1 >= 0:
              if ls[i-1][j-1] == "#":
                ans += 1
          if i-1 >= 0:#up
            if ls[i-1][j] == "#":
                ans += 1
         
        ls2[i][j] = ans
        
for i in range(h):
  for j in range(w):
  	print(ls2[i][j],end="")
  print("")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = [['' for i in range(w+2)] for j in range(h+2)]
for i in range(h):
    S[i+1][1:w+1] = input()
for i in range(h):
    for j in range(w):
        if S[i+1][j+1]=='.':
            S[i+1][j+1] = str(1*(sum(S[k+i][j:j+3].count('#') for k in range(3))))
    print(*S[i+1][1:w+1],sep='')

Submission status: Accepted

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

for i in range(len(S)):
	row = list()
	for j in range(len(S[i])):
		if S[i][j] == '#': # 爆弾だったら#を出力
			row.append('#')
			continue # この後のfor文内の処理をスキップ
		count = 0
		check_list =[[i-1,j-1],[i-1,j],[i-1,j+1],[i,j-1],[i,j+1],[i+1,j-1],[i+1,j],[i+1,j+1]]
		for c in range(len(check_list)):
			if check_list[c][0] >= 0 and check_list[c][0] < len(S) and check_list[c][1] >= 0 and check_list[c][1] < len(S[i]):
				if S[check_list[c][0]][check_list[c][1]] == '#':
					count += 1
		row.append(str(count))
	print(''.join(row))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h) :
    s += [["-1"] + list(input()) + ["-1"]]
s = [["-1"] * (w + 2)] + s + [["-1"] * (w + 2)]
for i in range(1, h + 1) :
    for j in range(1, w + 1) :
        if s[i][j] == "#" :
            continue
        else :
            count = 0
            for k in range(-1,2) :
                if s[i-1][j+k] == "#" :
                    count += 1
                if s[i][j+k] == "#" :
                    count += 1
                if s[i+1][j+k] == "#" :
                    count += 1
            s[i][j] = str(count)
s = s[1:-1]
for i in range(h) :
    print("".join(s[i][1:-1]))

Submission status: Accepted

Solution code:
import numpy as np
line = input().split()
h = int(line[0])
w = int(line[1])
grid = []
for i in range(h):
  grid.append(list(input()))
              
grid = np.array(grid)


def get8(grid,y,x):
  v = [None]*8
  try :
    if y-1 < 0 :
      pass
    else:
      v[0] = grid[y-1][x]
  except :
    pass
  try :
    if y-1 < 0 :
      pass
    else :
      v[1] = grid[y-1][x+1]
  except :
    pass  
  try :
    v[2] = grid[y][x+1]
  except :
    pass
  try :
    v[3] = grid[y+1][x+1]
  except :
    pass
  try :
    v[4] = grid[y+1][x]
  except :
    pass
  try :
    if x-1 < 0 :
      pass
    else:
      v[5] = grid[y+1][x-1]
  except :
    pass
  try :
    if x-1 < 0 :
      pass
    else:
      v[6] = grid[y][x-1]
  except :
    pass
  try :
    if y-1 < 0 or x-1 < 0 :
      pass
    else :
    	v[7] = grid[y-1][x-1]
        
  except :
    pass
  
  return v

for i in range(h) :
  for j in range(w) :
    v = get8(grid,i,j)
    if grid[i][j] == "." :
    	grid[i][j] = v.count("#")
  
  
for i in range(h) :
  print("".join(grid[i]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [input() for i in range(h)]
dp = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            continue
        for x in range(-1,2):
            for y in range(-1,2):
                if 0<=i+y<h and 0<=j+x<w:
                    dp[i+y][j+x]+=1
for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            print(dp[i][j],end = '')
        else:
            print('#',end='')
    print('')

Submission status: Accepted

Solution code:
def examB():
    H, W = LI()
    St = [S() for _ in range(H)]
    ans = [[] for _ in range(H)]
    for i1,l in enumerate(St):
        for i2,j in enumerate(l):
            cur = int(0)
            if j==".":
                for k in range(3):
                    for p in range(3):
                        if 0<=i1-1+k<H and 0<=i2-1+p<W:
                            if St[i1 - 1 + k][i2 - 1 + p] == "#":
                                cur += 1
            else:
                cur = "#"
            ans[i1].append(str(cur))
    for v in ans:
        print("".join(map(str,v)))



import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examB()

Submission status: Accepted

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

def calc(y,x):
  if y<0 or y>=H or x<0 or x>=W:
    return 0
  if grid[y][x]=="#":
    return 1
  else:
    return 0

for i in range(H):
  line=""
  for j in range(W):
    ans=0
    if grid[i][j]=="#":
      line+=("#")
    else:
      ans+=calc(i-1,j-1)
      ans+=calc(i-1,j)
      ans+=calc(i-1,j+1)
      ans+=calc(i,j-1)
      ans+=calc(i,j+1)
      ans+=calc(i+1,j-1)
      ans+=calc(i+1,j)
      ans+=calc(i+1,j+1)
      line+=str(ans)
  print(line)

Submission status: Accepted

Solution code:
import numpy as np

H, W = map(int, input().split())
input_map = np.array([input() for i in range(H)])

mine_map = np.array([[0 if j == "." else "#" for j in i] for i in input_map])

for y in range(H):
	for x in range(W):
		if mine_map[y][x] == "#":
			pass
		else:	
			y_lower = 0 if y-1<0 else y-1
			y_upper = H if y+2>H else y+2
			x_lower = 0 if x-1<0 else x-1
			x_upper = W if x+2>W else x+2

			mine_map[y][x] = np.sum(mine_map[y_lower:y_upper, x_lower:x_upper] == "#")

		print(mine_map[y][x], end = "")
	print("")