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:
# In progress
h, w = map(int, input().split())
# Setting
mine_set = set()
next_to_grid_list = [(1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),
                     (-1, 1), (0, 1), (1, 1)]


def add_number_for_dot(mine):
    for next_to_grid in next_to_grid_list:
        if is_constrained(mine[0] + next_to_grid[0], mine[1] + next_to_grid[1]):
            if type(grids[mine[0] + next_to_grid[0]][mine[1] + next_to_grid[1]]) is int:
                grids[mine[0] + next_to_grid[0]][mine[1] + next_to_grid[1]] += 1


def is_constrained(i, j):
    if 0 <= i < h and 0 <= j < w:
        return True
    else:
        return False


# Register grids information
grids = []
for i in range(h):
    grids.append(list(input()))
    for j in range(w):
        if grids[i][j] == "#":
            mine_set.add((i, j))
        else:
            grids[i][j] = 0

# add number to "."
for mine in mine_set:
    add_number_for_dot(mine)

# Show result
for i in range(h):
    s_i = [str(grid) if type(grid) is int else grid for grid in grids[i]]
    print("".join(s_i))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = []
s.append((". " * (w + 2)).split())
for i in range(h):
    b = "." + input() + "."
    s.append(list(b))
s.append((". " * (w + 2)).split())
for i in range(1,h + 1):
    for f in range(1, w + 1):
        if s[i][f] == "#":
            for x in range(-1, 2, 1):
                for y in range(-1, 2, 1):
                    if s[i+x][f+y] == ".":
                        s[i+x][f+y] = 1
                    elif isinstance(s[i+x][f+y], int):
                        s[i+x][f+y] += 1
for i in range(1, h + 1):
    ans = "".join(map(str, s[i][1:w+1])).replace(".","0")
    print(ans)

Submission status: Accepted

Solution code:

def countbomb(i,j):
    bomb = 0
    for y in range(i-1,i+2):
        for x in range(j-1,j+2):
            if a[y][x] == '#':
                bomb += 1
    return bomb      
            
h, w = list(map(int, input().split()))
a = [['w']*(w+2) for _ in range(h+2)]
for i in range(1,h+1):
    si = list(input())
    a[i][1:w+1] = si
#print(*a, sep='\n')
for i in range(1,h+1):
    for j in range(1,w+1):
        if a[i][j] != '.':
            continue
        cnt = countbomb(i,j)
        a[i][j] = cnt
for i in range(1,h+1):
    si = a[i][1:w+1]
    print(''.join(map(str,si)))

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    count = 0
    if area[i][j] == ".":
      for s in range(-1,2):
        for t in range(-1,2):
          if 0 <= i + s <h and 0 <= j+t < w:
            if area[i+s][j+t] == '#':
              count += 1
          else:
            continue
      print(count, end='')
    else:
      print("#", end='')
  print()

Submission status: Accepted

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

count = []
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    if 0 <= i+x < H and 0 <= j+y < W:
                        if 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())
a = [input() for _ in range(h)]
for i in range(h):
    for j in range(w):
        ans = 0
        if a[i][j] == ".":
            for s in range(-1, 2):
                for t in range(-1, 2):
                    if 0 <= i + s < h  and 0 <= j + t < w:
                            if a[i+s][j+t] == "#":
                                ans += 1
                    else:
                        continue
            print(ans, end="")
        else:
            print("#", end="")
    print()

Submission status: Accepted

Solution code:
import numpy as np
ans = []
H, W = map(int, input().split())
S = []
S.append('.'*(W+2))
for i in range(H):
    S.append('.'+input() + '.')
S.append('.'*(W+2))

for y in range(1, H+1):
    for x in range(1, W+1):
        if S[y][x] == '.':
            cnt = S[y][x-1].count('#')+S[y][x+1].count('#')+S[y-1][x-1:x+2].count('#')+S[y+1][x-1:x+2].count('#')
            ans.append(str(cnt))
        else:
            ans.append('#')
np_ans = np.array(ans)
re_ans = np.reshape(np_ans, (H, W))
for i in re_ans:
    print(''.join(i))

Submission status: Accepted

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

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

for h in range(H):
  for w in range(W):
    if S[h][w] == ".":
      a = 0
      for x in [-1, 0, 1]:
        for y in [-1, 0, 1]:
          if 0 <= h+x < H and 0 <= w+y < W:
            if S[h+x][w+y] == "#":
              a += 1
      S[h][w] = str(a)

for _ in range(H):
  print("".join(S[_]))

Submission status: Accepted

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

s = [[{".": 0, "#": 10}[c] for c in line] for line in s]

for h_i in range(h):
    for w_i in range(w):
        if 10 <= s[h_i][w_i]:
            for y in range(h_i-1, h_i+2):
                for x in range(w_i-1, w_i+2):
                    if 0 <= y < h and 0 <= x < w:
                        s[y][x] += 1

print("\n".join(["".join(["#" if 10 <= c else str(c) for c in line]) for line in s]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
dx=[1,1,1,0,0,-1,-1,-1]
dy=[-1,0,1,1,-1,-1,0,1]
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            cnt=0
            for k in range(8):
                x=i+dx[k]
                y=j+dy[k]
                if x>=0 and x<h and y>=0 and y<w and s[x][y]=="#":
                    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 = ["........................................................"]
for i in range(H):
    S.append(input())
for i in range(H+1):
    S[i] = '.' + S[i] + '.'
S.append("........................................................")
ans = [[0] * W for i 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:
            count = []
            for a in range(i-1, i+2):
                for b in range(j-1, j+2):
                    count.append(S[a][b])
            ans[i-1][j-1] = count.count('#')

for i in range(H):
    yoko = ''
    for j in range(W):
        yoko += str(ans[i][j])
    print(yoko)

Submission status: Accepted

Solution code:
hw = input()
hw = hw.split()
h = int(hw[0])  #hは行, 1 <= h
w = int(hw[1])  #wは列, w <= 50

board_input = []
board_output = []

for i in range(h):
    b = input()
    board_input.append(b)

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

for i in range(h):
    s = ""
    for j in range(w):
        if board_input[i][j] == '#':
            s += '#'
            continue

        num = 0
        for d in range(8):
            ni = i + dx[d]
            nj = j + dy[d]
            if ni > h - 1 or nj > w - 1 or ni < 0 or nj < 0:
                continue
            if board_input[ni][nj] == '#':
                num += 1
                
        s += str(num)
    board_output.append(s)


for i in range(h):
    print(board_output[i])

Submission status: Accepted

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


R = [(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 T[i][j] == ".":
            rec = 0
            for dx, dy in R:
                nx, ny = i + dx, j + dy
                if 0 <= nx < H and 0 <= ny < W and T[nx][ny] == "#":
                    rec += 1

            T[i][j] = str(rec)

for i in range(H):
    print("".join(T[i]))

Submission status: Accepted

Solution code:
def fa():  # final answer
    global ans
    print(ans)


def getN():
    return int(input())


def getNM():
    return map(int, input().split())


def getList():
    return list(map(int, input().split()))

def check_mine(i,j,place):
    if place[i][j] == "#":
        return "#"

    num = 0
    for x in range(i-1, i+2):
        for y in range(j-1, j+2):
            try:
                if place[x][y] == "#":
                    if x >= 0 and y >= 0:
                        num += 1
            except:
                pass

    return str(num)

h, w = getList()

place = []
for i in range(h):
    place.append(input())


for i in range(h):
    ans = []
    for j in range(w):
        ans.append(check_mine(i,j,place))
    print("".join(ans))

Submission status: Accepted

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

ans = ["" for i in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            ans[i] += "#"
        else:
            count = 0
            if i - 1 >= 0:
                if j - 1 >= 0:
                    if s[i-1][j-1] == "#":
                        count += 1
                if s[i-1][j] == "#":
                    count += 1
                if j + 1 <= w-1:
                    if s[i-1][j+1] == "#":
                        count += 1
            if j - 1 >= 0:
                if s[i][j-1] == "#":
                    count += 1
            if j + 1 <= w-1:
                if s[i][j+1] == "#":
                    count += 1
            if i + 1 <= h-1:
                if j - 1 >= 0:
                    if s[i+1][j-1] == "#":
                        count += 1
                if s[i+1][j] == "#":
                    count += 1
                if j + 1 <= w-1:
                    if s[i+1][j+1] == "#":
                        count += 1
            ans[i] += str(count)

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

h,w = li()
field = [["." for _ in range(w+2)]]
for _ in range(h):
    field.append(["."] + lc() + ["."])
field.append(["." for _ in range(w+2)])

dxs = [0, 1, 1, 1, 0, -1, -1, -1]
dys = [-1, -1, 0, 1, 1, 1, 0, -1]

for hi in range(1,h+1):
    for wj in range(1,w+1):
        if field[hi][wj] == "#":
            continue
        ans = 0
        for dy,dx in zip(dys,dxs):
            if field[hi+dy][wj+dx] == "#":
                ans += 1
        
        field[hi][wj] = str(ans)
        
for hi in range(1,h+1):
    print("".join(field[hi][1:-1]))

Submission status: Accepted

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

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

ans = [''] * H
for y in range(H):
    for x in range(W):
        if S[y][x] == '#':
            ans[y] += '#'
        else:
            b = 0
            for dx,dy in dxy:
                nx,ny = x+dx, y+dy
                if not 0 <= nx < W: continue
                if not 0 <= ny < H: continue
                if S[ny][nx] == '#':
                    b += 1
            ans[y] += str(b)

print(*ans,sep='\n')

Submission status: Accepted

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


for h in range(H):
    for w in range(W):
        cnt = 0
        if S[h][w]==".":
            if w > 0:
                cnt += int(S[h][w-1] == "#")
            if w < W-1:
                cnt += int(S[h][w+1] == "#")
                
            if h > 0:
                cnt += int(S[h-1][w] == "#")
                if w > 0:
                    cnt += int(S[h-1][w-1] == "#")
                if w < W-1:
                    cnt += int(S[h-1][w+1] == "#")
                    
            if h < H-1:
                cnt += int(S[h+1][w] == "#")
                if w > 0:
                    cnt += int(S[h+1][w-1] == "#")
                if w < W-1:
                    cnt += int(S[h+1][w+1] == "#")
            
            S[h][w] = str(cnt)

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

Submission status: Accepted

Solution code:
hw_list = list(map(int, input().split()))
h = hw_list[0]
w = hw_list[1]
row_list = ['.'*(w+2)]

def mine_check(area, x, y):
    mine = 0
    if area[y-1][x-1] == '#':
        mine += 1
    if area[y-1][x] == '#':
        mine += 1
    if area[y-1][x+1] == '#':
        mine += 1
    if area[y][x-1] == '#':
        mine += 1
    if area[y][x+1] == '#':
        mine += 1
    if area[y+1][x-1] == '#':
        mine += 1
    if area[y+1][x] == '#':
        mine += 1
    if area[y+1][x+1] == '#':
        mine += 1
    return mine

for i in range(h):
    row_list.append('.'+ input()+ '.')
row_list.append('.'*(w+2))

for y in range(1, h+1):
    temp_list = []
    for x in range(1, w+1):
        if row_list[y][x] == '#':
            temp_list.append('#')
        else:
            temp_list.append(str(mine_check(row_list, x, y)))
    print(''.join(temp_list))

Submission status: Accepted

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

a=[]
a.append(["_"]*(w+2))
for i in range(1,h+1):
  a.append(["_"] + list(input()) + ["_"])
a.append(["_"]*(w+2))

b=a
for i in range(1,h+1):
  for j in range(1,w+1):
    if a[i][j] != "#":
      b[i][j] = str((a[i-1][j-1]=="#")+(a[i-1][j]=="#")+(a[i-1][j+1]=="#")+(a[i][j-1]=="#")+(a[i][j+1]=="#")+(a[i+1][j-1]=="#")+(a[i+1][j]=="#")+(a[i+1][j+1]=="#"))

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

Submission status: Accepted

Solution code:
import numpy as np
h,w=map(int, input().split())
a=[list(input()) for i in range(h)]
a=np.array(a)
for i in range(h):
    for j in range(w):
        if a[i][j]=='.':
            b=a[max(i-1,0):min(i+2,h),max(j-1,0):min(j+2,w)].flatten()
            a[i][j]=sum(b=='#')
for i in a:
    print(''.join(i))

Submission status: Accepted

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

index=0
liForli=[]
while index < h:
    li = list(input())
    liForli.append(li)
    index+=1

index2=0
while index2 < len(liForli):
    index3=0
    while index3 < w:
        if liForli[index2][index3]=="#":
            pass
        else:
            liForli[index2][index3]=0
        index3+=1
    index2+=1

index4=0
while index4 < len(liForli):
    index5=0
    while index5 < w:
        if liForli[index4][index5]=="#":
            if index4+1 < h:
                if index5+1 < w and liForli[index4+1][index5+1]!="#":
                    liForli[index4+1][index5+1]+=1
                if index5-1 > -1 and liForli[index4+1][index5-1]!="#":
                    liForli[index4+1][index5-1]+=1
                if liForli[index4+1][index5]!="#":
                    liForli[index4+1][index5]+=1
            if index4-1 > -1:
                if index5+1 < w and liForli[index4-1][index5+1]!="#":
                    liForli[index4-1][index5+1]+=1
                if index5-1 > -1 and liForli[index4-1][index5-1]!="#":
                    liForli[index4-1][index5-1]+=1
                if liForli[index4-1][index5]!="#":
                    liForli[index4-1][index5]+=1
            if index5+1 < w and liForli[index4][index5+1]!="#":
                liForli[index4][index5+1]+=1
            if index5-1 > -1 and liForli[index4][index5-1]!="#":
                liForli[index4][index5-1]+=1
        index5+=1
    index4+=1

for li in liForli:
    print("".join(list(map(str, li))))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
list_prob = []
list_prob.append((list("."*(w+2))))
for i in range(0,h):
    list_prob.append(list("." + str(input()) + "."))
list_prob.append(list("."*(w+2)))
def decis_num(i,j):
    decis_str = \
    list_prob[i-1][j-1] + list_prob[i-1][j] + list_prob[i-1][j+1] + \
    list_prob[i][j-1] + list_prob[i][j+1] + \
    list_prob[i+1][j-1] + list_prob[i+1][j] + list_prob[i+1][j+1]
    return decis_str.count("#")

for i in range(1,h+1):
    for j in range(1,w+1):
        if list_prob[i][j] == "#":
            continue
        else:
            list_prob[i][j] = str(decis_num(i,j))
list_ans = []
for row in list_prob:
    list_ans.append(row[1:w+1])
del list_ans[0]
del list_ans[-1]
for row in list_ans:
    print("".join(row))

Submission status: Accepted

Solution code:

I = [-1,0,1]
J = [-1,0,1]

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

for i in range(len(S)):
    new_line = ''
    for j in range(len(S[i])):
        if S[i][j]=='.':
            count = 0
            for x in I:
                for y in J:
                    if 0 <= x+i < len(S) and 0 <= y+j < len(S[i]):
                        if S[x+i][y+j] == '#':
                            count = count + 1
            new_line = new_line + str(count)
        else:
            new_line = new_line + '#'

    print(new_line)

Submission status: Accepted

Solution code:
il = lambda: list(map(int, input().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

h, w = il()
s = ils(h, "list(input())")
for i in range(h):
    for j in range(w):
        cnt = 0
        if s[i][j] == "#":
            continue
        for k, l in zip([-1, -1, -1, 0, 1, 1, 1, 0], [-1, 0, 1, 1, 1, 0, -1, -1]):
            if 0 <= i+k < h and 0 <= j+l < w:
                if s[i + k][j + l] == "#":
                    cnt += 1
        s[i][j] = cnt
for i in s:
    for j in i:
        print(j, end="")
    print("")

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            S[i][j] = str(
                sum(S[x][y] == '#'
                    for x in range(max(0, i - 1), min(H, i + 2))
                    for y in range(max(0, j - 1), min(W, j + 2))
                )
            )
for s in S:
    print(''.join(s))

Submission status: Accepted

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

def hfunc1(i,j):
    if ss[i][j] != "#":
        ssc[i][j] += 1

def hfunc2(i,j):
    xs = -1
    xe = 2
    ys = -1
    ye = 2

    if i == 0:
        xs = 0
    if i == h-1:
        xe = 1
    if j == 0:
        ys = 0
    if j == w-1:
        ye = 1 

    for x in range(xs,xe):
        for y in range(ys,ye):
                hfunc1(i+x,j+y)
    
for i in range(h):
    for j in range(w):
        if ss[i][j] == "#":
            ssc[i][j] = "#"
            hfunc2(i,j)

ans = ""
for x in ssc:
    for y in x:
        ans += str(y)
    ans += "\n"
print(ans)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[list(input()) for i in range(h)]
for x in range(h):
 for y in range(w):
  if l[x][y]=='.':
   c=0
   if x>0 and y>0:
    if l[x-1][y-1]=='#':c+=1
   if x>0:
    if l[x-1][y]=='#':c+=1
   if x>0 and y+1<w:
    if l[x-1][y+1]=='#':c+=1
   if y>0:
    if l[x][y-1]=='#':c+=1
   if y+1<w:
    if l[x][y+1]=='#':c+=1
   if x+1<h and y>0:
    if l[x+1][y-1]=='#':c+=1
   if x+1<h:
    if l[x+1][y]=='#':c+=1
   if x+1<h and y+1<w:
    if l[x+1][y+1]=='#':c+=1
   l[x][y]=str(c)
for j in range(h):
  l[j]=''.join(l[j])
  print(l[j])

Submission status: Accepted

Solution code:
import numpy as np
H, W = map(int, input().split())

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


ans = np.zeros((H+2,W+2),dtype = int)

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

for h in range(H):
    for w,s in enumerate(S[h]):
        if s == '#':
            x = w + 1
            y = h + 1
            for del_x in dx:
                for del_y in dy:
                    if not (del_x == 0 and del_y == 0):
                        ans[y+del_y][x+del_x] += 1
for h in range(H):
    ans2 = ''
    for w in range(W):
        if S[h][w] == '#':
            ans2 += '#'
        else:
            ans2 += str(ans[h+1][w+1])

    print(ans2)

Submission status: Accepted

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

S = ["."*(H+2)] + ["." + input() + "." for i in range(H)] + ["."*(H+2)]

for y in range(H):
    for x in range(W):
        c = S[y+1][x+1]
        if c != "#":
            c = str(sum([S[i][x:x+3].count("#") for i in range(y,y+3)]))
        print(c, sep="", end="")
    print()

Submission status: Accepted

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

matrix = []
for part in range(H):
    matrix.append(list(input()))

ans_matrix = []
for h in range(H):
    line = []
    for w in range(W):
        line.append(0)
    ans_matrix.append(line)

for h in range(H):
    for w in range(W):

        if matrix[h][w] == "#":

            for dh in range(-1, 1 + 1):
                for dw in range(-1, 1 + 1):
                    # mat は触ろうとしてる場所
                    mat_h = h + dh
                    mat_w = w + dw
                    # 危ないやつは無視
                    if mat_h < 0 or H <= mat_h or mat_w < 0 or W <= mat_w:
                        continue

                    ans_matrix[mat_h][mat_w] += 1

for h in range(H):
    for w in range(W):
        # 爆弾はそのまま表示
        if matrix[h][w] == "#":
            print("#", end="")
        else:
            print(ans_matrix[h][w], end="")
    
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
HW = []
for i in range(H):
    HW.append(list(input()))
cnt = [[0 for i in range(W)] for j in range(H)]
for i in range(H):
    for j in range(W):
        if HW[i][j] == "#":
            if i != 0 and j != 0:
                cnt[i - 1][j - 1] += 1
            if i != 0:
                cnt[i - 1][j] += 1
            if i != 0 and j != W - 1:
                cnt[i - 1][j + 1] += 1
            if j != 0:
                cnt[i][j - 1] += 1
            if j != W - 1:
                cnt[i][j + 1] += 1
            if i != H - 1 and j != 0:
                cnt[i + 1][j - 1] += 1
            if i != H - 1:
                cnt[i + 1][j] += 1
            if i != H - 1 and j != W - 1:
                cnt[i + 1][j + 1] += 1
for i in range(H):
    for j in range(W):
        if HW[i][j] == "#":
            print("#", end = "")
        else:
            print(str(cnt[i][j]), end = "")
    print()
print()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s,t=[list(input()) for i in range(h)],[[0 for j in range(w+2)] for i in range(h+2)]
for i in range(h):
	for j in range(w):
		if s[i][j]=='.':
			continue
		for p in range(9):
			t[i+p//3][j+p%3]+=1
for i in range(h):
	for j in range(w):
		if s[i][j]=='.':
			s[i][j]=str(t[i+1][j+1])
	print("".join(s[i]))

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
s = [input() for _ in range(h)]
s_large = [''.join(['.']*(w+2))]\
          + ['.'+s[i]+'.' for i in range(h)]\
          + [''.join(['.']*(w+2))]
# for i in range(len(s)):
#     print(s[i])

for i in range(h):
    for j in range(w):
        n = 0
        if s[i][j] == '#':
            continue
        for ii in [i, i+1, i+2]:
            for jj in [j, j+1, j+2]:
                if s_large[ii][jj] == '#':
                    n += 1
        s[i] = s[i][:j]+str(n)+s[i][j+1:]

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

Submission status: Accepted

Solution code:
H,W = (int(i) for i in input().split())

#囲い付きで入力　上下左右斜めを確認するとき用
firstline = "*"*(W+2)
bomb = [firstline]
for i in range(H):
    line = "*" + input() + "*"
    bomb.append(line)
lastline = "*"*(W+2)
bomb.append(lastline)

#答えの配列
answer = [[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        if bomb[i+1][j+1] == "#":
            answer[i][j] = "#"
        else:
            if bomb[i+1+1][j+1+1] == "#":
                answer[i][j] += 1
            if bomb[i+1+1][j+1+0] == "#":
                answer[i][j] += 1
            if bomb[i+1+1][j+1-1] == "#":
                answer[i][j] += 1
            if bomb[i+1+0][j+1+1] == "#":
                answer[i][j] += 1
            if bomb[i+1+0][j+1-1] == "#":
                answer[i][j] += 1
            if bomb[i+1-1][j+1+1] == "#":
                answer[i][j] += 1
            if bomb[i+1-1][j+1+0] == "#":
                answer[i][j] += 1
            if bomb[i+1-1][j+1-1] == "#":
                answer[i][j] += 1

#print(answer)
for i in range(H):
    tmp = ""
    for j in range(W):
        tmp += str(answer[i][j])
    print(tmp)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    H, W = map(int, input().split())
    f = []
    for _ in range(H):
        f.append(input())

    ans = [[0] * W for _ in range(H)]
    for y in range(H):
        for x in range(W):
            if f[y][x] == "#":
                ans[y][x] = "#"
                continue
            num = 0
            for i in range(8):
                ny, nx = dy8[i] + y, dx8[i] + x
                if inside(ny, nx, H, W) and f[ny][nx] == "#":
                    num += 1

            ans[y][x] = num

    for line in ans:
        print(*line, sep="")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy
H, W=map(int, input().split())
S=[['.' for _ in range(W+2)]]+[['.']+list(input())+['.'] for _ in range(H)]+[['.' for _ in range(W+2)]]
S0=copy.deepcopy(S)
for i in range(1, H+1):
    for j in range(1, W+1):
        ct=0
        if S[i][j]=='.':
            for a in range(-1,2):
                for b in range(-1,2):
                    if S[i+a][j+b]=='#':
                        ct+=1
            S0[i][j]=str(ct)

S1=[]
for i in range(H+2):
    S1.append(''.join(S0[i]))

for i in range(1,H+1):
    print(S1[i][1:W+1])

Submission status: Accepted

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


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

for i in s[1:-1]:
    print(''.join(i[1:-1]))

Submission status: Accepted

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

for r in range(h):
    for c in range(w):
        if s[r][c] == '#':
            continue
        counter = 0
        for y in range(3):
            for x in range(3):
                nr = r + y - 1
                nc = c + x - 1
                if 0 <= nr < h and 0 <= nc < w and s[nr][nc] == '#':
                    counter += 1
        res[r][c] = str(counter)

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

Submission status: Accepted

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

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

for v in s[1:-1]:
    print(''.join(v[1:-1]))

Submission status: Accepted

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

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] == '#':
      for k in range(8):
        px = i+dx[k]
        py = j+dy[k]
        if 0 <= px and px < H and 0 <= py and py < W:
          if S[px][py] == '#':
            pass
          elif S[px][py] == '.':
            S[px][py] = '1'
          else:
            S[px][py] = str(int(S[px][py])+1)
    elif S[i][j] == '.':
      S[i][j] = '0'

for i in range(H):
  print("".join(S[i]))

Submission status: Accepted

Solution code:
def solve():
    H, W = map(int, input().split())
    a = [list("."+input()+".") for _ in [0]*H]
    a.insert(0, list("."*(W+2)))
    a.append(list("."*(W+2)))
    result = []
 
    for y in range(1, H+1):
        for x in range(1, W+1):
            if a[y][x] == "#":
                result.append("#")
            else:
                result.append(str((a[y-1][x-1:x+2]+a[y][x-1:x+2]+a[y+1][x-1:x+2]).count("#")))
        result.append("\n")
 
    print("".join(result))
 
 
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

zero1 = "0" * (w + 2)
zero2 = "0" * (w + 2)

table = [zero1]
for _ in range(h):
    table.append("0" + str(input()) + "0")

table.append(zero2)

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

outputs = []
for r in range(1, h+1):
    output = ""
    for c in range(1, w+1):
        if table[r][c] == ".":

            ll = table[r-1][c-1:c+2] + table[r][c-1:c+2] + table[r+1][c-1:c+2]
            count = 0
            for e in ll:
                if e == "#":
                    count += 1

        else:
            count = "#"

        output = output + str(count)

    outputs.append(output)

for op in outputs:
    print(op)