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())
ans = []
for h in range(H+2):
    ans.append([0] * (W + 2))
qu = []
for h in range(H):
    qu.append(input())

for h in range(H):
    for w in range(W):
        if qu[h][w] == "#":
            for i in range(3):
                for j in range(3):
                    if ans[h+i][w+j] != "#":
                        ans[h+i][w+j] += 1
            ans[h+1][w+1] = "#"

for h in range(H):
    result = map(str, ans[h+1][1:W+1])
    print(''.join(result))

Submission status: Accepted

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

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

#8方向走査
#下、右、上、左、右下、右上、左下、左上
dy = [1, 0, -1, 0, 1, -1, 1, -1]
dx = [0, -1, 0, 1, -1, -1, 1, 1]


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

        if field[y][x] == "#":
            ans[y][x] = "#"
            continue

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

            if 0 <= ny and ny < H and 0 <= nx and nx < W:
                if field[ny][nx] == "#":
                    cnt += 1

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

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

Submission status: Accepted

Solution code:
i = list(map(int,input().split()))
H = i[0]
W = i[1]


#二次元配列の作成
#ひとまわり大きく作る
masu = []
for i in range(H+2):
    sublist = []
    if(i == 0)or(i == H+1):
        sublist = ['.']*(W+2)
        masu.append(sublist)
        continue
    a = input()
    for k in a:
        sublist.append(k)

    sublist.insert(0,'.')
    sublist.append('.')
    masu.append(sublist)



count = 0
for j in range(1,H+1):
    for k in range(1,W+1):
        if(masu[j-1][k-1] == '#'):
            count += 1

        if(masu[j-1][k] == '#'):
            count += 1

        if(masu[j-1][k+1] == '#'):
            count += 1

        if(masu[j][k-1] == '#'):
            count += 1

        if(masu[j][k+1] == '#'):
            count += 1

        if(masu[j+1][k-1] == '#'):
            count += 1

        if(masu[j+1][k] == '#'):
            count += 1

        if(masu[j+1][k+1] == '#'):
            count += 1

        if(masu[j][k] != '#'):
            masu[j][k] = count

        count = 0

for i in range(1,H+1):
    masu[i].pop(0)
    masu[i].pop(-1)
    result = ''.join(map(str,masu[i]))
    print(result)

Submission status: Accepted

Solution code:
'''
8方向走査

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

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

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

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

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

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

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

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

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

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

        cnt = 0

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

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

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

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

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

Submission status: Accepted

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

A = []
B = []
for i in range(w+2):
    B.append(".")
A.append(B)
for i in range(h):
    s = "." + input() + "."
    A.append(list(s))
A.append(B)


for i in range(1, h+1):
    for j in range(1, w+1):
        # print(A[i][j], end="")
        C = []
        if A[i][j] == "#":
            print("#", end="")
        else:
            for k in range(-1, 2):
                for l in range(-1, 2):
                    C.append(A[i+k][j+l])
            print(C.count("#"), end = "")
    print()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
board=[list(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 board[i][j]==".":
            cnt=0
            for k in range(8):
                x=i+dx[k]
                y=j+dy[k]
                if 0<=x<H and 0<= y <W:
                    if board[x][y]=='#':
                        cnt+=1
            board[i][j]=str(cnt)
        
        
for b in board:
    b = map(str, b)
    print("".join(b))

Submission status: Accepted

Solution code:
x,y=list(map(int, input().split()))
s=[list(map(str,list(input()))) for i in range(x)]

count=0
for i in range(x):
	for j in range(y):
		count=0
		if s[i][j]=='.':
			if i>0 and j>0 and s[i-1][j-1]=='#':
				count=count+1
			if i>0 and s[i-1][j]=='#':
				count=count+1
			if i>0 and j<y-1 and s[i-1][j+1]=='#':
				count=count+1
			if j>0 and s[i][j-1]=='#':
				count=count+1
			if j<y-1 and s[i][j+1]=='#':
				count=count+1
			if i<x-1 and j>0 and s[i+1][j-1]=='#':
				count=count+1
			if i<x-1 and s[i+1][j]=='#':
				count=count+1
			if i<x-1 and j<y-1 and s[i+1][j+1]=='#':
				count=count+1
			s[i][j]=count
for a in s:
	print(*a,sep='')

Submission status: Accepted

Solution code:
x,y = [int(i) for i in input().split()]
ans = []
for i in range(x):
	ans.append(list(input()))
for i in range(x):
	for j in range(y):
		if ans[i][j] == '.':
			d = 0
			for k in range(i-1,i+2):
				for l in range(j-1,j+2):
					if 0 <= k and k < x and 0 <= l and l < y:
						if ans[k][l] == '#':
							d += 1
							ans[i][j] = str(d)
for i in range(x):
	for j in range(y):
		if ans[i][j] == '.':
			ans[i][j] = '0'
for i in ans:
	print(''.join(i))

Submission status: Accepted

Solution code:
H, W = list(map(int, input().split()))
S = []
for _ in range(H):
  S.append([0 if s == '.' else s for s in input()])
  
posi = [(-1,-1), (-1,0), (-1,1), (0, -1), (0, 1), (1, -1), (1,0), (1,1)]
for h in range(H):
  for w in range(W):
    koushin = [1]*8
    if S[h][w] == '#':
      if h == 0:
        koushin[0],koushin[1],koushin[2] = 0,0,0
      if h == H-1:
        koushin[5],koushin[6],koushin[7] = 0,0,0
      if w == 0:
        koushin[0],koushin[3],koushin[5] = 0,0,0
      if w == W-1:
        koushin[2],koushin[4],koushin[7] = 0,0,0
      for i in range(8):
        if koushin[i]:
          if S[h+posi[i][0]][w+posi[i][1]] != '#':
            S[h+posi[i][0]][w+posi[i][1]] += 1

for Si in S:
  print(''.join(map(str, Si)))

Submission status: Accepted

Solution code:
## coding: UTF-8
s = input().split()
t = [int(p) for p in s]

a = []
H = t[0]
W = t[1]
for i in range(H):
    a.append(input())


#print(t)



#print(H)
#print(W)
#print(a)



#上と下に入れる行を作る
b = ''
for i in range(W+2):
    b += '.'
#print(b)

'''
c = 'rei'
c = '.' + c
print(c)

'''
#上と下と右と左に空きマスを挿入する
a.insert(0, b)
for i in range(1, 1+H):
    a[i] = '.' + a[i] + '.'
a.append(b)

#print(a)




#周りの爆弾の数を数える
def countbomb(l, h, w):
    count = 0
    for i in range(w-1, w+2):
        if(l[h-1][i] == '#'):
            count += 1
        if(l[h+1][i] == '#'):
            count += 1
    if(l[h][w-1] == '#'):
        count += 1
    if(l[h][w+1] == '#'):
        count += 1
    return count

#print(countbomb(a, 3, 4))



for i in range(1, H+1):
    d = ''
    for j in range(1, W+1):
        if(a[i][j] == '#'):
            d += '#'
        else:
            d += (str(countbomb(a, i, j)))
    print(d)

Submission status: Accepted

Solution code:
# Author: cr4zjh0bp
# Created: Thu Mar 12 15:30:03 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

H, W = na()
M = nsn(H)
ans = [[-1] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if M[i][j] != '#':
            cnt = 0
            for a in range(-1, 2):
                for b in range(-1, 2):
                    ni, nj = i + a, j + b
                    if ni >= 0 and ni < H and nj >= 0 and nj < W and M[ni][nj] == '#':
                        cnt += 1
            ans[i][j] = cnt

for i in range(H):
    for j in range(W):
        if ans[i][j] == -1:
            print("#", end="")
        else:
            print(ans[i][j], end="")
    print()

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
      if squares[i][j] == '#':
          if not j - 1 < 0:
              squares_target[i][j-1] += 1
          if not j - 1 < 0 and not i - 1 < 0:
              squares_target[i-1][j-1] += 1
          if not i - 1 < 0:
              squares_target[i-1][j] += 1
          if not j + 1 > w -1 and not i - 1 < 0:
              squares_target[i-1][j+1] += 1
          if not j + 1 > w -1 :
              squares_target[i][j+1] += 1
          if not j + 1 > w -1 and not i + 1 > h -1:
              squares_target[i+1][j+1] += 1
          if not i + 1 > h - 1:
              squares_target[i+1][j] += 1
          if not j - 1 < 0 and not i + 1 > h - 1:
              squares_target[i+1][j-1] += 1

for i in range(h):
    for j in range(w):
        if squares[i][j] == '#':
            squares_target[i][j] = '#'
    print(''.join(map(str, squares_target[i])))

Submission status: Accepted

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

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


for i in range(1,h+1):
    for g in range(1,w+1):
        b=0
        for l in [-1,0,1]:
            for m in [-1,0,1]:
                if not(l==0 and m==0) and s[i+l][g+m]=="#":
                    b+=1
        if s[i][g]==".":
            ans[i-1][g-1]=b
        else:
            ans[i-1][g-1]="#"

for i in range(h):
    st=""
    for g in range(w):
        st+=str(ans[i][g])
    print(st)

Submission status: Accepted

Solution code:

# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():


    H, W = map(int, input().split())
    maze = [input() for _ in range(H)]
    temp = [[0] * W for _ in range(H)]



    for h in range(H):
        for w in range(W):
            if maze[h][w] == ".":
                cnt=0
                for k, l in ([1, 0], [-1, 0], [0, 1], [0, -1],[1,1],[-1,1],[-1,-1],[1,-1]):

                    nh, nw = h + k, w + l
                    if nh < 0 or nh >= H or \
                            nw < 0 or nw >= W:
                        continue

                    elif maze[nh][nw] == "#":
                        cnt+=1

                temp[h][w]=cnt
            else:
                temp[h][w]='#'


    for i in temp:
        print(*i,sep='')

resolve()

Submission status: Accepted

Solution code:
def minesweeper(grid, x, y):
    if grid[x][y] == '.':
        cnt = 0
        for i in range(x-1, x+2): 
            if i < 0 or H-1 < i:
                pass
            else :
                for j in range(y-1, y+2):
                    if j < 0 or W - 1 < j:
                        pass
                    else :
                        if grid[i][j] == '#':
                            cnt += 1
        grid[x][y] = str(cnt)
    return grid


H, W = [int(i) for i in input().split()]

grid = []
for h in range(H):
    grid.append([l for l in input()])
    
for x in range(H):
    for y in range(W):
        grid = minesweeper(grid, x, y)
        
for row in grid:
    print(''.join(row))

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
c=[]
for i in range(a):
  c+=[list(input())]
for i in range(a):
  for j in range(b):
    if c[i][j]=='.':
      d=0
      for k in range(i-1,i+2):
        for l in range(j-1,j+2):
          if 0<=k and k<a and 0<=l and l<b:
            d+=c[k][l]=='#'
      c[i][j]=str(d)
for i in c:
  print(''.join(i))

Submission status: Accepted

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

dx = [-1, 0, 1, 1, 1, 0, -1, -1]  #任意のマスの周り8方向のマスの差分を
dy = [1, 1, 1, 0, -1, -1, -1, 0]  #左上から時計回りに用意した

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            bomb = 0
            for k in range(8):
                if 0 <= i + dy[k] < h and 0 <= j + dx[k] < w:
                    if s[i+dy[k]][j+dx[k]] == '#':
                        bomb += 1
            s[i][j] = bomb
for i in s:
    print(*i, sep='')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
for i in range(h):
    s[i].insert(0,'9')
    s[i].append('9')
wall = ['9' for _ in range(w+2)]
s.insert(0, wall)
s.append(wall)
ans = [[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
    for j in range(1, w+1):
        if s[i][j] == '.':
            cnt = 0
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if s[k][l] == '#':
                        cnt += 1
            s[i][j] = cnt
        ans[i-1][j-1] = s[i][j]
for i in ans:
    print(*i, sep='')

Submission status: Accepted

Solution code:
inH, inW = map(int, input().split())
inMap = list()

for i in range(inH):
  inMap.append(list(input()))

def cntB(x,y):

  mv = [-1, 0, 1]
  cnt=0
  for dx in mv:
    if x+dx < 0 or x+dx>inH-1:
      continue
    for dy in mv:
      if y+dy<0 or y+dy>inW-1:
        continue
      if inMap[x+dx][y+dy]=="#":
        cnt+=1
      dy+=1
    dx+=1
    
  return cnt

for i in range(inH):
  s=""
  for j in range(inW):
    if inMap[i][j] == ".":
      s+=str(cntB(i,j))
    else:
      s+=str((inMap[i][j]))
  print(s)

Submission status: Accepted

Solution code:
# 相対的な座標
dy = [-1, -1, -1, 0, 0, 1, 1, 1]
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
field = []
H, W = map(int, input().split())
for _ in range(H):
    field.append(list(input()))
for y in range(H):
    for x in range(W):
        if field[y][x] == '#':
            continue
        count = 0
        for k in range(8): # 8方向
            nx = x + dx[k]
            ny = y + dy[k]
            if nx < 0 or W <= nx or ny < 0 or H <= ny: # 枠外
                continue
            if field[ny][nx] == '#':
                count += 1
        field[y][x] = str(count)
for h in range(H):
    print("".join(field[h]))

Submission status: Accepted

Solution code:
def minesweeper(grid, x, y):
    if grid[x][y] == '.':
        cnt = 0
        for i in range(x-1, x+2): 
            if i < 0 or H-1 < i:
                pass
            else :
                for j in range(y-1, y+2):
                    if j < 0 or W - 1 < j:
                        pass
                    else :
                        if grid[i][j] == '#':
                            cnt += 1
        grid[x][y] = str(cnt)
    return grid


H, W = [int(i) for i in input().split()]

grid = []
for h in range(H):
    grid.append([l for l in input()])
    
for x in range(H):
    for y in range(W):
        grid = minesweeper(grid, x, y)
        
for row in grid:
    print(''.join(row))

Submission status: Accepted

Solution code:
def resolve():
    h, w = map(int, input().split())
    L = []
    dx = [-1, -1, -1, 0, 0, 1, 1, 1]
    dy = [-1, 0, 1, -1, 1, -1, 0, 1]
    for i in range(h):
        L.append(list(input()))

    for i in range(h):
        for j in range(w):
            if L[i][j] == '#':
                pass
            else:
                count = 0
                for k in range(8):
                    x = i + dx[k]
                    y = j + dy[k]

                    if x < 0 or x > h - 1:
                        pass
                    elif y < 0 or y > w - 1:
                        pass
                    else:
                        if L[x][y] == "#":
                            count += 1
                L[i][j] = count

    for i in range(h):
        print(''.join(list(map(str, L[i]))))

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ 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):
  ans = ''
  for j in range(W):
    if S[i][j] == '#':
      ans += '#'
    else:
      count = 0  
      for d in range(8):
        x = i + dx[d]
        y = j + dy[d]
        if x in range(0,H) and y in range(0,W):
          if S[x][y] == '#':
            count += 1
      ans += str(count)
  print(ans)

Submission status: Accepted

Solution code:
H,W  = [int(x) for x in input().split()]
meiro= [list(input()) for _ in range(H)]
ans = [["0"]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    s = meiro[i][j]
    if s=="#":
      for a in range(3):
        for b in range(3):
          if 0<=i-1+a<H and 0<=j-1+b<W and ans[i-1+a][j-1+b]!="#":
            ans[i-1+a][j-1+b]= str(int(ans[i-1+a][j-1+b])+1)
      ans[i][j] ="#"
for i in range(H):
  print("".join(ans[i]))

Submission status: Accepted

Solution code:
#ABC075

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

d = [[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]]
for i in range(h):
    for j in range(w):
        
        if s[i][j] == ".":
            count = 0
            for k in range(8):
                
                if 0 <= i+d[k][0] <= h-1 and 0 <= j+d[k][1] <= w-1:
                    
                    if s[i+d[k][0]][j+d[k][1]] == "#":
                        count += 1
            
            s[i][j] = str(count)


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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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


def main():
    h, w = map(int, input().split())
    grid = []

    for i in range(h):
        grid.append(list(input()))

    # print(h,w,grid,sep=' ')

    for i in range(h):
        for j in range(w):
            # print(i,j,sep=' ')

            if grid[i][j] == '#':
                continue

            cnt = 0

            for k in range(8):
                y = i + dy[k]
                x = j + dx[k]
                # print(k,x,y,sep='~')

                if y >= 0 and y < h and x >= 0 and x < w:
                    if grid[y][x] == '#':
                        cnt += 1
                else:
                    continue
            grid[i][j] = cnt
        # print(grid[i])

    for i in range(h):
        for j in range(w):
            print(grid[i][j], end="")

        print('')


main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import copy

H, W = map(int, input().split(' '))
S = [list(input().rstrip('\r\n')) for _ in range(H)]
ans = copy.copy(S)

def count_bomb(h, w):
    hd_list = [-1, 0, 1]
    wd_list = [-1, 0, 1]
    count = 0
    for hd in hd_list:
        if h + hd < 0 or h + hd >= H:
            continue
        for wd in wd_list:
            if w + wd < 0 or w + wd >= W:
                continue
            if hd == 0 and wd == 0:
                continue
            if S[h+hd][w+wd] == '#':
                count += 1
    return count

for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            continue
        ans[h][w] = str(count_bomb(h, w))
for h in range(H):
    print(''.join(ans[h]))

Submission status: Accepted

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

s = [list(input()) for _ in range(h)]
ans = [["#"]*w for _ in range(h)]

DX = [0, -1, -1, -1,  0,  1, 1, 1]
DY = [1,  1,  0, -1, -1, -1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
        count = 0
        for k in range(8):
            dx, dy = j+DX[k], i+DY[k]
            if not(0<=dx<=w-1 and 0<=dy<=h-1):
                continue
            if s[dy][dx] == "#":
                count += 1
        ans[i][j] = count
for a in ans:
    print(*a,sep="")

Submission status: Accepted

Solution code:
def resolve():
    h, w = map(int, input().split())
    L = [list(input()) for i in range(h)]
    dx = [-1, 0, 1, -1, 1, -1, 0, 1]
    dy = [-1, -1, -1, 0, 0, 1, 1, 1]
    count = 0
    for i in range(h):
        for j in range(w):
            if L[i][j] == "#":
                pass
            else:
                count = 0
                for k in range(8):
                    m = i + dx[k]
                    n = j + dy[k]
                    if m < 0 or n < 0:
                        pass
                    elif m >= h or n >= w:
                        pass
                    else:
                        if L[m][n] == "#":
                            count += 1
                L[i][j] = count
    for i in range(len(L)):
        print(''.join(map(str, L[i])))

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
ans = [[-1]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    cnt = 0
    if S[i][j] == "#":
      ans[i][j] = "#"
      continue
    for n in range(-1, 2):
      for m in range(-1, 2):
        if i+n < 0 or i+n > H-1: 
          continue
        if j+m < 0 or j+m > W-1:
          continue
        if S[i+n][j+m] == "#":
          cnt += 1
    ans[i][j] = cnt
for i in range(H):
  print(*ans[i], sep="")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
grid = [[-float("inf")]*W for _ in range(H)]
for i in range(H):
  s = input()
  for j in range(W):
    if s[j]==".":
      grid[i][j]=0
dire = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for r in range(H):
  for c in range(W):
    if grid[r][c] == -float("inf"):
      for d in dire:
        if 0 <= r+d[0] and r+d[0] < H and 0 <= c+d[1] and c+d[1] < W:
          grid[r+d[0]][c+d[1]] += 1
for r in range(H):
  for c in range(W):   
    if grid[r][c] == -float("inf"):
      grid[r][c] = "#"
    else:
      grid[r][c] = str(grid[r][c])
  print("".join(grid[r]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
ls = [input() for _ in range(h)]
ll = [['#']*w for _ in range(h)]
for x in range(h):
  for y in range(w):
    if ls[x][y]=='.':
      cnt = 0
      for dx,dy in ((1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,1),(-1,-1),(1,-1)):
        nx,ny = x+dx,y+dy
        if not(0<=nx<h and 0<=ny<w): continue
        if ls[nx][ny]=='#': cnt += 1
      ll[x][y] = cnt
for l in ll:
  print(''.join(map(str,l)))

Submission status: Accepted

Solution code:
H,W=[int(s) for s in input().split()]
game=[input() for i in range(H)]
ans=[[] for i in range(H)]
direction=[[1,1],[1,0],[0,1],[1,-1,],[-1,1],[-1,0],[0,-1],[-1,-1]]
for i in range(H):
  for j in range(W):
    if game[i][j]=='#':
      ans[i].append('#')
      continue
    count=0
    for e in direction:
      y=i+e[0]
      x=j+e[1]
      if (x<0 or x>=W or y<0 or y>=H):
        continue
      if game[y][x]=='#':
        count+=1
    ans[i].append(count)
for i in range(H):
  print(*ans[i],sep='')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = ['.'*(W+2)]
for _ in range(H):
    tmp = '.' + input() + '.'
    grid.append(tmp)
grid.append('.'*(W+2))
ans = []
for i in range(1,H+1):
    tmp = ''
    for k in range(1,W+1):
        if grid[i][k] == '#':
            tmp = tmp + '#'
        else:
            t = 0
            if grid[i][k-1] == '#':
                t += 1
            if grid[i][k+1] == '#':
                t += 1
            if grid[i+1][k] == '#':
                t += 1
            if grid[i-1][k] == '#':
                t += 1
            if grid[i-1][k-1] == '#':
                t += 1
            if grid[i+1][k+1] == '#':
                t += 1
            if grid[i+1][k-1] == '#':
                t += 1
            if grid[i-1][k+1] == '#':
                t += 1
            tmp = tmp + str(t)
    ans.append(tmp)
for i in ans:
    print(i)

Submission status: Accepted

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

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:
import numpy as np
h,w=map(int,input().split())
L=[[i for i in input()] for i in range(h)]
L_first=np.zeros((2,w),dtype=str)
L_first2=np.zeros((1,1),dtype=str)
L=np.array(L,dtype=str)
L=np.insert(L_first,1,L,axis=0)
L=np.insert(L,0,L_first2,axis=1)
L=np.insert(L,w+1,L_first2,axis=1)
ans=np.zeros((h,w),dtype=str)
for i in range(1,h+1):
    for j in range(1,w+1):
        num = 0
        if L[i][j]==".":
            L2=L[i-1:i+2,j-1:j+2]
            ans[i-1][j-1]=np.sum(L2=="#")
        else:ans[i-1][j-1]="#"
for i in range(h):
    print("".join(ans[i]))

Submission status: Accepted

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

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

ans = [[0 for j in range(M)] for i in range(H)]

for i in range(H):
  for j in range(M):
    if array[i][j] == '.':
      if i != 0 and array[i-1][j] == '#':
        ans[i][j] += 1
      if i != 0 and j != 0 and array[i-1][j-1] == '#':
        ans[i][j] += 1
      if i != 0 and j != M-1 and array[i-1][j+1] == '#':
        ans[i][j] += 1
      if j != 0 and array[i][j-1] == '#':
        ans[i][j] += 1
      if j != M-1 and array[i][j+1] == '#':
        ans[i][j] += 1
      if i != H-1 and array[i+1][j] == '#':
        ans[i][j] += 1
      if i != H-1 and j != 0 and array[i+1][j-1] == '#':
        ans[i][j] += 1
      if i != H-1 and j != M-1 and array[i+1][j+1] == '#':
        ans[i][j] += 1
    else:
      ans[i][j] = '#'
for i in range(H):
  print(''.join(list(map(str,ans[i]))))

Submission status: Accepted

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

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

dr = [-1, -1, -1, 0, 1, 1, 1, 0]
dc = [-1, 0, 1, 1, 1, 0, -1, -1]

for i in range(h):
    for j in range(w):
        cnt = 0
        for k in range(8):
            nr = i + dr[k]
            nc = j + dc[k]
            if 0 <= nr < h and 0 <= nc < w:
               if s[nr][nc] == '#':
                   cnt += 1
        if s[i][j] == '#':
            ans[i][j] = '#'
        else:
            ans[i][j] = str(cnt)

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

Submission status: Accepted

Solution code:
#075_B
h,w=map(int,input().split())
s=['.'*(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 s[i][j]=='.':
            s[i][j]=sum([s[i+_i][j+_j]=='#' for _i in range(-1,2) for _j in range(-1,2)])
    print(*s[i][1:w+1],sep='')

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mat = [input() for _ in range(n)]

for i in range(n):
    for j in range(m):
        if mat[i][j] == '.':
            score = 0
            if not i == 0:
                if mat[i-1][j] == '#': score += 1
            
            if not i == n - 1:
                if mat[i+1][j] == '#': score += 1
            
            if not j == 0:
                if mat[i][j-1] == '#': score += 1
            
            if not j == m - 1:
                if mat[i][j+1] == '#': score += 1
            
            if not i == 0 and not j == 0:
                if mat[i-1][j-1] == '#': score += 1
            
            if not i == 0 and not j == m - 1:
                if mat[i-1][j+1] == '#': score += 1
            
            if not i == n - 1 and not j == 0:
                if mat[i+1][j-1] == '#': score += 1
            
            if not i == n - 1 and not j == m - 1:
                if mat[i+1][j+1] == '#': score += 1

            print(score, end='')
        else:
            print(mat[i][j],end='')
    print()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
field=[["."]*(W+2)]
for i in range(H):
    field.append(["."])
    field[-1].extend(list(input()))
    field[-1].append(".")
field.append(["."]*(W+2))
for i in range(1,H+1):
    for j in range(1,W+1):
        if field[i][j]=="#":
            print("#",end="")
        else:
            cnt=0
            for dy in range(-1,2):
                for dx in range(-1,2):
                    if field[i+dy][j+dx]=="#":
                        cnt+=1
            print(cnt,end="")
    print("")

Submission status: Accepted

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

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

for y in range(H):
    for x in range(W):
        s = S[y][x]
        if s == "#":
            tmp = "#"
        else:
            bomb = 0
            for k in range(8):
                x_next = x + dx[k]
                y_next = y + dy[k]

                if x_next < 0 or y_next < 0 or x_next >= W or y_next >= H:
                    continue

                if S[y_next][x_next] == "#":
                    bomb += 1
            tmp = str(bomb)
        print(tmp, end="")
    print("")