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:
import sys

input = sys.stdin.readline


def main():
    H, W = [int(x) for x in input().split()]
    S = [input().strip() for _ in range(H)]

    A = [[""] * W for j in range(H)]

    for j in range(H):
        for i in range(W):
            if S[j][i] == "#":
                A[j][i] = '#'
            else:
                cnt = 0
                for dj, di in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)):
                    if 0 <= j + dj <= H - 1 and 0 <= i + di <= W - 1:
                        if S[j + dj][i + di] == '#':
                            cnt += 1

                A[j][i] = str(cnt)

    for a in A:
        print("".join(a))








if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
X = [-1, 0, 1]
Y = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        if S[i][j] == '.':
            count = 0
            for x in X:
                for y in Y:
                    if (i == 0 and x == -1) or (i == h-1 and x == 1):
                        continue
                    if (j == 0 and y == -1) or (j == w-1 and y == 1):
                        continue
                    if S[i+x][j+y] == '#':
                        count += 1
        else:
            continue
        S[i][j] = count

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

Submission status: Accepted

Solution code:
from itertools import product

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

G=[[0] * W for h in range(H)]
P=[(y,x) for y,x in product(range(-1,2),range(-1,2)) if not (y==0 and x==0)]

for x,y in product(range(W),range(H)):
    if S[y][x]=="#":
        G[y][x]="#"
        continue

    for p in P:
        c_y=y+p[0]
        c_x=x+p[1]
#         print(c_x,c_y)
        if c_x <0 or c_x>=W or c_y<0 or c_y>=H: 
            continue
#         print(c_x,c_y)
        if S[c_y][c_x]=="#":
            G[y][x]+=1

_=[print("".join(map(str,r))) for r in G]

Submission status: Accepted

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

if h == 1:
    if w==1:
        if g[0][0]=="#":
            print("#")
        else:
            print("0")
    else:
        wn = ""
        for u in range(w):
            if g[0][u] == ".":
                if u==0:
                    stri = g[0][u+1]
                elif u==w-1:
                    stri = g[0][u-1]
                else:
                    stri = g[0][u-1:u+2]

                wn += str(stri.count("#"))
            else:
                wn += "#"
        print(wn)

elif w == 1:
    for v in range(h):
        wn = ""
        if g[v][0]== ".":
            if v==0:
                stri = g[v+1][0]
            elif v==h-1:
                stri = g[v-1][0]
            else:
                stri = g[v-1][0]+g[v+1][0]
            wn += str(stri.count("#"))
        else:
            wn += "#"
        print(wn)
        
else:
    for s in range(h):
        wn = ""
        for t in range(w):

            if g[s][t] == ".":
                if s==0:
                    if t==0:
                        stri =g[s][t+1]+g[s+1][t:t+2]
                    elif t==w-1:
                        stri = g[s][t-1]+g[s+1][t-1:t+1]
                    else:
                        stri = g[s][t-1:t+2]+g[s+1][t-1:t+2]
                        
                elif s==h-1:
                    if t==0:
                        stri = g[s-1][t:t+2]+g[s][t+1]
                    elif t==w-1:
                        stri = g[s-1][t-1:t+1]+g[s][t-1]
                    else:
                        stri = g[s-1][t-1:t+2]+g[s][t-1:t+2]
                else:
                    if t==0:
                        stri = g[s-1][t:t+2]+g[s][t+1]+g[s+1][t:t+2]
                    elif t==w-1:
                        stri = g[s-1][t-1:t+1]+g[s][t-1]+g[s+1][t-1:t+1]
                    else:
                        stri = g[s-1][t-1:t+2]+g[s][t-1]+g[s][t+1]+g[s+1][t-1:t+2]

                wn += str(stri.count("#"))
            else:
                wn += "#"

        print(wn)

Submission status: Accepted

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

def mine(x,y,grid):
    if grid[x][y] == ".":
        cnt = 0
        for dx,dy in ((1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)):
            nx,ny = x + dx,y + dy
            if 0 <= nx and 0 <= ny and nx <= h-1 and ny <= w-1 and grid[nx][ny] == "#":
                cnt += 1
        grid[x][y] = str(cnt)    
        
for i in range(h):
    for j in range(w):
        mine(i,j,s)
        
for i in range(h):
    print("".join(s[i]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
maze = []
maze.append('-'*(W+2))
for _ in range(H):
    S = input()
    maze.append('-'+S+'-')
maze.append('-'*(W+2))
# print(maze)

new_maze = [['' for _ in range(W)] for _ in range(H)]
for i in range(1, H+1):
    for j in range(1, W+1):
        if maze[i][j] == '.':
            count = 0
            for k in [-1, 0, 1]:
                for l in [-1, 0, 1]:
                    if k == 0 and l == 0:
                        continue
                    if maze[i+k][j+l] == '#':
                        count += 1
            new_maze[i-1][j-1] = str(count)
        else:
            new_maze[i-1][j-1] = '#'
    print(*new_maze[i-1], sep='')
# print(new_maze)

Submission status: Accepted

Solution code:
mine = []
h, w = map(int, input().split())
for i in range(h):
    mine.append(list(input()[:]))
ans = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        if mine[i][j] == '#':
            if i != 0:
                ans[i-1][j] += 1
            if j != 0:
                ans[i][j-1] += 1
            if i != h-1:
                ans[i+1][j] += 1
            if j != w-1:
                ans[i][j+1] += 1
            if i != 0 and j != 0:
                ans[i-1][j-1] += 1
            if i != 0 and j != w-1:
                ans[i-1][j+1] += 1
            if i != h-1 and j != 0:
                ans[i+1][j-1] += 1
            if i != h-1 and j != w-1:
                ans[i+1][j+1] += 1

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

Submission status: Accepted

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

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

for line in S:
    line.insert(0, ".")
    line.append(".")
S.insert(0, ["."]*(W+2))
S.append(["."]*(W+2))

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j] == ".":
            count = S[i-1][j-1:j+2].count("#") + S[i][j-1:j+2].count("#") + S[i+1][j-1:j+2].count("#")
            S[i][j] = count

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

Submission status: Accepted

Solution code:
import numpy as np
h, w = map(int, input().split())
s = np.array([list(input()) for i in range(h)])
a=s
c = 0

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

for ai in a:
	print("".join(ai))

Submission status: Accepted

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

def check(x,y,S):
    cnt = 0
    for dx in range(-1,2):
        for dy in range(-1,2):
            sx = x + dx
            sy = y + dy
            if sx < 0 or sx >= H or sy < 0 or sy >= W:
                continue
            if S[sx][sy] == "#":
                cnt += 1
    S[x][y] = str(cnt)
    return S

for y in range(W):
    for x in range(H):
        if S[x][y] == "#":
            continue
        S = check(x,y,S)
for i in S:
    print("".join(i))

Submission status: Accepted

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

Step = [
    [-1, -1],
    [-1,  0],
    [-1,  1],
    [0,   1],
    [1,   1],
    [1,   0],
    [1,  -1],
    [0,  -1],
]

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            C[i] += "#"
        else:
            ans = 0
            for one in Step:
                y = i + one[0]
                x = j + one[1]
                if 0 <= y and y < H and 0 <= x and x < W:
                    if S[y][x] == "#":
                        ans += 1
            C[i] += str(ans)
        
for i in range(H):
    print(C[i])

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

h,w = iim()
l = [['.']*(w+2)]
for i in range(h):
    l.append(list('.'+input()+'.'))
l.append(['.']*(w+2))
'''
for i in l:
    print(i)
'''
for row in range(1,h+1):
    for col in range(1,w+1):
        if l[row][col] != '#':
            li = [l[row-1][col-1],l[row-1][col],l[row-1][col+1],l[row][col-1],l[row][col+1],l[row+1][col-1],l[row+1][col],l[row+1][col+1]]
            l[row][col] = li.count('#')

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

Submission status: Accepted

Solution code:

def myAnswer(H:int,W:int,S:list) -> None:
   ans = [["#" for _ in range(W+2)]]
   index = []
   for i,s in enumerate(S):
      tmp = ["#"]
      for j,b in enumerate(s):
         if(b == "."):
            tmp.append(0)
         else:
            index.append([i+1,j+1])
            tmp.append(b)
      tmp.append("#")
      ans.append(tmp)
   ans.append(["#" for i in range(W+2)])
   for i,j in index:
      for n in range(i-1,i+2,1):
         for m in range(j-1,j+2,1):
            if(ans[n][m]!="#"):
               ans[n][m] +=1
   ans.pop(0)
   ans.pop()
   for a in ans:
      a.pop(0)
      a.pop(-1)
      string = ""
      for s in a:
         string += str(s)
      print(string)
      
def main():
   H,W = map(int,input().split())
   S = [list(input()) for _ in range(H)]
   myAnswer(H,W,S[:])
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
for i in range(H):
    for j in range(W):
        tmp = 0
        if s[i][j] == '.':
            if i >= 1 and j >= 1:
                if s[i-1][j-1] == '#': tmp += 1
            if i >= 1:
                if s[i-1][j] == '#': tmp += 1
            if i >= 1 and j < W-1:
                if s[i-1][j+1] == '#': tmp += 1
            if j >= 1:
                if s[i][j-1] == '#': tmp += 1
            if j < W-1:
                if s[i][j+1] == '#': tmp += 1
            if i < H-1 and j >= 1:
                if s[i+1][j-1] == '#': tmp += 1
            if i < H-1:
                if s[i+1][j] == '#': tmp += 1
            if i < H-1 and j < W-1:
                if s[i+1][j+1] == '#': tmp += 1

            s[i][j] = str(tmp)

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

bomb = np.array(np.where(s == '#')).T
n = np.zeros(s.shape, dtype=np.int64)
for k in bomb:
    i, j = k
    n[max(0,i-1), max(0,j-1)] += 1
    try: n[i+2, max(0,j-1)] -= 1
    except IndexError: pass
    try: n[max(0,i-1), j+2] -= 1
    except IndexError: pass
    try: n[i+2, j+2] += 1
    except IndexError: pass
n = np.cumsum(np.cumsum(n,axis=0), axis=1)
ans = np.where(s == '#' , '#', n)
for row in ans:
    print(*row, sep='')

Submission status: Accepted

Solution code:
from collections import Counter
import numpy as np
h, w = list(map(int, input().split()))
s = np.array([list(input()) for i in range(h)])

cnt = 0
ans = [[] for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == ".":
      xm = max(0, j-1)
      ym = max(0, i-1)
      xp = min(w-1, j+1)
      yp = min(h-1, i+1)
      temp = s[ym:yp+1, xm:xp+1].reshape(-1).tolist()
      ans[i].append(str(Counter(temp)["#"]))
     
    else:
      ans[i].append("#")
      
for ai in ans:
  print("".join(ai))

Submission status: Accepted

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            C[i][j] ="#"
        else:
            bottomi = i - 1 if i > 0 else 0
            bottomj = j - 1 if j > 0 else 0
            topi = i + 1 if i < H-1 else H-1
            topj = j + 1 if j < W-1 else W-1
            cnt = 0
            for k in range(bottomi,topi+1):
                for l in range(bottomj,topj+1):
                    if S[k][l] == "#":
                        C[i][j] += 1

for i in range(H):
    ST = ""
    for j in range(W):
        ST +=str(C[i][j])
    else:
        print(ST)

Submission status: Accepted

Solution code:
def resolve():
    H, W = list(map(int, input().split()))
    S = [input() for i in range(H)]
    A = []
    for i in range(H):
        line = ""
        for j in range(W):
            if S[i][j] == "#":
                line += "#"
                continue
            cnt = 0
            if i-1 >= 0:
                cnt += 1 if j-1 >= 0 and S[i-1][j-1] == "#" else 0
                cnt += 1 if S[i-1][j] == "#" else 0
                cnt += 1 if j+1 < W and S[i-1][j+1] == "#" else 0

            cnt += 1 if j-1 >= 0 and S[i][j-1] == "#" else 0
            cnt += 1 if S[i][j] == "#" else 0
            cnt += 1 if j+1 < W and S[i][j+1] == "#" else 0

            if i+1 < H:
                cnt += 1 if j-1 >= 0 and S[i+1][j-1] == "#" else 0
                cnt += 1 if S[i+1][j] == "#" else 0
                cnt += 1 if j+1 < W and S[i+1][j+1] == "#" else 0
            
            line += str(cnt)
        A.append(line)
    
    [print(l) for l in A]

            

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    H, W = [int(item) for item in input().split()]
    grid = [input() for _ in range(H)]

    res = ['' for _ in range(H)]



    def cnt_bomb(i, j):
        cnt = 0        

        for n in [-1, 0, 1]:
            for m in [-1, 0, 1]:
                cnt += chk_bomb(i+n, j+m)

        return str(cnt)


    def chk_bomb(i, j):

        if i < 0 or  H <= i or j < 0 or W <= j:
            return 0
        elif grid[i][j] == '#':
            return 1
        else:
            return 0



    for i in range(H):
        for j in range(W):
            if grid[i][j] == '#':
                res[i] += '#'
            else:
                res[i] += cnt_bomb(i, j)

    for line in res:
        print(line)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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


for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            print("#",end="")
        else:
            cnt=0
            for x in range(-1,2):
                for y in range(-1,2):
                    if 0<=i+x<h and 0<=j+y<w and s[i+x][j+y]=="#":
                        cnt+=1
            print(cnt,end="")
    print()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [input() for i in range(H)]
T = [[0 for i in range(W)] for j in range(H)]
# print(S)
# print(T)
for i in range(H):
    for j in range(W):
        if S[i][j]=="#":
            T[i][j]="#"
            for il in range(max(0,i-1),min(i+1,H-1)+1):
                for jl in range(max(0, j-1), min(j+1, W-1)+1):
                    # print(i,j,il,jl)
                    if S[il][jl] is not "#":
                        T[il][jl]+=1


for i in range(H):
    print(''.join(map(str, T[i])))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[input() for _ in range(H)]
for i in range(H):
  T=''
  for j in range(W):
    if S[i][j] == '#':
      T+='#'
    else:
      T+=str(sum([x[max(0,j-1):min(j+2,W)].count('#') for x in S[max(0,i-1):min(i+2,H)]]))
    
  print(T)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [input() for _ in range(H)]
answer = [[0 if cell == '.' else '#' for cell in row] for row in S]

import itertools
for i in range(H):
  for j in range(W):
    if S[i][j] != '#':
      continue
    for dx,dy in itertools.product([-1,0,1],repeat=2):
      x = i+dx
      y = j+dy
      if x < 0 or x >= H:
        continue
      if y < 0 or y >= W:
        continue
      if S[x][y] == '#':
        continue
      answer[x][y] += 1

      
for row in answer:
  print(*row,sep='')

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
MAP = []
tx = [1,1,0,-1,-1,-1,0,1]
ty = [0,1,1,1,0,-1,-1,-1]
for _ in range(H):
    MAP.append(list(input()))
for x in range(H):
    for y in range(W):
        if MAP[x][y] == '.':
            cnt = 0
            for i in range(8):
                nx = x + tx[i]
                ny = y + ty[i]
                if 0 <= nx <= H - 1 and 0 <= ny <= W - 1 and MAP[nx][ny] == '#':
                    cnt += 1
            MAP[x][y] = str(cnt)
for i in range(H):
    print(''.join(MAP[i]))

Submission status: Accepted

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

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

for h in range(H):
    t = ''
    for w in range(W):
        if s[h][w] == '#':
            t += '#'
            continue

        cnt = 0
        if w < W - 1 and s[h][w + 1] == '#':
            cnt += 1

        if w < W - 1 and h < H - 1 and s[h + 1][w + 1] == '#':
            cnt += 1

        if h < H - 1 and s[h + 1][w] == '#':
            cnt += 1

        if h < H - 1 and w > 0 and s[h + 1][w - 1] == '#':
            cnt += 1

        if w > 0 and s[h][w - 1] == '#':
            cnt += 1

        if w > 0 and h > 0 and s[h - 1][w - 1] == '#':
            cnt += 1

        if h > 0 and s[h - 1][w] == '#':
            cnt += 1

        if h > 0 and w < W - 1 and s[h - 1][w + 1] == '#':
            cnt += 1

        t += str(cnt)

    print(t)

Submission status: Accepted

Solution code:

h, w = map(int, input().split())

s = [list(input()) for _ in range(h)]
ans = [[0] * w for _  in range(h)]
for i in range(h):
    for j in range(w):
        ans[i][j] += s[i][max(j - 1, 0):min(j + 2, w)].count("#")
        if i != 0:
            ans[i][j] += s[i - 1][max(j - 1, 0):min(j + 2, w)].count("#")
        if i != h - 1:
            ans[i][j] += s[i + 1][max(j - 1, 0):min(j + 2, w)].count("#")

        
            

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    S[i] = list(input())
for i in range(H):
    for j in range(W):
        if (S[i][j] == '.'):
            count = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if(0<=i+a<=H-1 and 0<=j+b<=W-1):
                        if(S[i+a][j+b] == '#'):
                            count += 1
            S[i][j] = count
    print(*S[i], sep='')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [[i for i in input()] for _ in range(h)]
count = 0
if h==1 and w==1:
    if s[0][0]=='#':
        print('#')
    else:
        print(0)
    exit(0)
if h==1:
    for j in range(w):
        if s[0][j]=='.':
            if j == 0:
                if s[0][j+1]=='#':
                    count += 1
                s[0][j]=str(count)
                count=0
            elif j == w-1:
                if s[0][j-1]=='#':
                    count += 1
                s[0][j]=str(count)
                count=0
            else:
                if s[0][j+1]=='#':
                    count += 1
                if s[0][j-1]=='#':
                    count += 1
                s[0][j]=str(count)
                count=0
    print(''.join(s[0]))
    exit(0)
if w==1:
    for i in range(h):
        if s[i][0]=='.':
            if i == 0:
                if s[i+1][0]=='#':
                    count += 1
                s[i]=str(count)
                count=0
            elif i == h-1:
                if s[i-1][0]=='#':
                    count += 1
                s[i]=str(count)
                count=0
            else:
                if s[i+1][0]=='#':
                    count += 1
                if s[i-1][0]=='#':
                    count += 1
                s[i][0]=str(count)
                count=0
    for i in range(h):
        print(''.join(s[i][0]))
    exit(0)

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

                    s[i][j]=str(count)
                    count = 0
for i in range(h):
    print(''.join(s[i]))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc075/tasks/abc075_b
h, w = map(int, input().split())
matrix = []
for _ in range(h):
    row = list(input())
    matrix.append(row)

for i in range(h):
    for j in range(w):
        # 上、右上、右、右下、下、左下、左、左上
        yd = [-1, -1, 0, 1, 1, 1, 0, -1]
        xd = [0, 1, 1, 1, 0, -1, -1, -1]
        if matrix[i][j] == '.':
            bomb = 0
            for k in range(8):
                y = i + yd[k]
                x = j + xd[k]
                if 0 <= y < h and 0 <= x < w and matrix[y][x] == '#':
                    bomb += 1
            matrix[i][j] = str(bomb)

for r in matrix:
    print(''.join(r))

Submission status: Accepted

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

#print(S)
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            mass = 0
            for s in range(-1, 2):
                for t in range(-1, 2):
                    if H > i+s >= 0 and W > j+t >= 0:
                        #print(i,j)
                        #print(i+s,j+t)
                        if S[i+s][j+t] == "#":

                            mass += 1
            S[i][j] = str(mass)
for i in range(H):
    l = "".join(S[i])
    print(l)

Submission status: Accepted

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

for a in range(H):
    for b in range(W):
        if L[a][b]=='.':
             num=0
             for k in range(-1,2):
                 for l in range(-1,2):
                    if 0<=a+k<=H-1 and 0<=b+l<=W-1 and L[a+k][b+l]=='#':
                        num+=1
             L[a][b]=num
    print(*L[a],sep='')

Submission status: Accepted

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

for i in range(h):
    s_l[i].insert(w,'*')
    s_l[i].insert(0,'*')

s_add = ['*']*(w+2)
s_l.insert(h,s_add)
s_l.insert(0,s_add)

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

s_l_result = s_l[1:h+1]
for i in range(h):
    print(''.join(s_l_result[i][1:w+1]))

Submission status: Accepted

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

for i in range(h):
    s_l[i].insert(w,'*')
    s_l[i].insert(0,'*')

s_add = ['*']*(w+2)
s_l.insert(h,s_add)
s_l.insert(0,s_add)

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

s_l_result = s_l[1:h+1]
for i in range(h):
    print(''.join(s_l_result[i][1:w+1]))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    S[i] = list(input())
for i in range(H):
    for j in range(W):
        if (S[i][j] == '.'):
            count = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if(0<=i+a<=H-1 and 0<=j+b<=W-1):
                        if(S[i+a][j+b] == '#'):
                            count += 1
            S[i][j] = count
    print(*S[i], sep='')