Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.  

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).  

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.  

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.  

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310



Sample input:
3 5
.....
.#.#.
.....


Sample output:
11211
1#2#1
11211

Submission status: Accepted

Solution code:
def main():
    grid = []
    height, width = map(int, input().split())
    grid.append(["."] * (width + 2))
    search_directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for _ in range(height):
        grid.append(["."] + list(input()) + ["."])
    grid.append(["."] * (width + 2))
    for i in range(1, height + 1):
        for j in range(1, width + 1):
            if grid[i][j] == ".":
                count_bomb = 0
                for dir in search_directions:
                    if grid[i + dir[0]][j + dir[1]] == "#":
                        count_bomb += 1
                grid[i][j] = str(count_bomb)
    for i in range(1, height + 1):
        print("".join(grid[i][1:-1]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s_li = [input() for i in range(H)]
for h in range(H):
    for w in range(W):
        cnt = 0
        if s_li[h][w] == "#":
            print("#",end="")
            continue
        for i in range(-1, 2):
            for k in range(-1, 2):
                if 0 <= h + i < H and 0 <= w + k < W and s_li[h + i][w + k] == "#":
                    cnt += 1
                    # print("zahyo = ", h + i, w + k)
        print(cnt,end="")
    print()

Submission status: Accepted

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


#----------
H,W = list(map(int, input().rstrip().split()))
all_S_list = [ list(input().strip()) for _ in range(H) ]
#----------
new_S_list = [["_"]*W for i in range(H)]

for i in range(H):
    for j in range(W):
        if all_S_list[i][j] == "#":
            new_S_list[i][j] = "#"
        
        if all_S_list[i][j] == ".":
            d_i_list = [(-1)*(i>0) , 0 , 1*(i<(H-1)) ]
            d_j_list = [(-1)*(j>0) , 0 , 1*(j<(W-1)) ]
        
            count=0
            for d_i in set(d_i_list):
               for d_j in set(d_j_list):
                   if all_S_list[i + d_i][j + d_j] == "#":
                       count += 1
            
            new_S_list[i][j] = str(count)
            
for line in new_S_list:
    print("".join(line))

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    h, w = map(int, input().split())
    s = list()
    s.append('.' * (w+2))
    for i in range(h):
        s.append('.' + input() + '.')
    s.append('.' * (w+2))

    pos = ((-1, -1),
           (-1, 0),
           (-1, 1),
           (0, -1),
           (0, 1),
           (1, -1),
           (1, 0),
           (1, 1))
    ans = list()
    for i in range(h):
        tmp = ''
        for j in range(w):
            if s[i+1][j+1] == '#':
                tmp += '#'
            else:
                count = 0
                for p in pos:
                    if s[i + p[0] + 1][j + p[1] + 1] == '#':
                        count += 1
                tmp += str(count)
        ans.append(tmp)

    for a in ans:
        print(a)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(h):
    s_i=input()
    s.append(s_i)
for i in range(h):
    s_list=list(s[i])
    for j in range(w):
        if s[i][j]==".":
            ans=0
            for k in range(-1,2):
                if i+k<0 or i+k>h-1:
                    continue
                for l in range(-1,2):
                    if j+l<0 or j+l>w-1:
                        continue
                    if s[i+k][j+l]=="#":
                        ans+=1
            s_list[j]=str(ans)
    s_ans = "".join(s_list)
    print(s_ans)

Submission status: Accepted

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

result = [[] for _ in range(H)]
for y in range(H):
    Sy = S[y]
    resulty = result[y]
    for x in range(W):
        if Sy[x] == '#':
            resulty.append('#')
        else:
            t = 0
            for i in range(max(y - 1, 0), min(y + 2, H)):
                Si = S[i]
                for j in range(max(x - 1, 0), min(x + 2, W)):
                    if Si[j] == '#':
                        t += 1
            resulty.append(str(t))

for y in range(H):
    print(''.join(result[y]))

Submission status: Accepted

Solution code:
import numpy as np
h, w = map(int, input().split())
grid = np.array([list(input()) for _ in range(h)])
new_grid = np.zeros(grid.shape, dtype='object')

dx = [1, 0, -1, 0, 1, 1, -1, -1]
dy = [0, 1, 0, -1, 1, -1, 1, -1]
for x in range(h):
    for y in range(w):
        if grid[x, y] == '#':
            new_grid[x, y] = '#'
        else:
            cnt = 0
            for i in range(8):
                nx = x + dx[i]
                ny = y + dy[i]
                if nx >= 0 and nx < h and ny >= 0 and ny < w:
                    if grid[nx, ny] == '#':
                        cnt += 1
            new_grid[x, y] = str(cnt)
for i in range(h):
    print(''.join(new_grid[i]))

Submission status: Accepted

Solution code:
def aroundbom(i , q , h , w , sl):
    bomcount = 0
    dx = [1 , 1 , 0 , -1 , -1 , -1 , 0 , 1]
    dy = [0 , 1 , 1 , 1 , 0 , -1 , -1 , -1]

    for b in range(len(dx)):
        t = i + dx[b]
        y = q + dy[b]
        if t >= 0 and t < h and y >= 0 and y < w and sl[t][y] == "#":
            bomcount += 1

    return str(bomcount)

def minesweeper(h , w , sl):

    ans = []

    for i in range(h):
        count = []
        for q in range(w):
            if sl[i][q] == ".":
                count.append(aroundbom(i , q , h , w , sl))
            else:
                count.append("#")
        ans.append(count)

    return ans

def main():
    h , w = map(int , input().split())
    sl = [list(str(input())) for i in range(h)]
    ans = minesweeper(h , w , sl)

    for i in ans:
        print("".join(i))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(1, (h + 1)):
    for j in range(1, (w + 1)):
        if s[i][j] == "#":
            ans[i - 1][j - 1] = "#"
        else:
            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
            ans[i - 1][j - 1] = str(tmp)
for i in range(h):
    print("".join(ans[i]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
list = [[0 for i in range(w)] for j in range(h)]
for y in range(h):
    s = input()
    for x in range(w):
        if s[x] == "#": 
            list[y][x] = 1
for y in range(h):
    anslist = []
    for x in range(w):
        cnt = 0
        if list[y][x] == 1: anslist.append("#")
        else:
            if y >= 1: cnt += sum(list[y - 1][max(x - 1, 0):min(w, x + 2)])
            cnt += sum(list[y][max(x - 1, 0):min(w, x + 2)])
            if y <= h - 2: cnt += sum(list[y + 1][max(x - 1, 0):min(w, x + 2)])
            anslist.append(str(cnt))   
    print(''.join(anslist))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
for _ in range(h):
    a=list(input())
    s.append(a)
grid=[[0 for _ in range(w)] for _ in range(h)]
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 s[i][j]=="#":
            grid[i][j]="#"
            for t in range(8):
                ny=i+dy[t]
                nx=j+dx[t]
                if 0 <= ny <= h-1 and 0<= nx <= w-1 and grid[ny][nx] != "#":
                    grid[ny][nx]+=1
for row in grid:
    print(*row,sep="")

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            continue
        else:
            cnt = 0
            for k in range(8):
                i += dx[k]
                j += dy[k]
                if (0<=i and i<h) and (0<=j and j<w):
                    if s[i][j]=='#':
                        cnt += 1
                i -= dx[k]
                j -= dy[k]
            s[i][j] = str(cnt)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
lis=[["."]*(W+2)]
for i in range(H):
    lis+=[["."]+list(input())+["."]]
lis += [["."]*(W+2)]
for h in range(1, H+1):
    for w in range(1, W+1):
        x=0
        if lis[h][w] == ".":
            if lis[h+1][w-1] == "#":
                x += 1
            if lis[h+1][w] == "#":
                x += 1
            if lis[h+1][w+1] == "#":
                x += 1
            if lis[h][w-1] == "#":
                x += 1
            if lis[h][w+1] == "#":
                x += 1
            if lis[h-1][w-1] == "#":
                x += 1
            if lis[h-1][w] == "#":
                x += 1
            if lis[h-1][w+1] == "#":
                x += 1
            lis[h][w] = x
lis=lis[1:-1]
for i in lis:
    print(*i[1:-1], sep="")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
S=[]
S.append(["."] * (w + 2))
for i in range(h):
  S.append(["."] + list(input()) + ["."])
S.append(["."] * (w + 2))
 
 
for i in range(1, h+1):
  for j in range(1, w+1):
    counter = 0
    if S[i][j] == ".":
      if S[i-1][j-1] == "#":
        counter += 1
      if S[i-1][j] == "#":
        counter += 1
      if S[i-1][j+1] == "#":
        counter += 1
      if S[i][j-1] == "#":
        counter += 1
      if S[i][j+1] == "#":
        counter += 1
      if S[i+1][j-1] == "#":
        counter += 1
      if S[i+1][j] == "#":
        counter += 1
      if S[i+1][j+1] == "#":
        counter += 1
      S[i][j] = counter
 
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

H, W= list(map(int,input().split()))
S = numpy.array(["."]*(H+2)*(W+2),dtype=str).reshape(H+2,W+2)
for i in range(1,H+1):
	S[i,:]= list("."+input()+".")

X = numpy.array([0]*(H+2)*(W+2),dtype=int).reshape(H+2,W+2)
X[S=="#"]=1

rH=range(1,H+1)
rW=range(1,W+1)
for h in rH:
	for w in rW:
		if S[h,w]==".":
			S[h,w]=numpy.sum(X[h-1:h+2, w-1:w+2])
	print(*S[h,1:-1],sep="")

Submission status: Accepted

Solution code:
#Minesweeper
H,W = map(int,input().split())
S = ["."*(W+2)]
for i in range(H):
    S.append("."+input()+".")
S.append("."*(W+2))

for i in range(H):
    R = ""
    for j in range(W):
        n = 0
        if S[i+1][j+1] == "#":
            R = R + "#"
        else:
            if S[i][j] == "#":
                n += 1
            if S[i][j+1] == "#":
                n += 1
            if S[i][j+2] == "#":
                n += 1
            if S[i+1][j] == "#":
                n += 1
            if S[i+1][j+2] == "#":
                n += 1
            if S[i+2][j] == "#":
                n += 1
            if S[i+2][j+1] == "#":
                n += 1
            if S[i+2][j+2] == "#":
                n += 1
            R = R + str(n)
    print(R)

Submission status: Accepted

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

def count_bombs(y,x):
  y_c = y - 1
  x_c = x - 1
  r = 0
  for i in range(y_c, y_c + 3):
    for j in range(x_c, x_c + 3):
      if s[i][j] == '#':
        r += 1  
  return r

for i in range(1, h + 1):
  for j in range(1, w + 1):    
    if s[i][j] == '.':
      s[i][j] = count_bombs(i,j)
    print(s[i][j], end='')
  print('')

Submission status: Accepted

Solution code:
H, W = [int(x) for x in input().split()]
s_lst = [input() for i in range(H)]
for i in range(H):
    for j in range(W):
        if s_lst[i][j] == '.':
            cnt = 0
            for k in range(max(0, i-1), min(H, i + 2)):
                for l in range(max(0, j-1), min(W, j+2)):
                    if s_lst[k][l] == '#':
                        cnt += 1
            s_lst[i] = s_lst[i][:j] + str(cnt) + s_lst[i][j + 1:]
for i in range(H):
    print(s_lst[i])

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i+1][j+1] = '#'
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if ans[k+1][l+1] != '#':
                        ans[k+1][l+1] += 1
#print(ans)
del ans[0]
del ans[-1]
#print(ans)
for i in range(h):
    del ans[i][0]
    del ans[i][-1]
#print(ans)
for i in range(h):
    print(''.join(map(str,ans[i])))

Submission status: Accepted

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

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]

dxy = [(-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 S[i][j] != '#':
            continue
        for dx, dy in dxy:
            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
#print(answer)
for i in answer:
    print(*i,sep='')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
a = ["." for i in range(w+2)]
dot_map = [a]
for _ in range(h):
    l = list(input())
    l.insert(0, ".")
    l.append(".")
    dot_map.append(l)
dot_map.append(a)

for i in range(1, h+1):
    for j in range(1,w+1):
        if dot_map[i][j] == ".":
            counter = 0
            if dot_map[i-1][j-1] == "#":
                counter += 1
            if dot_map[i-1][j] == "#":
                counter += 1
            if dot_map[i-1][j+1] == "#":
                counter += 1
            if dot_map[i][j-1] == "#":
                counter += 1
            if dot_map[i][j+1] == "#":
                counter += 1
            if dot_map[i+1][j-1] == "#":
                counter += 1
            if dot_map[i+1][j] == "#":
                counter += 1
            if dot_map[i+1][j+1] == "#":
                counter += 1
            dot_map[i][j] = str(counter)

for i in range(1, h+1):
    ans = dot_map[i]
    print("".join(ans[1:w+1]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    M = [[-1, -1], [-1, 0], [-1, 1],
         [0, -1], [0, 0], [0, 1],
         [1, -1], [1, 0], [1, 1]]
    for h in range(H):
        for w in range(W):
            result = 0
            if S[h][w] == '#':
                continue
            for m in M:
                if (w + m[1] < 0
                    or h + m[0] < 0
                    or w + m[1] >= W
                        or h + m[0] >= H):
                    continue
                elif S[h + m[0]][w + m[1]] == '#':
                    result += 1
            S[h][w] = result
    for s in S:
        print(*s, sep='')


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [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]

for i in range(h):
    for j in range(w):
        cnt = 0
        if s[i][j] != "#":
            
            for k in range(8):
                nx = j + dx[k]
                ny = i + dy[k]
                if nx>=0 and ny>=0 and nx<w and ny<h:
                    if s[ny][nx] == "#":
                        cnt += 1
                
            s[i][j] = str(cnt)
for i in s:
    print("".join(i))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input() for _ in range(h)]
for i in range(h):
    l=""
    for j in range(w):
        if s[i][j]=="#":
            l+="#"
        else:
            l+=str(sum([t[max(0,j-1):min(w,j+2)].count("#") for t in s[max(0,i-1):min(h,i+2)]]))
    print(l)

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if hwl[i][j] == ".":
            cnt = 0
            if i != 0 and j != 0 and hwl[i-1][j-1] == "#":
                cnt += 1
            if i != 0 and hwl[i-1][j] == "#":
                cnt += 1
            if i != 0 and j < w-1 and hwl[i-1][j+1] == "#":
                cnt += 1
            if j != 0 and hwl[i][j-1] == "#":
                cnt += 1
            if j < w-1 and hwl[i][j+1] == "#":
                cnt += 1
            if i < h-1 and j != 0 and hwl[i+1][j-1] == "#":
                cnt += 1
            if i < h-1 and hwl[i+1][j] == "#":
                cnt += 1
            if i < h-1 and j < w-1 and hwl[i+1][j+1] == "#":
                cnt += 1
            hwl[i][j] = str(cnt)
    print("".join(hwl[i]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
lis = [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]

for i in range(h):
    for j in range(w):
        count = 0
        if lis[i][j] != "#":
            for k in range(8):
                nx = j + dx[k]
                ny = i + dy[k]
                if nx >= 0 and ny >= 0 and ny < h and nx < w:
                    if lis[ny][nx] == "#":
                        count += 1    
            lis[i][j] = str(count)
for ans in lis:
    print("".join(ans))

Submission status: Accepted

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


def checknum(x,y):
    count = 0
    for i in range(-1,2):
        for j in range(-1,2):
            nx = x+i
            ny = y+j
            if 0 <= nx < h and 0 <= ny < w and l[nx][ny] == "#":
                count += 1
    return count

for i in range(h):
    for j in range(w):
        if l[i][j] == ".":
            l[i][j] = checknum(i,j)
for i in range(h):
    for j in range(w):
        print(l[i][j],end="")
    print()

Submission status: Accepted

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

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
        for k in range(8):
            nx = j + dx[k]
            ny = i + dy[k]
            if nx >= 0 and ny >= 0 and ny < H and nx < W :
                if S[ny][nx] == "#":
                    S[i][j] += 1
for i in range(H):
    l = map(str,S[i])
    ans = "".join(l)
    print(ans)

Submission status: Accepted

Solution code:
def up(x,y):
    if(y-1<0):return 0
    if a[y-1][x]=="#":return 1
    else:return 0
def down(x,y):
    if(y+1>h-1):return 0
    if a[y+1][x]=="#":return 1
    else:return 0
def right(x,y):
    if(x+1>w-1):return 0
    if a[y][x+1]=="#":return 1
    else:return 0
def left(x,y):
    if(x-1<0):return 0
    if a[y][x-1]=="#":return 1
    else:return 0

def upleft(x,y):
    if(y-1<0):return 0
    if(x-1<0):return 0
    if a[y-1][x-1]=="#":return 1
    else:return 0
def downleft(x,y):
    if(x-1<0):return 0
    if(y+1>h-1):return 0
    if a[y+1][x-1]=="#":return 1
    else:return 0
def upright(x,y):
    if(y-1<0):return 0
    if(x+1>w-1):return 0
    if a[y-1][x+1]=="#":return 1
    else:return 0
def downright(x,y):
    if(y+1>h-1):return 0
    if(x+1>w-1):return 0
    if a[y+1][x+1]=="#":return 1
    else:return 0

h,w=map(int,input().split())
a=[[i for i in input()] for j in range(h)]

for y in range(h):
    for x in range(w):
        if a[y][x]!="#":
            p=up(x,y)+down(x,y)+left(x,y)+right(x,y)+upright(x,y)+downright(x,y)+upleft(x,y)+downleft(x,y)
            print(p,end="")
        else:print("#",end="")
    print()

Submission status: Accepted

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

ans = []


def calc(S,i,j):
    n=0
    if (i-1)>=0:
        if S[i-1][j]=="#":
            n += 1
        if (j-1)>=0:
            if S[i-1][j-1]=="#":
                n += 1
        if (j+1)<W:
            if S[i-1][j+1]=="#":
                n += 1
    if (j-1)>=0:
        if S[i][j-1]=="#":
            n += 1
        if (i+1)<H:
            if S[i+1][j-1]=="#":
                n += 1
    if (i+1)<H:
        if S[i+1][j] == "#":
            n += 1
        if (j+1)<W:
            if S[i+1][j+1]=="#":
                n += 1
    if (j+1)<W:
        if S[i][j+1]=="#":
            n += 1
    return n

for i in range(H):
    s = ""
    c = [-1]*W
    for j in range(W):
        if S[i][j]==".":
            n = calc(S,i,j)
            c[j]=n
    for k in range(W):
        if c[k]==-1:
            s = s+"#"
        else:
            s = s+str(c[k])
    ans.append(s)

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
ans = []
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
for x in range(H):
  tmp = ""
  for y in range(W):
    num = 0
    if S[x][y] == "#":
      tmp += "#"
      continue
    for k in range(8):
      cx = x + dx[k]
      cy = y + dy[k]
      if cx < 0 or cx >= H or cy < 0 or cy >= W:
        continue
      if S[cx][cy] == "#":
        num += 1
    tmp += str(num)
  ans.append(tmp)
  
for i in range(H):
	for j in range(W):
		print(ans[i][j],end = "")
	print("")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
Gd = [list(input()) for _ in range(H)]
ret = [['0']*W for _ in range(H)]
direc = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]
for h in range(H):
  for w in range(W):
    if Gd[h][w]=='#':
      ret[h][w]='#'
      for d in direc:
        hs, ws = h + d[0], w + d[1]
        if 0<=hs<H and 0<=ws<W:
          if Gd[hs][ws]!='#':
            ret[hs][ws]=str(1+int(ret[hs][ws]))
for _ in ret:
  print(''.join(_))

Submission status: Accepted

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

maze = [input() for i in range(H)]
ans = [[0] * W for i in range(H)]

def count_mine(y, x):
  count = 0
  for i, j in [1, 0], [-1, 0], [0, 1], [0, -1], [-1, 1], [1, 1], [-1, -1], [1, -1]:
    if -1 < i + y < H and -1 < j + x < W:
      if maze[i+y][j+x] == '#':
        count += 1
  return count

for i in range(H):
  for j in range(W):
    if maze[i][j] == '.':
      ans[i][j] = count_mine(i, j)
    else:
      ans[i][j] = '#'

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

Submission status: Accepted

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

Submission status: Accepted

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

if h==1 and w==1:
  s=slist[0]
  if s=="#":
    print(s)
  else:
    print(str(0))
  exit()
  
# 以下どちらかはnon 1

if h==1:
  s_new = ""
  # 1行w列のcase
  s=slist[0]
  for i in range(w):
    if i==0 and s[0]==".":
      if s[1]=="#":
        s_new += str(1)
      else:
        s_new += str(0)
    elif i==w-1 and s[i]==".":
      if s[i-1]=="#":
        s_new += str(1)
      else:
        s_new += str(0)
    elif s[i]==".":
      m=0
      if s[i-1]=="#":
        m+=1
      if s[i+1]=="#":
        m+=1
      s_new += str(m)
    else:
      s_new += "#"
  print(s_new)
  exit()

# 以下 h > 1

if w==1:
  for i in range(h):
    if i==0 and slist[0]==".":
      if slist[1]=="#":
        print(str(1))
      else:
        print(str(0))
    elif i==h-1 and slist[i]==".":
      if slist[i-1]=="#":
        print(str(1))
      else:
        print(str(0))
    elif slist[i]==".":
      m=0
      if slist[i-1]=="#":
        m+=1
      if slist[i+1]=="#":
        m+=1
      print(str(m))
    else:
      print("#")
  exit()

for i in range(h):
  s=slist[i]
  s_new = ""
  if i==0:
    t=slist[1]
    for j in range(w):
      if j==0 and s[0]==".":
        m=0
        if s[1]=="#":
          m+=1
        if t[0]=="#":
          m+=1
        if t[1]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[w-1]==".":
        m=0
        if s[w-2]=="#":
          m+=1
        if t[w-2]=="#":
          m+=1
        if t[w-1]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if s[j-1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)
  elif i==h-1:
    r = slist[i-1]
    for j in range(w):
      if j==0 and s[0]==".":
        m=0
        if s[1]=="#":
          m+=1
        if r[0]=="#":
          m+=1
        if r[1]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[w-1]==".":
        m=0
        if s[w-2]=="#":
          m+=1
        if r[w-2]=="#":
          m+=1
        if r[w-1]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if s[j-1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if r[j]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)
  else:
    r = slist[i-1]
    t = slist[i+1]
    for j in range(w):
      if j==0 and s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if s[j-1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if s[j-1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)

Submission status: Accepted

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

def grid(i,j):
    direc = [(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)] #8近傍
    res = 0
    for xy in direc:
        if 0 <= i+xy[1] < h and 0 <= j+xy[0] < w \
            and lst1[i+xy[1]][j+xy[0]] == "#":
            res += 1
    lst1[i][j] = str(res)

for i in range(h):
    for j in range(w):
        if lst1[i][j] ==".":
            grid(i,j)

for i in lst1:
    print("".join(i))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
B = [list(input()) for _ in range(N)]
list = [['#']*M] * N

MOVE = [(0, 1),(0, -1),(1, 0),(-1, 0),(1, 1),(1, -1),(-1, 1),(-1, -1)]

for h in range(N):
    for w in range(M):
        count = 0

        for x, y in MOVE:
            next_h = h + y
            next_w = w + x
            if not 0 <= next_h < N or not 0 <= next_w < M:
                continue
            if B[next_h][next_w] == '#':
                count += 1

        list[h][w] = str(count)
        if B[h][w] == '#':
            list[h][w] = '#'

    print(''.join(list[h]))