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:
H, W = map(int, input().split())
S = ['.'*(W+2)] + ['.'+input()+'.'for _ in[0]*H] + ['.'*(W+2)]
cells = [[0] * W for _ in[0] * H]
for i in range(H):
    for j in range(W):
        if S[i+1][j+1] == '#':
            cells[i][j] = '#'
        else:
            surrounder = S[i][j:j+3] + S[i+1][j:j+3:2] + S[i+2][j:j+3]
            cells[i][j] = surrounder.count('#')
for x in cells:
    print(''.join(map(str, x)))

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)])

ans = [["." for _ in range(W)] for _ in range(H)]

for h in range(1, H+1):
	for w in range(1, W+1):
		if s[h][w] == "#":
			ans[h-1][w-1] = "#"
		else:
			rinsetsu = [s[h-1][w-1], s[h-1][w], s[h-1][w+1], s[h][w-1], s[h][w+1], s[h+1][w-1], s[h+1][w], s[h+1][w+1]]
			ans[h-1][w-1] = str(rinsetsu.count("#"))

for h in range(H):
	print("".join(ans[h]))

Submission status: Accepted

Solution code:
import numpy
import re
import math

def main():
	"""
	テストコード実行
	:return:
	"""
	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), (0, -1), (1, -1), (1, 0), (1, 1)] #8近傍の位置
	for y in range(h):
		for x in range(w):
			if src[y][x] is "#": 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] is "#": c += 1
			ans[y][x] = c
	for row in ans:
		print(''.join(list(map(str, row))))

if __name__ == '__main__':
	main()

Submission status: Accepted

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

for i in range(H):
  S=list(input())
  P.append(S)


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

for i in range(H):
  for j in range(W):
    if P[i][j]=='.':
      count=0
      for k in range(8):
        x=i+dx[k]
        y=j+dy[k]
        if x<H and x>=0 and y<W and y>=0 and P[x][y]=='#':
          count+=1
        P[i][j]=count
          
for i in range(H):
  answer=""
  for j in range(W):
    if P[i][j]=='#':
      answer+="#"
    else:
      answer+=str(P[i][j])
  print(answer)

Submission status: Accepted

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



# 左右に要素を追加
for i in range(H): # 左右に要素を追加
    S[i] = ['.'] + S[i] + ['.']

# 上下に要素を追加
S = [['.' for i in range(W + 2)]] + S + [['.' for i in range(W + 2)]]

S_dummy = S

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == '.':
            tmp = 0
            if S[i - 1][j - 1] == '#':
                tmp += 1
            if S[i - 1][j] == '#':
                tmp += 1
            if S[i - 1][j + 1] == '#':
                tmp += 1
            if S[i][j - 1] == '#':
                tmp += 1
            if S[i][j + 1] == '#':
                tmp += 1
            if S[i + 1][j - 1] == '#':
                tmp += 1
            if S[i + 1][j] == '#':
                tmp += 1
            if S[i + 1][j + 1] == '#':
                tmp += 1
            S_dummy[i][j] = tmp

for i in range(1, H + 1):
    for j in range(1, W + 1):
        print(S_dummy[i][j], end = "")
    print('')

Submission status: Accepted

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

nS = [['' for i in range(W)] for j in range(H)]

cpC = [1, 0, -1, 0, 1, -1, -1, 1]
cpR = [0, 1, 0, -1, 1, 1, -1, -1]


for i in range(H):
    for j in range(W):
        counter = 0
        if S[i][j] == '#':
            nS[i][j] = '#'
            continue

        for r,c in zip(cpR,cpC):
            x = i + r
            y = j + c
            if x >= 0 and x < H and y >= 0 and y < W and S[x][y] == "#":
                counter += 1
        
        nS[i][j] = counter

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

Submission status: Accepted

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

for i in range(H):
    S = list(input())
    L.append(S)

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

for i in range(H):
    for j in range(W):
        if L[i][j] == ".":
            cnt = 0
            for k in range(8):
                x = i + dx[k]
                y = j + dy[k]
                if x >= 0 and x < H and y >= 0 and y < W and L[x][y] == "#":
                    cnt += 1
            L[i][j] = cnt
for i in L:
    print("".join(map(str,i)))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
f = [[0]*(w+3) for i in range(h+3)]
lines = []
for s in range(h):
    l = input()
    lines.append(l)
    for i in range(w):
        if l[i] == "#":
            f[s][i] += 1
            f[s+3][i] -= 1
            f[s][i+3] -= 1
            f[s+3][i+3] += 1

for j in range(h+1):
    for i in range(w):
        f[j][i+1] += f[j][i]
for i in range(w+1):
    for j in range(h):
        f[j+1][i] += f[j][i]
for j in range(h):
    line = ""
    for i in range(w):
        line += "#" if lines[j][i] == "#" else str(f[j+1][i+1])
    print(line)

Submission status: Accepted

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

result = ""
vektor = [
    [-1, -1],
    [-1, 0], 
    [-1, 1], 
    [0, -1], 
    [0, 1],
    [1, -1],
    [1, 0], 
    [1, 1]
    ]

for i in range(H):
    for j in range(W):
        if P[i][j] == "#":
            result += "#"
        else:
            count = 0
            for k in range(8):
                try:
                    if i+vektor[k][0] >= 0 and j+vektor[k][1] >= 0 and P[i+vektor[k][0]][j+vektor[k][1]] == "#":
                        count += 1
                except IndexError:
                    pass
            result += str(count)

start = 0
for i in range(0, H*W+1, W):
    print(result[start:i])
    start = i

Submission status: Accepted

Solution code:
s=[int(x) for x in input().split()]
f=['.'*(s[1]+2)]
for i in range(s[0]):
  f.append('.'+input()+'.')
f.append('.'*(s[1]+2))
for i in range(1,s[0]+1):
  for j in range(1,s[1]+1):
    if f[i][j]=='#':
      continue
    cnt = 0
    for v in [-1,0,1]:
      for h in [-1,0,1]:
        if f[i+v][j+h] =='#':
          cnt+=1
    f[i]=f[i][:j]+str(cnt)+f[i][j+1:]
print('\n'.join([x[1:-1] for x in f[1:-1]]))

Submission status: Accepted

Solution code:
W, H=map(int,input().split())
arr = [input() for j in range(W)]
ans = ['' for i in range(W)] 

for i in range(0, W):
    for j in range(0, H):
        bomb_num = 0
        if arr[i][j] == '#':
            ans[i] += '#'
        else:
            for k in (-1, 0, 1):
                for l in (-1, 0, 1):
                    if (-1<i+k<W) and (-1<j+l<H):
                        if arr[i+k][j+l] == '#':
                            bomb_num += 1
                        
            ans[i] += str(bomb_num)
            
for i in range(W):
        print(ans[i])

Submission status: Accepted

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

for i in range(W):
    gyou=input()
    for j in range(H):
        arr[i][j]=gyou[j:j+1]


for i in range(W):
    for j in range(H):
        if arr[i][j]=="#":
            ans[i][j]="#"
        else:    
            for k in range(-1,2):
                for l in range(-1,2):
                    if i+k>=0 and l+j>=0 and i+k<W and l+j <H:
                        
                        if arr[i+k][l+j]=="#":
                            
                            ans[i][j]+=1
                            
for i in range(W):
    ans1=map(str,ans[i])
    ans2="".join(ans1)
    
    print(ans2)

Submission status: Accepted

Solution code:
h,w=map(int,input().split());a=[list(input())for _ in[0]*h]
for y in range(h):
    for x in range(w):
        c=0
        if a[y][x]=='.':
            for i in range(8):
                u=x+[-1,-1,-1,0,0,1,1,1][i];v=y+[-1,0,1,-1,1,-1,0,1][i]
                if 0<=u<w and 0<=v<h and a[v][u]=='#':c+=1
            a[y][x]=c
for i in range(h):print(*a[i],sep='')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
field = []
mines = []
for ridx in range(H):
    row = list(input())
    field.append([])
    for cidx in range(W):
        is_mine = row[cidx] == '#'
        field[-1].append('#' if is_mine else 0)
        if is_mine:
            mines.append((ridx, cidx))

for m in mines:
    for i in range(-1, 2):
        for j in range(-1, 2):
            if m[0] + i >= 0 and m[0] + i < H and m[1] + j >= 0 and m[1] + j < W and isinstance(field[m[0] + i][m[1] + j], int):
                field[m[0] + i][m[1] + j] += 1

for r in field:
    print(''.join([str(e) for e in r]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [input() for i in range(h)]
b = [[0 for i in range(w+2)] for j in range(h+2)]
for y in range(h):
    for x in range(w):
        if(a[y][x] == '#'):
            b[y][x] += 1
            b[y+1][x] += 1
            b[y+2][x] += 1
            b[y][x+1] += 1
            b[y+2][x+1] += 1
            b[y][x+2] += 1
            b[y+1][x+2] += 1
            b[y+2][x+2] += 1
d = [""] * h
for y in range(h):
    for x in range(w):
        if(a[y][x] == "."):
            d[y] += str(b[y+1][x+1])
        else:
            d[y] += '#'
for s in d:
    print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
M=[input() for x in range(H)]
A=[[0 for x in range(W)]for y in range(H)]
nx=[-1,0,1]
ny=[-1,0,1]

for i in range(H):
  for j in range(W):
    if M[i][j]=='.':
      cnt=0
      for x in nx:
        for y in ny:
          if (0<= i+x <H) and (0<= j+y <W) and M[i+x][j+y]=="#":
            cnt+=1
      A[i][j]=str(cnt)
    else:
      A[i][j]="#"
      
for i in range(H):
  print("".join(A[i]))

Submission status: Accepted

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

for i in range(1, H+1):
    for j in range(1, W+1):
        if maps[i][j] == '.':
            maps[i][j] = (maps[i-1][j-1:j+2] + maps[i][j-1:j+2] + maps[i+1][j-1:j+2]).count('#')
maps.pop(0)
maps.pop(H)
for i in maps:
    print(''.join(map(str, i[1:-1])))

Submission status: Accepted

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

a =[]
a.append('.'*(w+2))
for i in range(h):
    a.append('.'+input()+'.')
a.append('.'*(w+2))

b = 0
c = ''
d =[]
for i in range(h):
    for j in range(w):
        if a[i+1][j+1] =='#':
            c += '#'
        else:
            if a[i][j] =='#':
                b += 1
            if a[i+1][j] =='#':
                b += 1
            if a[i+2][j] =='#':
                b += 1
            if a[i][j+1] =='#':
                b += 1
            if a[i+2][j+1] =='#':
                b += 1
            if a[i][j+2] =='#':
                b += 1
            if a[i+1][j+2] =='#':
                b += 1
            if a[i+2][j+2] =='#':
                b += 1
            c += str(b)
            b = 0
    d.append(c)
    c = ''
for i in d:
    print(i)

Submission status: Accepted

Solution code:
H, W = map(int, raw_input().split())
field = []
for i in range(0, H):
	lst = []
	s = raw_input()
	for j in range(0, W):
		lst.append(s[j])
	field.append(lst)

for i in range(0, H):
	for j in range(0, W):
		if field[i][j] != '#':
			cnt = 0
			for p in range(-1, 2):
				if 0 <= i+p and i+p <= H-1:
					for q in range(-1, 2):
						if 0 <= j+q and j+q <= W-1:
							if field [i+p][j+q] == '#':
								cnt = cnt + 1
			field[i][j] = str(cnt)

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

Submission status: Accepted

Solution code:
def print_board( X):
    print("\n".join(map(lambda x: "".join(map(str, x)), X)))

H, W = [int(x) for x in input().split()]
board = [ list(input() ) for i in range(H)]
means = [ [-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
for i in range(H):
    for j in range(W):
        if board[i][j] == ".":
            mean = [ board[ i + m[0]][j + m[1]] for m in means if 0 <= i + m[0] < H and 0 <= j + m[1] < W  ]
            board[i][j] = mean.count("#")

print_board(board)

Submission status: Accepted

Solution code:
q=list(map(int,input().split()))
grid=[]
def k1(x,y):
  z=[]
  if y[0]+1<x[0]:
    z.append([y[0]+1,y[1]])
    if y[1]+1<x[1]:#
      z.append([y[0]+1,y[1]+1])
    if y[1]-1>=0:
      z.append([y[0]+1,y[1]-1])
  if y[0]-1>=0:
    z.append([y[0]-1,y[1]])
    if y[1]+1<x[1]:#
      z.append([y[0]-1,y[1]+1])
    if y[1]-1>=0:
      z.append([y[0]-1,y[1]-1])    
  if y[1]+1<x[1]:
    z.append([y[0],y[1]+1])
  if y[1]-1>=0:
    z.append([y[0],y[1]-1])
  return(z)
for i in range(q[0]):
    y=input()

    grid.append([i for i in y])
  
for i in range(q[0]):
    for j in range(q[1]):
        if grid[i][j]=='.':
            
          
            kh=k1(q,[i,j])
            
            count=0
            
            for k in range(len(kh)) :
                
                
                if grid[kh[k][0]][kh[k][1]] == '#' :
                  count+=1
            grid[i][j]=count      
                
for i in range(q[0]):
    pr=''
    for j in range(q[1]):
        pr=pr+str(grid[i][j])
    print(pr)

Submission status: Accepted

Solution code:
q=list(map(int,input().split()))
grid=[]
def ze(x,y):
  z=[]
  if y[0]+1<x[0]:
    z.append([y[0]+1,y[1]])
    if y[1]+1<x[1]:#
      z.append([y[0]+1,y[1]+1])
    if y[1]-1>=0:
      z.append([y[0]+1,y[1]-1])
  if y[0]-1>=0:
    z.append([y[0]-1,y[1]])
    if y[1]+1<x[1]:#
      z.append([y[0]-1,y[1]+1])
    if y[1]-1>=0:
      z.append([y[0]-1,y[1]-1])    
  if y[1]+1<x[1]:
    z.append([y[0],y[1]+1])
  if y[1]-1>=0:
    z.append([y[0],y[1]-1])
  return(z)
for i in range(q[0]):
    y=input()

    grid.append([i for i in y])
  
for i in range(q[0]):
    for j in range(q[1]):
        if grid[i][j]=='.':
            
          
            lamsa=ze(q,[i,j])
            
            count=0
            
            for k in range(len(lamsa)) :
                
                
                if grid[lamsa[k][0]][lamsa[k][1]] == '#' :
                  count+=1
            grid[i][j]=count      
                
for i in range(q[0]):
    pr=''
    for j in range(q[1]):
        pr=pr+str(grid[i][j])
    print(pr)

Submission status: Accepted

Solution code:
h,w=map(int,raw_input().split())
temp=[]
e='eee'
for i in range(w-1):
	e='e'+e
temp.append(e)	
for i in range(h):
	ip=raw_input()
	ip='e'+ip+'e'
	temp.append(ip)
temp.append(e)
n=0
op=[]	
fop=[]
for i in range(h):
	for j in range(w):
		if temp[i+1][j+1]=='.':
			if temp[i+2][j+1]=='#':
				n=n+1
			if temp[i][j+1]=='#':
				n=n+1
			if temp[i+1][j+2]=='#':
				n=n+1
			if temp[i+1][j]=='#':
				n=n+1
			if temp[i+2][j+2]=='#':
				n=n+1	
			if temp[i][j+2]=='#':
				n=n+1
			if temp[i+2][j]=='#':
				n=n+1	
			if temp[i][j]=='#':
				n=n+1
			op.append(str(n))	
			n=0
		elif temp[i+1][j+1]=='#':
			op.append('#')
	fop.append(op)
	op=[]

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

Submission status: Accepted

Solution code:
import numpy as np

H, W = [int(elem) for elem in input().split(' ')]
grid = np.array([list(input()) for _ in range(H)])
assert len(grid) == H
assert len(grid[0]) == W

ans_grid = [['#'] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            continue
        elif i == 0 and j == 0:
            count = sum([1 if string == '#' else 0 for string in grid[0:2,0:2].flatten()])
        elif i == 0:
            count = sum([1 if string == '#' else 0 for string in grid[0:2, j - 1:j + 2].flatten()])
        elif j == 0:
            count = sum([1 if string == '#' else 0 for string in grid[i - 1:i + 2, 0:2].flatten()])
        else:
            count = sum([1 if string == '#' else 0 for string in grid[i - 1:i + 2, j - 1:j + 2].flatten()])
        ans_grid[i][j] = str(count)

for ans_row in ans_grid:
    print("".join(ans_row))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input() for i in range(h)]
ans=[[0 for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            ans[i][j]='#'
            if i>0:
                try:
                    ans[i-1][j]+=1
                except:
                    pass
                try:
                    ans[i-1][j+1]+=1
                except:
                    pass
                if j>0:
                    try:
                        ans[i-1][j-1]+=1
                    except:
                        pass
            try:
                ans[i+1][j]+=1
            except:
                pass
            try:
                ans[i+1][j+1]+=1
            except:
                pass
            if j>0:
                try:
                    ans[i+1][j-1]+=1
                except:
                    pass
            try:
                ans[i][j+1]+=1
            except:
                pass
            if j>0:
                try:
                    ans[i][j-1]+=1
                except:
                    pass
for x in ans:
    y = list(map(str, x))
    print(''.join(y))

Submission status: Accepted

Solution code:
h,w=map(int,list(input().split()))
all=[[None] for i in range (h)]
temp=[[0]*w for i in range (h)]


for i in range(h):
    all[i]=list(input())



for i in range(h):
    for j in range(w):
        if(all[i][j]=="#"):
            temp[i][j]="#"
            if(i+1!=h):
                if(temp[i + 1][j]!="#"):
                    temp[i + 1][j] += 1
                if(j-1!=-1):
                    if (temp[i + 1][j-1] != "#"):
                        temp[i + 1][j - 1] += 1
                if(j+1!=w):
                    if (temp[i + 1][j+1] != "#"):
                        temp[i + 1][j + 1] += 1

            if(i-1!=-1):
                if (temp[i - 1][j] != "#"):
                    temp[i - 1][j] += 1
                if(j-1!=-1):
                    if (temp[i -1][j-1] != "#"):
                         temp[i - 1][j - 1] += 1
                if(j+1!=w):
                   if(temp[i - 1][j+1]!="#"):
                       temp[i - 1][j + 1] += 1

            if (j - 1 != -1):
                if (temp[i][j-1] != "#"):
                   temp[i][j - 1] += 1
            if (j + 1 != w):
                if (temp[i][j+1] != "#"):
                    temp[i][j + 1] += 1

for i in range(h):
    temp[i]=map(str,temp[i])
    print("".join(temp[i]))

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if f[i][j] == '#':
            continue
        bomb = 0
        for k in range(i-1, i+2):
            if k < 0 or k > h-1:
                continue
            for l in range(j-1, j+2):
                if l < 0 or l > w-1:
                    continue
                if f[k][l] == '#':
                    bomb += 1

        f[i][j] = str(bomb)

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
T = [","*W + ",,"]  + ["," + input() + "," for _ in range(H)] + [","*W + ",,"]
ans = ""
d = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
for i in range(1, H+1):
    for j in range(1, W+1):
        if T[i][j] == "#":
            ans += "#"
        else:
            ans += str(sum(T[i+x][j+y] == "#" for x, y in d))
    ans += "\n"
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import numpy as np

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

S = [-1 if cell == '#' else 0 for s in S for cell in list(s)]
mat = np.reshape(np.array(S), (H, W))

for h in range(H):
    for w in range(W):
        if mat[h, w] == -1:
            if 0 <= h - 1 < H and 0 <= w - 1 < W:
                if mat[h - 1, w - 1] != -1:
                    mat[h - 1, w - 1] += 1
            if 0 <= h - 1 < H and 0 <= w < W:
                if mat[h - 1, w] != -1:
                    mat[h - 1, w] += 1
            if 0 <= h - 1 < H and 0 <= w + 1 < W:
                if mat[h - 1, w + 1] != -1:
                    mat[h - 1, w + 1] += 1
            if 0 <= h < H and 0 <= w - 1 < W:
                if mat[h, w - 1] != -1:
                    mat[h, w - 1] += 1
            if 0 <= h < H and 0 <= w + 1 < W:
                if mat[h, w + 1] != -1:
                    mat[h, w + 1] += 1
            if 0 <= h + 1 < H and 0 <= w - 1 < W:
                if mat[h + 1, w - 1] != -1:
                    mat[h + 1, w - 1] += 1
            if 0 <= h + 1 < H and 0 <= w < W:
                if mat[h + 1, w] != -1:
                    mat[h + 1, w] += 1
            if 0 <= h + 1 < H and 0 <= w + 1 < W:
                if mat[h + 1, w + 1] != -1:
                    mat[h + 1, w + 1] += 1

for h in range(H):
    row = list(mat[h, :])
    for i in row:
        if i == -1:
            print('#', end='')
        else:
            print(i, end='')
    print('')

Submission status: Accepted

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

S = []
D = [-1,0,1]

for _ in range(H):
    S.append(input())

Ans = ["" for _ in range(H)]

for h in range(H):
    for w in range(W):
        if S[h][w]==".":
            cnt = 0
            for dh in D:
                for dw in D:
                    if h+dh >= 0  and  h+dh < H  and  w+dw >= 0  and  w+dw < W  and  S[h+dh][w+dw] == "#":
                        cnt += 1
            Ans[h] += str(cnt)
        else:
            Ans[h] += "#"

for h in range(H):
    print(Ans[h])

Submission status: Accepted

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

S = []
Snum = [[0 for j in range(W+2)] for i in range(H+2)]

def kabe_c(W):
    kabe = ""
    for j in range(W+2):
        kabe += " "
    return kabe
    
S.append(kabe_c(W))
for i in range(1,H+1):
    S.append(" "+input()+" ")
S.append(kabe_c(W))
    
for i in range(H+2):
    for j in range(W+2):
        if S[i][j] == "#":
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    Snum[k][l] += 1

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == "#":
            print("#",end="")
        else:
            print(str(Snum[i][j]),end="")
    print("")

Submission status: Accepted

Solution code:
H, W= map(int, input().split())
S = ["K"*(W+2)]
for k in range(H):
    S.append("K"+input()+"K")
S.append("K"*(W+2))
A = [[0 for k in range(W)] for l in range(H)]

for y in range(1,W+1):
    for x in range(1,H+1):
        if S[x][y] == "#":
            A[x-1][y-1] = "#"
        else:
            c = 0
            for dy in range(-1,2):
                for dx in range(-1,2):
                    if S[x+dx][y+dy] == "#":
                        c += 1
            A[x-1][y-1] = c
for x in A:
    for y in x:
        print(str(y), end="")
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input())for _ in range(H)]
grid = [(-1,-1),(-1,1),(1,-1),(1,1),(1,0),(0,1),(-1,0),(0,-1)]
for y in range(H):
    for x in range(W):
        count = 0
        if s[y][x] == '#':
            print('#', end='')
        else:
            for g in grid:
                try:
                    sy = y+g[0]
                    sx = x+g[1]
                except IndexError:
                    continue
                if sy < 0 or sy >= H:
                    continue
                elif sx < 0 or sx >= W:
                    continue
                if s[sy][sx] == '#':
                    count += 1
            print(count,end='')
    print()

Submission status: Accepted

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


for y in range(h):
    for x in range(w):
        ans=0
        if s[y][x]=='.':
            for dy in [-1,0,1]:
                for dx in [-1,0,1]:
                    if 0<=x+dx<w and 0<=y+dy<h and s[y+dy][x+dx]=='#':
                        ans+=1
        else:
            ans='#'
        print(ans,end="")
    print()

Submission status: Accepted

Solution code:
x,y=map(int,input().split())
s=[list(input())for i in range(x)]
dx=[1,1,0,-1,-1,-1,0,1]
dy=[0,1,1,1,0,-1,-1,-1]
for i in range(x):
    for j in range(y):
        if s[i][j]==".":
            cnt=0
            for k in range(8):
                x_p=i+dx[k]#点i,jからの移動先のx座標
                y_p=j+dy[k]#y座標
                if 0<=x_p and x_p<x and 0<=y_p and y_p<y and s[x_p][y_p]=="#":#範囲をはみ出さないように
                    cnt+=1
            s[i][j]=cnt
for i in s:
    print(*i,sep="")

Submission status: Accepted

Solution code:
def find_mine_num(H, W, S, current_H, current_W):
    if S[current_H][current_W] == '#':
        return '#'
    mine_count = 0
    # 左斜め上、上、右斜め上
    find_H_position = current_H - 1
    for i in range(-1, 2):
        find_W_position = current_W + i
        if 0 <= find_H_position < H and 0 <= find_W_position < W:
            if S[find_H_position][find_W_position] == '#':
                mine_count += 1
    # 左、右
    if 0 <= current_W - 1 < W:
        if S[current_H][current_W - 1] == '#':
            mine_count += 1
    if 0 <= current_W + 1 < W:
        if S[current_H][current_W + 1] == '#':
            mine_count += 1
    # 左斜め下、下、右斜め下
    find_H_position = current_H + 1
    for i in range(-1, 2):
        find_W_position = current_W + i
        if 0 <= find_H_position < H and 0 <= find_W_position < W:
            if S[find_H_position][find_W_position] == '#':
                mine_count += 1
    return mine_count


H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))
S_mine = []
for i in range(H):
    mine = []
    for j in range(W):
        mine.append(find_mine_num(H, W, S, i, j))
    S_mine.append(mine)
for s in S_mine:
    print(*s, sep='')

Submission status: Accepted

Solution code:
import sys

s_args = sys.stdin.read().splitlines()

rows = []
rows.append([0] * (len(s_args[1])+2))
for s_arg in s_args[1:]:
    row = []
    for cell in s_arg:
        if cell == '.':
            row.append(0)
        else:
            row.append(-100000)
    rows.append([0] + row + [0])
rows.append([0] * (len(s_args[1])+2))

for (i, row) in enumerate(rows):
    if i == 0 or i == len(rows) - 1:
        continue
    for (j, cell) in enumerate(row):
        if j == 0 or j == len(row) - 1:
            continue
        if cell < 0:
            rows[i-1][j-1] += 1
            rows[i-1][j] += 1
            rows[i-1][j+1] += 1
            rows[i][j-1] += 1
            rows[i][j+1] += 1
            rows[i+1][j-1] += 1
            rows[i+1][j] += 1
            rows[i+1][j+1] += 1
            
for (i, row) in enumerate(rows):
    if i == 0 or i == len(rows) - 1:
        continue
    for (j, cell) in enumerate(row):
        if j == 0 or j == len(row) - 1:
            continue
        if cell < 0:
            print('#', end='')
        else:
            print(str(cell), end='')
    print('')

Submission status: Accepted

Solution code:
#■標準入力ショートカット


def get_next_int():
    return int(float(input()))


def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])


def get_next_str():
    return input()


def get_next_strs(delim=" "):
    return tuple(input().split(delim))


def get_next_by_types(*value_types, delim=" "):
    return tuple([t(x) for t, x in zip(value_types, input().split(delim))])


def isMine(x, y, field):
    return field[y][x] == '#'


def count(H, W, x, y, field):
    diff = [-1, 0, 1]
    count = 0
    for d in diff:
        if x + d >= 0 and x + d < W:
            for d2 in diff:
                if y + d2 >= 0 and y + d2 < H:
                    count += 1 if isMine(x + d, y + d2, field) else 0
    return count


def solve():
    H, W = get_next_ints()
    field = []
    for i in range(H):
        field.append(list(get_next_str()))
    for y, line in enumerate(field):
        for x, cell in enumerate(line):
            if cell == '.':
                field[y][x] = str(count(H, W, x, y, field))
    for line in field:
        print(''.join(line))

solve()