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:
def draw_map(count_map, row_index, col_index) :
	for i in range(row_index-1, row_index+2) :
		if (i < 0 or len(count_map) <= i) :
			continue
		for j in range(col_index-1, col_index+2) :
			if (j < 0 or len(count_map[0]) <= j) :
				continue
			count_map[i][j] += 1
	
	return count_map
    				
    			

H,W = [int(i) for i in input().split()]
mines = []
for i in range(H) :
	mines.append(input())

# 白地図を用意して、1ずつ足す
# 空きマスだったら数字に置き換える
count_map = [[0 for i in range(W)] for j in range(H)]
for i in range(H) :
	for j in range(W) :
		if (mines[i][j] == "#") :
			mine_map = draw_map(count_map,i,j)

for i in range(H) :
	out_line = ""
	for j in range(W) :
		if (mines[i][j] == ".") :
			out_line += str(count_map[i][j])
		else :
			out_line += "#"
	print(out_line)

Submission status: Accepted

Solution code:
import copy as cp 
H, W = map(int, input().split(" "))
L = [list(input()) for i in range(H)]
a = ["#" for i in range(W)]
A = [cp.copy(a) for i in range(H)]
for i in range(H):
  for j in range(W):
    if L[i][j] == "#":
      continue
    cnt = 0
    for ii in range(-1, 2):
      for jj in range(-1, 2):
        if ii == 0 and jj == 0:
          continue
        iii = i + ii
        jjj = j + jj
        if iii < 0 or iii > H -1:
          continue
        if jjj < 0 or jjj > W -1:
          continue
        if L[iii][jjj] == "#":
          cnt+=1
    A[i][j] = str(cnt)
for i in range(len(A)):
  print(''.join(A[i]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split(" "))
L = [list(input()) for i in range(H)]
a = ["#" for i in range(W)]
A = [a for i in range(H)]
ans = []
for i in range(H):
  for j in range(W):
    if L[i][j] == "#":
      ans.append("#")
      continue
    cnt = 0
    for ii in range(-1, 2):
      for jj in range(-1, 2):
        if ii == 0 and jj == 0:
          continue
        iii = i + ii
        jjj = j + jj
        if iii == i and jjj == j:
          continue
        if iii < 0 or iii > H -1:
          continue
        if jjj < 0 or jjj > W -1:
          continue
        if L[iii][jjj] == "#":
          cnt+=1
    ans.append(str(cnt))
    A[i][j] = str(cnt)
  print("".join(ans))
  ans = []

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
A=[]
a=[0 for i in range(W+2)]
A.append(a)
for i in range(H):
    s=input()
    s="0"+s+"0"
    S=list(s)
    A.append(S)
A.append(a)
#print(A)

for x in range(1,H+1):
    for y in range(1,W+1):
        cnt=0
        if A[x][y]==".":
            if A[x-1][y-1]=="#":
                cnt+=1
            if A[x-1][y]=="#":
                cnt+=1
            if A[x-1][y+1]=="#":
                cnt+=1
            if A[x][y-1]=="#":
                cnt+=1
            if A[x][y]=="#":
                cnt+=1
            if A[x][y+1]=="#":
                cnt+=1
            if A[x+1][y-1]=="#":
                cnt+=1
            if A[x+1][y]=="#":
                cnt+=1
            if A[x+1][y+1]=="#":
                cnt+=1
            A[x][y]=str(cnt)
i=0
j=0
ans=""
for i in range(1,H+1):
    for j in range(1,W+1):
        ans+=A[i][j]
    print(ans)
    ans=""

Submission status: Accepted

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

Solution code:
def around(grid,h,w,x,y):
    ans = []
    if y-1 >= 0:
        ans.append(grid[y-1][x])
        if x-1 >= 0:
            ans.append(grid[y-1][x-1])
        if x+1 < w:
            ans.append(grid[y-1][x+1])
    if x-1 >= 0:
        ans.append(grid[y][x-1])
    if x+1 < w:
        ans.append(grid[y][x+1])
    if y+1 < h:
        ans.append(grid[y+1][x])
        if x-1 >= 0:
            ans.append(grid[y+1][x-1])
        if x+1 < w:
            ans.append(grid[y+1][x+1])
    return ans
        
h,w = map(int,input().split())
grid = []
for i in range(h):
    s = list(input())
    grid.append(s)

for y in range(h):
    for x in range(w):
        if grid[y][x] != "#":
            tile = around(grid,h,w,x,y)
            counter = 0
            for i in range(len(tile)):
                if tile[i] == "#":
                    counter += 1
            grid[y][x] = counter

for i in range(h):
    for k in range(w):
        if k != w-1:
            print(grid[i][k],end="")
        else:
            print(grid[i][k])

Submission status: Accepted

Solution code:
li = []
a, b = map(int,input().split())
for i in range(a):
    li.append(list(input()))

for i in range(a):
    for j in range(b):
        if li[i][j] == '.':
            li[i][j] = 0
            if i!=0 and li[i-1][j]=='#':
                li[i][j]+=1
            if i!=a-1 and li[i+1][j]=='#':
                li[i][j]+=1
            if j!=0 and li[i][j-1]=='#':
                li[i][j]+=1
            if j!=b-1 and li[i][j+1]=='#':
                li[i][j]+=1
            if i!=0 and j!=0 and li[i-1][j-1]=='#':
                li[i][j]+=1
            if i!=a-1 and j!=0 and li[i+1][j-1]=='#':
                li[i][j]+=1
            if i!=0 and j!=b-1 and li[i-1][j+1]=='#':
                li[i][j]+=1
            if i!=a-1 and j!=b-1 and li[i+1][j+1]=='#':
                li[i][j]+=1
        print(li[i][j],end="")
    print()

Submission status: Accepted

Solution code:
import sys
H, W = map(int, raw_input().split())
S = [raw_input() for i in range(H)]
C = [[0] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        for di in range(-1, 2):
            if i + di < 0 or H <= i + di:
                continue
            for dj in range(-1, 2):
                if j + dj < 0 or W <= j + dj:
                    continue
                C[i][j] += 1 if S[i + di][j + dj] == "#" else 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            sys.stdout.write("#")
        else:
            sys.stdout.write(str(C[i][j]))
    print ""

Submission status: Accepted

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

Solution code:
h,w = map(int,input().split())
board = [input() for _ in range(h)]
dx = [-1,-1,-1, 0,0, 1,1,1]
dy = [-1, 0, 1,-1,1,-1,0,1]
ans_board = [[0 for _ in range(w)] for _ in range(h)]

for x in range(h):
    for y in range(w):
        cnt = 0
        if board[x][y] == '#':
            ans_board[x][y] = '#'
        else:
            for d in range(len(dx)):
                n_x = x+dx[d]
                n_y = y+dy[d]

                if 0 <= n_x < h and 0 <= n_y < w:
                    if board[n_x][n_y] == '#':
                        cnt += 1
            ans_board[x][y] = str(cnt)

for ans in ans_board:
    print('' .join(ans))

Submission status: Accepted

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

for row_num, padded_row in enumerate(all_info[1: -1]):
    for collumn_num, s in enumerate(padded_row[1: -1]):
        if s == '#':
            continue
        else:
            all_info[row_num + 1][collumn_num + 1] = str((all_info[row_num][collumn_num: collumn_num +3]
                                                                                     + all_info[row_num + 2][collumn_num: collumn_num + 3]
                                                                                     + all_info[row_num + 1][collumn_num: collumn_num + 3]).count('#'))

for row in all_info[1: -1]:
    print(''.join(row[1: -1]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
all_info = [list(input()) for i in range(H)]
for i, line in enumerate(all_info):
    all_info[i] = ['.'] + line + ['.']
all_info = [['.' for i in range(W+2)]] + all_info + [['.' for i in range(W+2)]]

for row_num, padded_row in enumerate(all_info[1: -1]):
    for collumn_num, s in enumerate(padded_row[1: -1]):
        if s == '#':
            continue
        else:
            all_info[row_num + 1][collumn_num + 1] = str((all_info[row_num][collumn_num: collumn_num +3]
                                                                           + all_info[row_num + 2][collumn_num: collumn_num + 3]
                                                                            + all_info[row_num + 1][collumn_num: collumn_num + 3]).count('#'))

for row in all_info[1: -1]:
    print(''.join(row[1: -1]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
all_info = [list(input()) for i in range(H)]
for i, line in enumerate(all_info):
    all_info[i] = ['.'] + line + ['.']
all_info = [['.' for i in range(W+2)]] + all_info + [['.' for i in range(W+2)]]
all_count = [[-1 for i in range(W)] for j in range(H)]

for row_num, padded_row in enumerate(all_info[1: -1]):
    for collumn_num, s in enumerate(padded_row[1: -1]):
        if s == '#':
            all_count[row_num][collumn_num] = '#'
        else:
            all_count[row_num][collumn_num] = str((all_info[row_num][collumn_num: collumn_num +3]
                                                                           + all_info[row_num + 2][collumn_num: collumn_num + 3]
                                                                            + all_info[row_num + 1][collumn_num: collumn_num + 3]).count('#'))

for count in all_count:
    print(''.join(count))

Submission status: Accepted

Solution code:
import itertools

H, W = [int(_) for _ in input().split()]
S = [input() for _ in range(H)]
memo = [[0] * W for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if S[i][j] == '#':
        memo[i][j] = '#'
        for k, l in itertools.product(range(-1, 2), repeat=2):
            if 0 <= i+k and i+k<H and 0<=j+l and j+l<W and memo[i+k][j+l] != '#':
                memo[i+k][j+l] += 1
[print(''.join(map(str, _))) for _ in memo]

Submission status: Accepted

Solution code:
import itertools
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
l=list(itertools.product([-1,0,1],repeat=2))

for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            cnt = 0
            for k in l:
                x = i+k[0]
                y = j+k[1]
                if 0<=x<=h-1 and 0<=y<=w-1:
                    if s[x][y]=='#':
                        cnt+=1
            s[i][j]=str(cnt)
for i in s:
    print(''.join(i))

Submission status: Accepted

Solution code:
size = input().split(' ')
H = int(size[0])
W = int(size[1])
S = []
for k in range(H):
    row = input()
    S.append(row)

T = []
for i in range(H):
    TI = ''
    for j in range(W):
        #print('S[i][j]={} i={} j={}'.format(S[i][j],i,j))
        count = 0
        for s in range(0,3):
            for t in range(0,3):
                #print('i+s-1={} j+t-1={}'.format(i+s-1,j+t-1))
                if (0 <= i + s - 1 < H) and (0 <= j + t - 1 < W) and (S[i+s-1][j+t-1] == '#'):
                    count +=1
        if S[i][j] == '#':
            TI = TI + '#'
        elif S[i][j] != '#':
            TI = TI + str(count)
    T.append(TI)
for u in range(H):
    print(T[u])

Submission status: Accepted

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

grid = [input() for i in range(H)]

grid2 = [[0 for i in range(W)] for j in range(H)]

for i in range(H):
    for j in range(W):
        if grid[i][j] == "#":
            grid2[i][j] = "#"
            continue
        count = 0
        for k in range(3):
            for l in range(3):
                if(i+k-1 < H and  i+k-1 >= 0 and j+l-1 < W and j+l-1 >=0 and grid[i+k-1][j+l-1] == "#"):
                    count+=1
        grid2[i][j] = str(count)

for row in grid2:
    print("".join(row))

Submission status: Accepted

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

s = ['-'*(w+2)]
for i in range(h):
    s += ['-'+input()+'-']
s += ['-'*(w+2)]

for hi in range(1,h+1):
    ans = ''
    for wi in range(1,w+1):
        if s[hi][wi] == '#':
            ans += '#'
        else:
            c = [s[hi-1][wi-1],s[hi][wi-1],s[hi+1][wi-1],s[hi-1][wi],s[hi+1][wi],s[hi-1][wi+1],s[hi][wi+1],s[hi+1][wi+1]]
            ans += str(c.count('#'))
    print(ans)

Submission status: Accepted

Solution code:
import copy
H, W = map(int, input().split())
S = [input() for i in range(H)]
dd = ((-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1))
T = copy.deepcopy(S)
count = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        for dx, dy in dd:
            nx = j + dx; ny = i + dy
            if 0 <= nx < W and 0 <= ny < H:
                if S[ny][nx] == '#':
                    count += 1
        T[i] = list(T[i])
        T[i][j] = str(count)
        T[i] = "".join(T[i])
        count = 0
for k in range(len(T)):
    print(T[k])

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [list(input()) for i in range(H)] # S[i][j] i行j列
def judge(l,i,j):
    count = 0
    for a in [-1,0,1]:
        for b in [-1,0,1]:
            if a == 0 and b == 0:
                continue
            else:
                if (not 0 <= i+a <= H-1) or (not 0 <= j+b <= W-1):
                    continue
                elif S[i+a][j+b] == "#":
                    count += 1
    return count
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = judge(S,i,j)
        print(S[i][j],end = "")
    print("")

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            if i != 0:
                NUM[i-1][j] += 1
                if j != 0:
                    NUM[i-1][j-1] += 1
                if j != W-1:
                    NUM[i-1][j+1] += 1
            if j != 0:
                NUM[i][j-1] += 1
            if j != W-1:
                NUM[i][j+1] += 1
            if i != H-1:
                NUM[i+1][j] += 1
                if j != W-1:
                    NUM[i+1][j+1] += 1
                if j != 0:
                    NUM[i+1][j-1] += 1
for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            NUM[i][j] = '#'
    else:
        print(''.join(map(str, NUM[i])))

Submission status: Accepted

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

src = [input() for i in range(H)]
ans = []

for row in src:
  ans.append(list(row))

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

Submission status: Accepted

Solution code:
from itertools import product

H, W = map(int, input().split())
table = [list(input()) for _ in range(H)]

for i in range(H):
    for j in range(W):
        count = 0
        if table[i][j] == '#':
            continue
        for y, x in product((-1, 0, 1), repeat=2):
            if not (0 <= i + y < H and 0 <= j + x < W):
                continue
            if table[i + y][j + x] == '#':
                count += 1
        table[i][j] = str(count)

for row in table:
    print(''.join(row))

Submission status: Accepted

Solution code:
tate, yoko = map(int, input().split())
main = [input() for i in range(tate)] 

ans = []

for row in main: # rowの中身は文字列
    ans.append(list(row)) # listを使えば文字列を一文字づつ分けてlistにできる
    
dxy = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)]

for y in range(tate):
    for x in range(yoko):
        if main[y][x] == "#": continue # 満たすときだけ次に進む、満たさない場合飛ばされて次のループに行く
        c = 0
        for dx,dy in dxy:
            if x + dx < 0 or x + dx > yoko - 1 or y + dy < 0 or y + dy > tate - 1: continue
            if main[y + dy][x + dx] == "#":
                    c += 1
        ans[y][x] = c
for row in ans:
    print("".join(list(map(str, row))))

Submission status: Accepted

Solution code:
H, W = [int(i) for i in input().split()]
Map = []
for i in range(H):
    Map.append(list(input()))
for i in range(H):
    for j in range(W):
        if Map[i][j] != '.':
            print(Map[i][j], sep="", end="")
            continue
        bomb = 0
        if i > 0 and Map[i-1][j] == '#': #左
            bomb += 1
        if i < H-1 and Map[i+1][j] == '#': #右
            bomb += 1
        if j > 0 and Map[i][j-1] == '#': #上
            bomb += 1
        if j < W-1 and Map[i][j+1] == '#': #下
            bomb += 1
        if i > 0 and j > 0 and Map[i-1][j-1] == '#': #左上
            bomb += 1
        if i < H-1 and j > 0 and Map[i+1][j-1] == '#': #右上
            bomb += 1
        if i > 0 and j < W -1 and Map[i-1][j+1] == '#': #左下
            bomb += 1
        if i < H -1 and j < W -1 and Map[i+1][j+1] == '#': #右下
            bomb += 1
        Map[i][j] = str(bomb)
        print(Map[i][j], sep="", end="")
    print()

Submission status: Accepted

Solution code:
[ height , width ] = list(map(int,input().split()))

input_map = []
output_map = [[0 for j in range(width)] for i in range(height)]

def is_sharp( input_map , x, y , width , height):
	if( x==-1 or y == -1 or x+1 > width or y+1 > height  ) :return 0
	
	if( input_map[y][x] == '#'):
		return 1
	else:
		 return 0

def count_sharp_num( input_map , x , y):
	count = 0
	count += is_sharp(input_map , x-1 , y-1 , width , height)
	count += is_sharp(input_map , x-1 , y , width , height)
	count += is_sharp(input_map , x-1 , y+1 , width , height)
	count += is_sharp(input_map , x , y-1 , width , height)
	count += is_sharp(input_map , x , y+1 , width , height)
	count += is_sharp(input_map , x+1 , y-1 , width , height)
	count += is_sharp(input_map , x+1 , y , width , height)
	count += is_sharp(input_map , x+1 , y+1 , width , height)
	return count

for i in range( height ):
	input_map.append( input() )


for x in range( width ):
	for y in range( height ):
		output_map[y][x] = count_sharp_num(input_map,x,y)
		if input_map[y][x] == '#':
			output_map[y][x] = '#'


for y in range(height):
	print( ''.join(map(str,output_map[y])))

Submission status: Accepted

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

for y in range(h):
    for x in range(w):
        cnt = 0
        if cells[y][x] == '#':
            continue
        for dy, dx in itertools.product((-1, 0, 1), repeat=2):
            if not (0 <= y + dy < h and 0 <= x + dx < w):
                continue
            if cells[y + dy][x + dx] == '#':
                cnt += 1
        cells[y][x] = str(cnt)

for line in cells:
    print(''.join(line))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = []
grid.append(' '*(W + 2))
for i in range(H):
    grid.append(' ' + str(input()) + ' ')
grid.append(' '*(W + 2))

ans = []
for i in range(1,H+1):
    R = ''
    for j in range(1,W+1):
        count = 0
        if grid[i][j] == '#': R += '#'
        else:
            loop = [(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 tmp in loop:
                if grid[tmp[0]][tmp[1]] == '#': count += 1
            R += str(count)
    ans.append(R)

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

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            continue
        else:
            R = 0
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    if x == 0 and y == 0:
                        continue
                    elif 0 <= h + x and h + x < H and 0 <= w + y and w + y < W:
                        if S[h + x][w + y] == '#':
                            R += 1
            S[h][w] = str(R)

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
L = [input() for i in range(H)]
dxy = [(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)]

ANS = [[] for i in range(H)]
for y in range(H) :
    for x in range(W) :
        if L[y][x] == "#" :
            ANS[y].append("#")
        else :
            mine = 0
            for dx,dy in dxy :
                nx = x + dx
                ny = y + dy
                if (0 <= nx <= W-1) and (0 <= ny <= H-1) :
                    if L[ny][nx] == "#" :
                        mine += 1
            ANS[y].append(str(mine))
for i in range(H) :
    ans = "".join(ANS[i])
    print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [['.'] * (w + 2)]
s += [['.'] + list(input()) + ['.'] for _ in range(h)]
s += [['.'] * (w + 2)]
t = s
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == '.':
            n = 0
            for k in range(3):
                n += s[i - 1 + k][j - 1:j + 2].count('#')
            t[i][j] = str(n)
for i in range(1, h + 1):
    tt =''
    for j in range(1, w + 1):
        tt += t[i][j]
    print(tt)

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if k != 0 or l != 0:
                        if 0 <= i+k < H and 0 <= j+l < W and S[i+k][j+l] == "#":
                            S[i][j] += 1

            S[i][j] = str(S[i][j])

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
field = []
xx = [-1, -1, -1, 0, 0, 1, 1, 1]
yy = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(h):
    field.append(input())

for i in range(h):
    line = ''
    for j in range(w):
        if field[i][j] == '#':
            line += '#'
        else:
            temp = 0
            for x,y in zip(xx,yy):
                if i+x < 0 or i+x > h-1:
                    continue
                elif j+y < 0 or j+y > w-1:
                    continue
                elif field[i+x][j+y] == '#':           
                    temp += 1
            line += str(temp)
                
    print(line)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
C = [[0 for i in range(W)] for j 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 S[i][j] == '.':
      for k in range(8):
        if 0 <= j + dx[k] < W and 0 <= i + dy[k] < H:
          if S[i + dy[k]][j + dx[k]] == '#':
            C[i][j] += 1
    else:
      C[i][j] = '#'
 
for i in range(H):
  for j in range(W):
    print(C[i][j], end = '')
  print('\n')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
C = [[0 for i in range(W)] for j 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 S[i][j] == '.':
      for k in range(8):
        if 0 <= j + dx[k] < W and 0 <= i + dy[k] < H:
          if S[i + dy[k]][j + dx[k]] == '#':
            C[i][j] += 1
          else:
            pass
        else:
          pass
    else:
      C[i][j] = '#'
 
for i in range(H):
  for j in range(W):
    print(C[i][j], end = '')
  print('\n')

Submission status: Accepted

Solution code:
t = []
H = 0
W = 0

def is_bomb(h, w):
    if 0 <= h and h < H and 0 <= w and w < W:
        if t[h][w] == '#':
            return 1

    return 0


def count_bomb(h, w):
    global t

    num = 0
    if is_bomb(h - 1, w - 1):
        num += 1

    if is_bomb(h - 1, w):
        num += 1

    if is_bomb(h - 1, w + 1):
        num += 1

    if is_bomb(h, w + 1):
        num += 1

    if is_bomb(h + 1, w + 1):
        num += 1

    if is_bomb(h + 1, w):
        num += 1

    if is_bomb(h + 1, w - 1):
        num += 1

    if is_bomb(h, w - 1):
        num += 1

    t[h][w] = str(num)
        

def do_main():
    global H, W, t

    H, W = list(map(int, input().rstrip().split()))

    t = []

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

    for h in range(H):
        for w in range(W):
           if t[h][w] != '#':
                count_bomb(h, w) 


    for h in range(H):
        print(''.join(t[h]))


do_main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
C = [[0 for i in range(W)] for j 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 S[i][j] == '.':
      for k in range(8):
        if 0 <= j + dx[k] < W and 0 <= i + dy[k] < H:
          if S[i +dy[k]][j + dx[k]] == '#':
            C[i][j] += 1
          else:
            pass
        else:
          pass
    else:
      C[i][j] = '#'

for i in range(H):
  for j in range(W):
    print(C[i][j], end = '')
  print('\n')

Submission status: Accepted

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

for _ in range(h):
    s.append(['.'] + list(input()) + ['.'])

s.append(['.' for _ in range(w + 2)])

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


for i in range(1, h + 1):
    print(''.join(map(str, s[i][1:w + 1])))

Submission status: Accepted

Solution code:
H,W=list(map(int,input().split()))
ans=[[0 for _ in range(W+2)] for _ in range(H+2)]
for i in range(H):
    for j,x in enumerate(input()):
        if x=='#':
            ans[i+1][j+1]=-10
            for k in (-1,0,1):
                for m in (-1,0,1):
                    ans[i+1+k][j+1+m]+=1
for x in ans:
    for i,y in enumerate(x):
        if y <0:
            x[i]='#'
for x in ans[1:-1]:
    for y in x[1:-1]:
        print(y,end='')
    print('')

Submission status: Accepted

Solution code:
from sys import stdin


def main() -> None:
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]

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

    for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                ans[i][j] = 9
                for x in (-1, 0, 1):
                    for y in (-1, 0, 1):
                        if 0 <= i + y < h and 0 <= j + x < w:
                            ans[i + y][j + x] += 1

    for i in range(h):
        for j in range(w):
            if ans[i][j] >= 9:
                print("#", end="")
            else:
                print(ans[i][j], end="")
        print()


def next_str() -> str:
    result = ""
    while True:
        tmp = stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        bomb = 0
        for x in range(i-1, i+2):
            for y in range(j-1, j+2):
                if 0 <= x < H and 0 <= y < W and S[x][y] == '#':
                    bomb += 1
        S[i][j] = str(bomb)

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

Submission status: Accepted

Solution code:
board = []
direction = [[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]]
h, w = map(int, input().split(" "))
for i in range(h):
	board.append([k for k in input()])

for i in range(h):
	for j in range(w):
		if board[i][j] == ".":
			board[i][j] = "0"
			for k in range(8):
				x = j + direction[k][0]
				y = i + direction[k][1]
				if 0 <= x < w and 0 <= y < h:
					if board[y][x] == "#":
						board[i][j] = str(1 + int(board[i][j]))
for i in range(h):
	for j in range(w):
		print(board[i][j], end="")
	print("")

Submission status: Accepted

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