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:
# coding: utf-8

H, W = map(int, input().split(" "))

S = []
for i in range(H):
    tmp = input()
    S.append(list(tmp))


def eight_math_index(i, j, H, W):
    result = [(i-1, j-1), (i-1, j), (i-1, j+1),
              (i, j - 1), (i, j), (i, j + 1),
              (i + 1, j - 1), (i + 1, j), (i + 1, j + 1)]
    return list(filter(lambda x: (0 <= x[0] <= (H-1)) and (0 <= x[1] <= (W-1)), result))


for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            eight = eight_math_index(i, j, H, W)
            S[i][j] = 0
            for (k, l) in eight:
                if S[k][l] == "#":
                    S[i][j] += 1

for l in S:
    print("".join(list(map(str, l))))

Submission status: Accepted

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

S = [list(input()) for _ in range(H)]
dydx = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]


def check(h, w):
  count = 0
  for dy, dx in dydx:
    if 0 <= h + dy < H and 0 <= w + dx < W and S[h+dy][w+dx] == '#':
      count += 1
  return count

for h in range(H):
  for w in range(W):
    if S[h][w] == '.':
      S[h][w] = str(check(h, w))
  res = "".join(S[h])
  print(res)

Submission status: Accepted

Solution code:
h,w=map(int, input().split())
mat = []
mat.append(["."]*(w+2))
for i in range(h):
  mat.append(["."]+list(input())+["."])
mat.append(["."]*(w+2))
for y in range(1, h+1):
  for x in range(1, w+1):
    if mat[y][x] != "#":
      c = 0
      if mat[y-1][x-1] == "#":
        c += 1
      if mat[y-1][x] == "#":
        c += 1       
      if mat[y-1][x+1] == "#":
        c += 1      
      if mat[y][x-1] == "#":
        c += 1
      if mat[y][x+1] == "#":
        c += 1
      if mat[y+1][x-1] == "#":
        c += 1        
      if mat[y+1][x] == "#":
        c += 1        
      if mat[y+1][x+1] == "#":
        c += 1
      mat[y][x] = str(c)

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

Submission status: Accepted

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

# print(s)

d = [[-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0]]



for i in range(h):
    si = ""
    for j in range(w):
        c = 0
        if s[i][j] == ".":
            for di in d:
                if i+di[0] >= 0 and i+di[0] < h and j+di[1] >= 0 and j+di[1] < w:
                    if s[i+di[0]][j+di[1]] == "#":
                        c += 1
            si += str(c)
        else:
            si += s[i][j]
    s[i] = si

for si in s:
    print(si)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
r,c = map(int,input().split())

map = []
for i in range(r):
    map.append(input())

def kinbou(y,x):
    sum = 0
    if y-1 >= 0:
        if (map[y-1][x] == '#'):
            sum += 1

    if x-1 >= 0:
        if (map[y][x-1] == '#'):
            sum += 1

    if x+1 < c:
        if (map[y][x+1] == '#'):
            sum += 1

    if y+1 < r:
        if (map[y+1][x] == '#'):
            sum += 1

    if y-1 >= 0 and x-1 >= 0:
        if (map[y-1][x-1] == '#'):
            sum += 1

    if y-1 >= 0 and x+1 < c:
        if (map[y-1][x+1] == '#'):
            sum += 1

    if y+1 < r and x-1 >= 0:
        if (map[y+1][x-1] == '#'):
            sum += 1

    if y+1 < r and x+1 < c:
        if (map[y+1][x+1] == '#'):
            sum += 1
    return sum

map2 = [['0']*c for i in range(r)]

for i in range(r):
    for j in range(c):
        if map[i][j] == '.':
            map2[i][j] = str(kinbou(i,j))
        else:
            map2[i][j] = '#'
    print(''.join(map2[i]))

Submission status: Accepted

Solution code:
H, W = list(map(int,input().split()))
S = []
c = []
for h in range(H):
    S.append(input())
    c.append([0 for _ in range(W)])
for y in range(H):
    for x in range(W):
        if S[y][x]=="#":
            continue
        for cy in range(max(0,y-1),min(H,y+2)):
            for cx in range(max(0,x-1),min(W,x+2)):
                c[y][x] += 1 if ( (cy!=y or cx!=x) and S[cy][cx]=="#" ) else 0
for y in range(H):
    for x in range(W):
        print("#" if S[y][x]=="#" else c[y][x], end="")
    print("")

Submission status: Accepted

Solution code:
a = []
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
h, w = map(int, input().split())
ans = [["#"] * w for _ in range(h)]

for _ in range(h):
    a.append(input())

for k in range(h):
    for j in range(w):
        if a[k][j] == ".":
            count = 0
            for i in range(8):
                nx = j + dx[i]
                ny = k + dy[i]

                if nx < 0 or ny < 0 or nx >= w or ny >= h:
                    continue
                if a[ny][nx] == "#":
                    count += 1
            ans[k][j] = count

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

Submission status: Accepted

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

sd = {}
for i in range(0, h + 2):
    for j in range(0, w + 2):
        if (i == 0) or (i == h + 1) or (j == 0) or (j == w + 1):
            sd[(i, j)] = 0
        elif s[i - 1][j - 1] == '.':
            sd[(i, j)] = 0
        else:
            sd[(i, j)] = 1

for i in range(1, h + 1):
    line = ""
    for j in range(1, w + 1):
        if s[i - 1][j - 1] == '.':
            num = 0
            num += sd[(i - 1, j - 1)]
            num += sd[(i - 1, j)]
            num += sd[(i - 1, j + 1)]
            num += sd[(i, j - 1)]
            num += sd[(i, j)]
            num += sd[(i, j + 1)]
            num += sd[(i + 1, j - 1)]
            num += sd[(i + 1, j)]
            num += sd[(i + 1, j + 1)]
            line += str(num)
        else:
            line += s[i - 1][j - 1]
    print(line)

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if s_l[h][w] == ".":
            cnt = 0
            if h != 0:
                if w != 0:
                    if s_l[h - 1][w - 1] == "#":
                        cnt += 1
                if w != W - 1:
                    if s_l[h - 1][w + 1] == "#":
                        cnt += 1
                if s_l[h - 1][w] == "#":
                    cnt += 1
            if h != H - 1:
                if w != 0:
                    if s_l[h + 1][w - 1] == "#":
                        cnt += 1
                if w != W - 1:
                    if s_l[h + 1][w + 1] == "#":
                        cnt += 1
                if s_l[h + 1][w] == "#":
                    cnt += 1
            if w != 0:
                if s_l[h][w - 1] == "#":
                    cnt += 1
            if w != W - 1:
                if s_l[h][w + 1] == "#":
                    cnt += 1
            s_l[h][w] = str(cnt)

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

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] == '#':
            continue
        cnt = 0
        for k in range(-1, 2):
            for l in range(-1, 2):
                if k == 0 and l == 0:
                    continue
                if 0 <= i + k < h and 0 <= j + l < w and s[i+k][j+l] == '#':
                    cnt += 1
        s[i][j] = cnt

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

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
grid = [list(input()) for _ 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 grid[i][j] == ".":
            cnt = 0
            for k in range(8):
                nx = j + dx[k]
                ny = i + dy[k]            
                if (nx < 0) or (w-1 < nx) or (ny < 0) or (h-1 < ny):
                    continue
                elif grid[ny][nx] == "#":
                    cnt += 1
                else:
                    pass
            grid[i][j] = cnt
for i in range(h):
  ans = ''.join(map(str,grid[i]))
  print(ans)

Submission status: Accepted

Solution code:

H,W = map(int,input().split())

lst = [str(input()) for _ in range(H)]

#print(lst)

def check(y,x):
    if x < 0 or x >= W or y < 0 or y >= H:
        return 0
    elif lst[y][x] == '#':
        return 1
    else:
        return 0


fin = [[] for _ in range(H)]
dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]
for j in range(H):
    for i in range(W):
    
        sol = 0
        if lst[j][i] == '#':
            sol = '#'
        else:
            for dy,dx in dxy:
                sol += check(j+dy,i+dx)
            
        fin[j].append(str(sol))    
    print(*fin[j],sep = '')

Submission status: Accepted

Solution code:

H,W = map(int,input().split())

lst = [str(input()) for _ in range(H)]

#print(lst)

def check(y,x):
    if x < 0 or x >= W or y < 0 or y >= H:
        return 0
    elif lst[y][x] == '#':
        return 1
    else:
        return 0


fin = [[] for _ in range(H)]
for j in range(H):
    for i in range(W):
    
        sol = 0
        if lst[j][i] == '#':
            sol = '#'
        else:
            
            sol += check(j-1,i)
            sol += check(j+1,i)
            sol += check(j,i-1)
            sol += check(j,i+1)
            sol += check(j-1,i-1)
            sol += check(j+1,i+1)
            sol += check(j+1,i-1)
            sol += check(j-1,i+1)
        fin[j].append(str(sol))    
    print(*fin[j],sep = '')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for _ in range(h)]
a=[-1,0,1]
b=[-1,0,1]
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            s[i][j]=0
            for c in a:
                for d in b:
                    if i+c<0 or j+d<0 or i+c>h-1 or j+d>w-1 or (a==0 and b==0) or s[i+c][j+d]!="#":
                        continue
                    else:
                        s[i][j]+=1
for i in range(h):
    print(*s[i][:],sep="")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        tmp = 0
        for di in range(max(i-1, 0), min(i+2, H)):
            for dj in range(max(j-1, 0), min(j+2, W)):
                tmp += int(S[di][dj] == '#')
        S[i][j] = str(tmp)

for si in S:
    print(''.join(si))

Submission status: Accepted

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

H,W = list(map(int,input().split()))
cell = []
for i in range(H):
    s = list(input().rstrip())
    cell.append(s)
for ih in range(H):
    for iw in range(W):
        if cell[ih][iw] == '.':
            ans =0
            for iih in range(max(0,ih-1),min(ih+2,H)):
                for iiw in range(max(0,iw-1),min(iw+2,W)):
                    if cell[iih][iiw]=='#':
                        ans+=1
            cell[ih][iw] = ans
for i in range(H):
    for j in range(W):
        print(cell[i][j],end='')
    print('')

Submission status: Accepted

Solution code:
# B - Minesweeper
import copy
H, W = map(int, input().split())
S_src = []
for i in range(H):
    S_src.append(list(input()))

S_tgt = copy.deepcopy(S_src)
for i in range(H):
    for j in range(W):
        if S_src[i][j] == '#':
            continue

        count = 0
        for m in range(-1, 2):
            if not (0 <= i + m < H):
                continue
            for n in range(-1, 2):
                if not (0 <= j + n < W):
                    continue
                if S_src[i+m][j+n] == '#':
                    count += 1
        S_tgt[i][j] = str(count)

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

Submission status: Accepted

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

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

for h in range(1,1+H):
  print(S[h][1:1+W])

Submission status: Accepted

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


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

ans = S

for i,row in enumerate(S):
    for j,mas in enumerate(row):
        if mas == '#':
            pass
        else:
            count = 0
            for i2 in [-1,0,1]:
                for j2 in [-1,0,1]:
                    if (i+i2<0) | (j+j2<0) | (i+i2>=H) |(j+j2>=W):
                        pass
                    else:
                        if S[i+i2][j+j2]=='#':
                            count +=1
            ans[i] = ans[i][:j] + str(count) +ans[i][j+1:]
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [list(input()) for n in range(H)]
A = [["."]*(W+2)]*(H+2)

S.insert(0,(["."]*W))
S.append(["."]*W)

for h in range(H+2):
  S[h].insert(0,".")
  S[h].append(".")

for h in range(1,1+H):
  for w in range(1,1+W):
    ans=0
    if S[h][w]==".":
      ans+=S[h-1][w-1:w+2].count("#")
      ans+=S[h][w-1:w+2].count("#")
      ans+=S[h+1][w-1:w+2].count("#")
      S[h][w]=ans

for h in range(1,1+H):
  print(*S[h][1:-1],sep="")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

lis = []
tmp =["."]*(w+2)
lis.append(tmp)

for i in range(h):
    a = ["."]+list(input())+["."]
    lis.append(a)

lis.append(tmp)

for i in range(1,h+1):
    for j in range(1,w+1):
        if lis[i][j] == "#":
            continue
        
        else:
            n = 0
            if lis[i-1][j-1] == "#":
                n += 1
            if lis[i-1][j] == "#":
                n += 1
            if lis[i][j-1] == "#":
                n += 1
            if lis[i][j+1] == "#":
                n += 1
            if lis[i+1][j-1] == "#":
                n += 1
            if lis[i+1][j] == "#":
                n += 1
            if lis[i+1][j+1] == "#":
                n += 1
            if lis[i-1][j+1] == "#":
                n += 1
            
            lis[i][j] = n

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

Submission status: Accepted

Solution code:
def CountBomb(box, line, row):
    count = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            if line+i < 0 or line+i >= len(box) or row+j < 0 or row+j >= len(box[0]):
                continue
            else:
                cell = box[line+i][row+j]
                if cell == "#":
                    count += 1
    return count

def resolve():
    h, w = map(int, input().split())
    box = list()
    for i in range(h):
        a = input()
        b = list()
        for i in range(len(a)):
            b.append(a[i])
        box.append(b)
    
    for line in range(h):
        for row in range(w):
            if box[line][row] == ".":
                box[line][row] = CountBomb(box, line, row)
    
    for line in range(h):
        for row in range(w):
            a = box[line][row]
            print(a, end="")
        print()
resolve()

Submission status: Accepted

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

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

for i in range(h):
  s = input()
  for j in range(w):
    if s[j] == '#':
      dp[i][j] = '#'
      for p in range(i-1, i+2):
        if p < 0 or h-1 < p:
          continue
        for q in range(j-1, j+2):
          if q < 0 or w-1 < q:
            continue
          elif dp[p][q] == '#':
            continue
          dp[p][q] += 1
      
for k in dp:
  print(*k, sep='')

Submission status: Accepted

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

#%%
mine = [['#' for _ in range(w)] for __ in range(h)]

def counter(i, j):
    if (0 <= i <= h-1) and (0 <= j <=w-1):
        if field[i][j] == '#':
            return 1
        else:
            return 0
    else:
        return 0

for i in range(h):
    for j in range(w):
        cnt = 0
        if field[i][j] == '#':
            mine[i][j] = '#'
        else:
            for k, l in itertools.product([i-1, i, i+1], [j-1, j, j+1]):
                cnt += counter(k, l)
            mine[i][j] = cnt

#%%
for m in mine:
    print(''.join(map(str, m)))

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for di in range(-1, 2):
                for dj in range(-1, 2):
                    tmp_i = i+di
                    tmp_j = j+dj
                    if (0 <= tmp_i <= H-1) and (0 <= tmp_j <= W-1) and (S[tmp_i][tmp_j]=="#"):
                        cnt += 1
            S[i][j] = cnt

for s in S:
    for i in s:
        print(i, end="")
    print()

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if lst[i][j]==".":
            cnt=0
            for h,w in ([0,1],[0,-1],[1,0],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]):
                nh,nw=i+h,j+w
                if nh<0 or nh>H-1 or nw<0 or nw>W-1:
                    continue
                if lst[nh][nw]=="#":
                    cnt+=1
            lst[i][j]=cnt

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

Submission status: Accepted

Solution code:


import numpy as np
H, W = map(int, input().split())
M = []
for h in range(H):
    col = []
    s = input()
    for w in range(W):
        if s[w] == '.':
            col.append(0)
        else:
            col.append(np.inf)
    M.append(col)



for h in range(H):
    for w in range(W):
        flag_h0= True
        flag_hM = True
        flag_w0 = True
        flag_wM = True
        if M[h][w] == np.inf:
            
            if h==0:
                flag_h0= False
            if h == H - 1:
                flag_hM = False
            if w== 0:
                flag_w0 = False
            if w== W-1:
                flag_wM = False
            
            if flag_h0:
                M[h-1][w] += 1
                if  flag_w0:
                    M[h-1][w-1] += 1
                if flag_wM:
                    M[h-1][w+1] += 1
            
            if flag_w0:
                M[h][w-1] += 1
            if flag_wM:
                M[h][w+1] += 1
            
            if flag_hM:
                M[h+1][w] += 1

                if flag_w0:
                    M[h+1][w-1] += 1
                if flag_wM:
                    M[h+1][w+1] += 1

                    
for h in range(H):
    tmp_ = ''
    for w in range(W):
        if M[h][w] < np.inf:
            tmp_ += str(M[h][w])
        else:
            tmp_  += '#'
    print(tmp_)

Submission status: Accepted

Solution code:
hw, *ss = open(0).read().splitlines()

H, W = map(int, hw.split())

ss = list(map(list, ss))

from itertools import product

for h in range(H):
    for w in range(W):
        if ss[h][w] == '#':
            continue
        ans = 0
        for i,j in product([-1,0,1], [-1,0,1]):
            if 0 <= h+i < H and 0 <= w+j < W:
                ans += (ss[h+i][w+j] == '#')
        ss[h][w] = ans

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

Submission status: Accepted

Solution code:
from collections import deque
h, w = map(int, input().split())

grid = [list(input()) for _ in range(h)]

dy = (-1, -1, -1, 0, 0, 0, 1, 1, 1)
dx = (-1, 0, 1, -1, 0, 1, -1, 0, 1)

for i in range(h):
    for j, value in enumerate(grid[i]):
        if grid[i][j] == "#":
            continue
        else:
            bom_cnt = 0
            for k in range(9):
                y = i + dy[k]
                x = j + dx[k]
                if (y < 0 or y >= h or x < 0 or x >= w): continue
                if grid[i][j] == "#": continue
                if grid[y][x] == "#":
                    bom_cnt += 1

            grid[i][j] = bom_cnt

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

Submission status: Accepted

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

for i in range(1,h+1):
    temp = ""
    for j in range(1,w+1):
        cnt = 0
        if maze[i][j] == "#":
            temp += "#"
        else:
            for y in range(-1,2,1):
                for x in range(-1,2,1):
                    if maze[i+y][j+x] == "#":
                        cnt += 1
            temp += str(cnt)
    ans += [temp]
for i in range(h):
    print(*ans[i],sep="")

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

#import numpy as np

def main():
	h,w=MI()
	G=[[0]*(w+2)]
	for _ in range(h):
		G.append([0]+list(input())+[0])
	G.append([0]*(w+2))

	for i in range(h+2):
		for j in range(w+2):
			if G[i][j]==".":
				cnt=0
				for ii in range(i-1,i+2):
					for jj in range(j-1,j+2):
						if G[ii][jj]=="#":
							cnt+=1
				G[i][j]=cnt
	G=G[1:-1]

	for s in G:
		print(*s[1:-1],sep="")


if __name__ == "__main__":
	main()

Submission status: Accepted

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

H, W = map(int, input().split())

S = []
ans_S = []

for i in range(H):
    tmp_s = list(str(input()))
    S.append(tmp_s)
    ans_S.append(tmp_s)

for y in range(H):
    for x in range(W):
        tmp_ans_S = 0
        if S[y][x] == '#':
            tmp_ans_S = '#'
        else:
            # 1 左
            if x > 0:
                if S[y][x - 1] == '#':
                    tmp_ans_S += 1 
            # 2 左上
            if x > 0 and y > 0:
                if S[y - 1][x - 1] == '#':
                    tmp_ans_S += 1
            # 3 上
            if y > 0:
                if S[y - 1][x] == '#':
                    tmp_ans_S += 1
            # 4 右上
            if x < (W - 1) and y > 0:
                if S[y - 1][x + 1] == '#':
                    tmp_ans_S += 1
            # 5 右
            if x < (W - 1):
                if S[y][x + 1] == '#':
                    tmp_ans_S += 1
            # 6 右下
            if x < (W - 1) and y < (H - 1):
                if S[y + 1][x + 1] == '#':
                    tmp_ans_S += 1
            # 7 下
            if y < (H - 1):
                if S[y + 1][x] == '#':
                    tmp_ans_S += 1
            # 8 左下
            if x > 0 and y < (H - 1):
                if S[y + 1][x - 1] == '#':
                    tmp_ans_S += 1
        # ヒトマスごとに集計した分の挿入
        ans_S[y][x] = tmp_ans_S

for ans in ans_S:
    print(*ans, sep='')

Submission status: Accepted

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

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

[print(s[i][1:w+1]) for i in range(1,h+1)]