Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

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

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

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

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

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

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

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

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

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

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

Sample Input 1

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

Sample Output 1

11211
1#2#1
11211

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

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

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

Sample Input 2

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

Sample Output 2

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

It is possible that there is no empty square.  

Sample Input 3

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

Sample Output 3

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



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


Sample output:
11211
1#2#1
11211

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
result = []
for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            result.append("#")
        else:
            count = 0
            for h2 in range(h - 1, h + 2):
                for w2 in range(w - 1, w + 2):
                    if 0 <= h2 < H and 0 <= w2 < W:
                        if S[h2][w2] == "#":
                            count += 1
            result.append(str(count))
    print("".join(result))
    result.clear()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,1,1,1,0,-1,-1,-1]
# S_ij = S[i-1][j-1]
S_count = []
for i in range(H):
    Count = []
    for j in range(W):
        if S[i][j] == ".":
            count = 0
            for t in range(8):
                if j+dx[t] <0 or j+dx[t] > W-1 or i+dy[t] <0 or i+dy[t] >H-1:
                    pass
                elif S[i+dy[t]][j+dx[t]] == "#":
                    count += 1
                else:
                    pass
        else:
            count = "#"
        Count.append(count)
    S_count.append(Count)
for i in range(H):
    R = "".join(map(str,S_count[i]))
    print(R)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
bom_dic = {}
for h_num in range(1, h+1):
    bom_dic_child = {}
    num_row = input()
    num_list = []
    for num in num_row:
        num_list.append(num)

    for idx, num in enumerate(num_list):
        bom_dic_child[idx+1] = num
    bom_dic[h_num] = bom_dic_child

result_dic = {}
for row_idx, row_str in bom_dic.items():
    result_dic_row = {}
    for column_idx, cell_str in row_str.items():
        if cell_str == ".":
            bom_count = 0
            for dx in range(-1,2):
                for dy in range(-1,2):
                    x = column_idx + dx
                    y = row_idx + dy
                    if w >= x > 0 and h >= y > 0:
                        terget_str = bom_dic[y][x]
                        if terget_str == "#":
                            bom_count += 1 
            result_dic_row[column_idx] = bom_count
        else:
            result_dic_row[column_idx] = cell_str
    result_dic[row_idx] = result_dic_row

for print_row in result_dic.values():
    for cell_str in print_row.values():
        print(cell_str, end="")
    print()

Submission status: Accepted

Solution code:
import numpy as np

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

ar = [["."] * w for i in range(h)]

for i in range(h):
    st = input()
    for j, s in enumerate(st):
        ar[i][j] = s

for i in range(h):
    for j in range(w):
        if ar[i][j] == ".":
            sum = 0
            for ii in range(3):
                for jj in range(3):
                    if i+ii >h or j + jj > w:
                        continue
                    elif (i == 0 and ii ==0) or(j ==0 and jj==0):
                        continue
                    
                    if ar[i + ii-1][j + jj-1] == "#":
                        sum += 1
            ar[i][j] = str(sum)



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

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if l[i][j] == '#': continue
        c = 0
        if i > 0:
            if j > 0 and l[i-1][j-1] == '#': c += 1
            if l[i-1][j] == '#': c += 1
            if j < w-1 and l[i-1][j+1] == '#': c += 1
        if j > 0 and l[i][j-1] == '#': c += 1
        if j < w-1 and l[i][j+1] == '#': c += 1
        if i < h-1:
            if j > 0 and l[i+1][j-1] == '#': c += 1
            if l[i+1][j] == '#': c += 1
            if j < w-1 and l[i+1][j+1] == '#': c += 1
        ans[i][j] = c
        
for row in ans:
    print(''.join(list(map(str, row))))

Submission status: Accepted

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


dxdy = [(-1,-1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
ans = [['#' for _ in range(w)] for _ in range(h)] 

for y in range(h):
    for x in range(w):
        if l[y][x] == '#': 
          continue
        ct = 0
        for dx,dy in dxdy:
            if x+dx < 0 or x+dx > w-1 or y+dy < 0 or y+dy > h-1: 
              continue
            if l[y+dy][x+dx] == "#": 
              ct += 1
            
        ans[y][x] = str(ct)
        

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [[''for i in range(w+2)]for j in range(h+2)]
for i in range(h):
    s[i+1][1:w+1] = input()
for i in range(h):
    for j in range(w):
        if s[i+1][j+1] == '.' :
            s[i+1][j+1] = str(1*(sum(s[k+i][j:j+3].count('#')for k in range(3))))
    print(*s[i+1][1:w+1],sep='')

Submission status: Accepted

Solution code:


def main()->None:
    # gridの処理
    # 近傍の座標をdx=[-1,0,1], dy=[-1,0,1]を用いて
    # (xx, yy) が　xx = x + dx , yy = y + dy　とできる
    # 参考：http://prd-xxx.hateblo.jp/entry/2017/10/19/205331
    # 入力のグリッドから一点ずつ座標指定して選んでその周囲８マスを調べる。
    H, W = map(int, input().split())
    src = [input() for _ in range(H)]
    ans = []
    for row in src:
        ans.append(list(row))


    dxdy = [(-1,-1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for y in range(H):
        for x in range(W):
            if src[y][x] == "#":continue
            c = 0
            for dx, dy in dxdy:
                # 探索範囲はx:0~W, y:0~Hでその周囲８マスを探索する
                if x+dx < 0 or x+dx > W-1 or y+dy < 0 or y+dy > H-1: continue
                if src[y+dy][x+dx] == "#": c += 1
            ans[y][x] = c
    
    for row in ans:
        print("".join(list(map(str, row))))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import product

def inc(i, j, H, W, field):
    if not(0 <= i <= H-1 and 0 <= j <= W-1):
        pass
    elif field[i][j] == "#":
        pass
    else:
        field[i][j] += 1


def main():
    H, W = [int(i) for i in input().split()]
    ans = [[0] * W for _ in range(H)]
    for i in range(H):
        line = input()
        for j in range(W):
            if line[j] == "#":
                ans[i][j] = "#"
                for d in product([-1, 0, 1], repeat=2):
                    d_i, d_j = d
                    inc(i+d_i, j+d_j, H, W, ans)
    for a in ans:
        print(*a, sep="", end="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

h, w = map(int, sys.stdin.readline().split())
canvas = ['.' * (w + 2)] + ['.' + sys.stdin.readline().rstrip() + '.' for _ in range(h)] + ['.' * (w + 2)]


def main():
    res = [[0] * (w + 1) for _ in range(h + 1)]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if canvas[i][j] == '#':
                res[i][j] = '#'
                continue
            for di in range(-1, 2):
                for dj in range(-1, 2):
                    y = i + di
                    x = j + dj
                    if canvas[y][x] == '#':
                        res[i][j] += 1
    
    for i in range(1, h+1):
        yield ''.join(list(map(str, res[i][1:])))

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            grid[i + 1][j + 1] = '#'

            if grid[i][j] != '#':
                grid[i][j] += 1

            if grid[i][j + 1] != '#':
                grid[i][j + 1] += 1

            if grid[i][j+2] != '#':
                grid[i][j+2] += 1

            if grid[i+1][j] != '#':
                grid[i+1][j] += 1

            if grid[i+1][j+2] != '#':
                grid[i+1][j+2] += 1

            if grid[i+2][j] != '#':
                grid[i+2][j] += 1

            if grid[i+2][j+1] != '#':
                grid[i+2][j+1] += 1

            if grid[i+2][j+2] != '#':
                grid[i+2][j+2] += 1




for i in range(1, h+1):
    ans = ''
    for j in range(1, w+1):
        ans += str(grid[i][j])
    print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
grid = []
for i in range(H+2):
    grid.append([0]*(W+2))
    
idxes = []
tmpIdxes = []
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            idxes.append([i,j])
            tmpIdxes.append([i+1, j+1])
            
for tmpIdx in tmpIdxes:
    grid[tmpIdx[0]-1][tmpIdx[1]-1] += 1
    grid[tmpIdx[0]-1][tmpIdx[1]] += 1
    grid[tmpIdx[0]-1][tmpIdx[1]+1] += 1
    grid[tmpIdx[0]][tmpIdx[1]-1] += 1
    grid[tmpIdx[0]][tmpIdx[1]+1] += 1
    grid[tmpIdx[0]+1][tmpIdx[1]-1] += 1
    grid[tmpIdx[0]+1][tmpIdx[1]] += 1
    grid[tmpIdx[0]+1][tmpIdx[1]+1] += 1

for tmpIdx in tmpIdxes:
    grid[tmpIdx[0]][tmpIdx[1]] = "#"

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

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
S = [list(input()) for _ in range(H)] #データ形式はS[i][j]

eight = [[-1,-1],[0,-1],[1,-1], # 周囲8マスの定義
         [-1,0],[1,0],
         [-1,1],[0,1],[1,1]]

for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      for e in eight:
        if (0 <= i+e[0] < H) and (0 <= j+e[1] < W) :
          target = S[i+e[0]][j+e[1]]
          if target != '#' :
            if target == '.' :
              S[i+e[0]][j+e[1]] = 1
            else :
              S[i+e[0]][j+e[1]] += 1

for i in range(H):
  for j in range(W):
    print('0' if S[i][j] == '.' else str(S[i][j]), end='')
  if i < H: print('')

Submission status: Accepted

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

for i in range(H):
    ls.append([i if i == '#' else 0 for i in input().split()[0]])

for i in range(H):
    for j in range(W):
        if ls[i][j] == '#':
            ii_min = i if (i - 1) < 0 else i - 1
            ii_max = i if (i + 1) >= H else i + 1
            jj_min = j if (j - 1) < 0 else j - 1
            jj_max = j if (j + 1) >= W else j + 1

            for ii in range(ii_min, ii_max + 1):
                for jj in range(jj_min, jj_max + 1):
                    if ls[ii][jj] != '#':
                        ls[ii][jj] += 1

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

Submission status: Accepted

Solution code:
import sys
h,w = map(int, sys.stdin.readline().split())
s = list(input() for _ in range(h))

dx_list = [-1, 0, 1]
dy_list = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == "#":
            print("#", end="")
            continue
        for dx in dx_list:
            for dy in dy_list:
                x = i + dx
                y = j + dy
                if 0 <= x < h and 0 <= y < w and s[x][y] == "#":
                    count += 1
        print(count, end="")
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = (
    [["."] * (W + 2)]
    + [["."] + [i for i in input()] + ["."] for j in range(H)]
    + [["."] * (W + 2)]
)
ans = []
for i in range(1, H + 1):
    i_ans = []
    for j in range(1, W + 1):
        if S[i][j] == "#":
            i_ans.append("#")
            continue
        num = 0
        arrows = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
        for arrow in arrows:
            if S[i + arrow[0]][j + arrow[1]] == "#":
                num += 1
        i_ans.append(num)
    ans.append(i_ans)
for i in ans:
    print(*i, sep="")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
H,W=map(int,input().rstrip().split())
S=[input().rstrip() for _ in range(H)]
c=[[0 for _ in range(W)] for __ in range(H)]
dx=[0,1,1,1,0,-1,-1,-1]#12時から時計回り
dy=[1,1,0,-1,-1,-1,0,1]

for i in range(H):
    for j in range(W):
        if(S[i][j]=="#"):
            c[i][j]="#"
            continue
        for direction in range(8):
            if(i==0):
                if(i+dy[direction]<0):
                    continue
            if(i==H-1):
                if(i+dy[direction]>=H):
                    continue
            if(j==0):
                if(j+dx[direction]<0):
                    continue
            if(j==W-1):
                if(j+dx[direction]>=W):
                    continue

            if(S[i+dy[direction]][j+dx[direction]]=="#"):
                c[i][j]+=1

for i in range(H):
    s=""
    for j in range(W):
        s+=str(c[i][j])
    print(s)

Submission status: Accepted

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

for i in range(h):
    ans = ''
    for j in range(w):
        if s[i][j] == '.':
            n = 0
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    xx = x + i
                    yy = y + j
                    if (x != 0 or y != 0) and xx >= 0 and xx <= h - 1 and yy >= 0 and yy <= w - 1:
                        if s[xx][yy] == '#':
                            n += 1
            ans += str(n)
        else:
            ans += '#'
    print(ans)

Submission status: Accepted

Solution code:
H,W=map(int, input().split(' '))
Map = [list("A" + input() + "A") for i in range(H)]
Map.insert(0,["A"]*(W+2))
Map.append(["A"]*(W+2))
ans = [[] for i in range(H)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if Map[i][j] == "#":
            ans[i-1].append("#")
        else:
            minenum = 0
            for (x,y) in [(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)]:
                if Map[x][y] == "#":
                    minenum += 1
            ans[i-1].append(minenum)
for x in ans:
    print(*x,sep="")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s0 = list(["." for _ in range(w+2)])
s1 = [list("." + input() + ".") for _ in range(h)]
s = []
s.append(s0)
s = s + s1
s.append(s0)
a = [-1,0,1]
b = [-1,0,1]
for i in range(1,h+1) :
    for j in range(1,w+1):
        if not s[i][j] == "#":
            cnt = 0
            for ii in range(3):
                for jj in range(3):
                    if s[i+a[ii]][j+a[jj]] == "#":
                        cnt +=1
            s[i][j] = str(cnt)
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:
h,w=map(int,input().split())
L=[list(input()) for i in range(h)]
#L[y][x]
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 L[i][j]=='#':
            for k in range(8):
                xr=j+dx[k]
                yr=i+dy[k]
                if (yr>=0 and yr<h and xr>=0 and xr<w):
                    if L[yr][xr]=='.':
                        L[yr][xr]=1
                    elif L[yr][xr]=='#':
                        continue
                    else:
                        L[yr][xr]+=1
        elif L[i][j]=='.':
            L[i][j]=0
for i in range(h):
    print(*L[i],sep='')

Submission status: Accepted

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

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

ans = []
for i in range(1, H+1):
    tmp = []
    for j in range(1, W+1):
        count = 0
        if S[i-1][j-1] == "#":
            count += 1
        if S[i][j-1] == "#":
            count += 1
        if S[i-1][j] == "#":
            count += 1
        if S[i+1][j-1] == "#":
            count += 1
        if S[i-1][j+1] == "#":
            count += 1
        if S[i][j+1] == "#":
            count += 1
        if S[i+1][j] == "#":
            count += 1
        if S[i+1][j+1] == "#":
            count += 1
        if S[i][j] == "#":
            tmp.append("#")
        else:
            str_count = str(count)
            tmp.append(str_count)
    ans.append(tmp)

for i in range(len(ans)):
    print("".join(ans[i]))

Submission status: Accepted

Solution code:
h, w = map(int, (input().split()))
s = [['.'] * (w + 2)] + \
    [list('.' + 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] == '.':
            s[i][j] = (s[i - 1][j - 1:j + 2] +
                       s[i][j - 1:j + 2] +
                       s[i + 1][j - 1:j + 2]).count('#')

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

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

Submission status: Accepted

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

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

for hi in range(h):
  for wi in range(w):
    if ff[hi][wi]=='#':
      pass
    else:
      count = 0
      for dx,dy in d:
        x = dx+wi
        y = dy+hi        
        if (0<= x < w) and (0<= y <h) :
          if ff[y][x] == '#':
            count += 1
      ff[hi][wi] = str(count)
      
for fi in ff:
  print(''.join(fi))

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    if l[i][j]=='#':
      ans[i+1][j+1]=10
      
      ans[i][j]+=1
      ans[i][j+1]+=1
      ans[i][j+2]+=1
      
      ans[i+1][j]+=1
      ans[i+1][j+2]+=1
      
      ans[i+2][j]+=1
      ans[i+2][j+1]+=1
      ans[i+2][j+2]+=1
      
ans=[i[1:-1] for i in ans][1:-1]
ans=[['#' if j>=10 else str(j) for j in i] for i in ans]
for i in ans:
  print(''.join(i))

Submission status: Accepted

Solution code:
import numpy as np
h,w = map(int, input().split())
mat = np.array([list(map(int,input().replace(".","0").replace("#","1"))) for i in range(h)])
mat = np.concatenate([np.zeros((1,w),int),mat,np.zeros((1,w),int)])
mat = np.concatenate([np.zeros((h+2,1),int),mat,np.zeros((h+2,1),int)], axis = 1)
import copy
nmat = copy.deepcopy(mat)
for i in range(1,h+1):
  for j in range(1, w+1):
    if mat[i,j] == 0:
      nmat[i,j] = mat[i+1,j]+mat[i-1,j]+mat[i,j+1]+mat[i,j-1]+mat[i-1,j-1]+mat[i-1,j+1]+mat[i+1,j-1]+mat[i+1,j+1]
nmat = np.array(nmat, dtype = "str")
nmat[np.where(mat == 1)] = "#"
for i in range(h):
  print("".join(nmat[1:h+1,1:w+1].tolist()[i]))

Submission status: Accepted

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

for i in range(1, h+1):
    for j in range(1, w+1):
        if S[i][j] == ".":
            S[i][j] = (str([S[i-1][j], S[i+1][j], S[i][j-1], S[i][j+1], S[i-1][j-1], S[i-1][j+1], S[i+1][j-1], S[i+1][j+1]].count("#")))
for k in range(len(S[1:h+1])):
    print(*S[1:h+1][k][1:w+1], sep="")

Submission status: Accepted

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

N = [(0, 1), (1, 0), (1, 1), (0, -1), (-1, 0), (1, -1), (-1, 1), (-1, -1)]
ans = [[] for _ in range(H)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == '.':
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == '#' for n in N]))
        else:
            ans[h - 1].append('#')

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

Submission status: Accepted

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

def search(h,w):
    count = 0
    if mat[h][w] == '#':
        return '#'
    else:
        if h+1 <= H-1:
            if mat[h+1][w] == '#':
                count += 1
            if w+1 <= W-1:
                if mat[h+1][w+1] == '#':
                    count += 1
            if w-1 >= 0:
                if mat[h+1][w-1] == '#':
                    count += 1
        if h-1 >= 0:
            if mat[h-1][w] == '#':
                count += 1
            if w+1 <= W-1:
                if mat[h-1][w+1] == '#':
                    count += 1
            if w-1 >= 0:
                if mat[h-1][w-1] == '#':
                    count += 1
        if w+1 <= W-1:
            if mat[h][w+1] == '#':
                count += 1
        if w-1 >= 0:
            if mat[h][w-1] == '#':
                count += 1   
        return count
    
for i in range(H):
    for j in range(W):
        ans[i][j] = search(i,j)
        
for i in range(H):
    ans[i] = map(str,ans[i])
    print(''.join(ans[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
dx=[0,1,0,-1,1,1,-1,-1]
dy=[1,0,-1,0,1,-1,1,-1]#周囲八マスの進み方：マスから出ないか判定して調べる

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

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

Submission status: Accepted

Solution code:
Y,X=map(int,input().split())
info=[]
for i in range(Y):
    s=list(input())
    info.append(s)
#print(info)

def search(y,x):
    a=0
    if 0<=y<=Y-1 and 0<=x<=X-1:
        if info[y][x]=="#":
            a+=1
    return a
            
def count(y,x):
    num=0
    num+=search(y-1,x-1)
    num+=search(y-1,x)
    num+=search(y-1,x+1)
    num+=search(y,x-1)
    num+=search(y,x+1)
    num+=search(y+1,x-1)
    num+=search(y+1,x)
    num+=search(y+1,x+1)
    return num

for i in range(Y):
    for j in range(X):
        if info[i][j]!="#":
            info[i][j]=str(count(i,j))
for i in range(Y):
    for j in range(X):
        print(info[i][j],end="")
    print()

Submission status: Accepted

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


al_l = [["#" for _ in range(w)] for _ in range(h)]

for i in range(h):
    for j in range(w):
        if sl[i][j] == "#":
            continue
        ans = 0
        for v in range(i-1, i+2):
            for p in range(j-1, j+2):
                if 0 > v or v > h-1:
                    continue
                if 0 > p or p > w-1:
                    continue
                if sl[v][p] == "#":
                    ans += 1
        al_l[i][j] = str(ans)

for al in al_l:
    print("".join(al))

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 hi in range(h):
    for wi in range(w):
        c = 0
        if a[hi][wi] == "#":
            print('#',end='')
        else:
            print(np.count_nonzero(a[max(0,hi-1):hi+2,max(0,wi-1):wi+2] == "#"),end='',)
    print()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=list(map(list,[input() for i in range(h)]))
for i in range(h):
    for j in range(w):
        if l[i][j]=="#":
            continue
        num=0

        if i!=0 and j!=0 and l[i-1][j-1]=="#":
            num+=1
        if i!=0 and l[i-1][j]=="#":
            num+=1
        if i!=0 and j!=w-1 and l[i-1][j+1]=="#":
            num+=1
        if j!=0 and l[i][j-1]=="#":
            num+=1
        if j!=w-1 and l[i][j+1]=="#":
            num+=1
        if i!=h-1 and j!=0 and l[i+1][j-1]=="#":
            num+=1
        if i!=h-1 and l[i+1][j]=="#":
            num+=1
        if i!=h-1 and j!=w-1 and l[i+1][j+1]=="#":
            num+=1

        l[i][j]=str(num)
    print("".join(l[i]))

Submission status: Accepted

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

bomb=[]

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

for i in range(len(bomb)):
    bomb[i].insert(0,0)
    bomb[i].append(0)

bomb.insert(0,[0 for j in range(W+2)])
bomb.append([0 for j in range(W+2)])

result=[[0 for i in range(W+2)] for j in range(H+2)]

count=0

for i in range(1,H+1):
    for j in range(1,W+1):
        if bomb[i][j]=='#':
            result[i][j]=-1
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if(bomb[k][l] != '#'):
                        result[k][l]+=1

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

Submission status: Accepted

Solution code:
H , W = map(int,input().split())
stage = list(input() for _ in range(H))
dy_list = [1,1,0,-1,-1,-1,0,1]
dx_list = [0,1,1,1,0,-1,-1,-1]

for y in range(H):
    for x in range(W):
        cnt = 0
        Flag = False
        for dx , dy in zip(dy_list , dx_list):
            if stage[y][x] == "#":
                break
            Flag = True
            if 0 <= y + dy < H and 0 <= x + dx < W:
                if stage[y + dy][x + dx] == "#":
                    cnt += 1
        if Flag:
            print(cnt , end = "")
        else:
            print("#" ,end = "")
    print()

Submission status: Accepted

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

ls_ans = [[0 for _ in range(w+2)] for _ in range(h+2)]


for i in range(h):
    for j in range(w):
        if ls_s[i][j] == "#":
            ls_ans[i][j] += 1
            ls_ans[i][j+1] += 1
            ls_ans[i][j+2] += 1
            ls_ans[i+1][j] += 1
            ls_ans[i+1][j+2] += 1
            ls_ans[i+2][j] += 1
            ls_ans[i+2][j+1] += 1
            ls_ans[i+2][j+2] += 1

for i in range(h):
    for j in range(w):
        if ls_s[i][j] == "#":
            ls_ans[i+1][j+1] = "#"
            
del(ls_ans[0])
del(ls_ans[h])

for k in range(h):
    ls_ans[k] = ls_ans[k][1:w+1]
    
for a in ls_ans:
    print(*a, sep="")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
matrix = [input() for i in range(H)] 
ans = [[0 for j in range(W)] for i in range(H)] 
dy = [-1, -1, -1, 0, 0, 1, 1, 1]
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
bomb = []
for h in range(H):
        for w in range(W):
                if matrix[h][w] == "#":
                        bomb.append([h, w]) 
                        for i in range(8):
                                if H -1 < h + dy[i] or h + dy[i] < 0:
                                        continue
                                if W - 1 < w + dx[i] or w + dx[i] < 0:
                                        continue
                                ans[h + dy[i]][w + dx[i]] += 1
for i in bomb:
        ans[i[0]][i[1]] = "#" 
for i in ans:
        l = map(str, i)
        print("".join(l))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
matrix = [input() for i in range(H)] 
ans = ""  
for h in range(H):
        for w in range(W):
                bomb = 0 
                for i in range(-1, 2): 
                        if 0 <= h - 1 and 0 <= w + i < W:
                                if matrix[h - 1][w + i] == "#":
                                        bomb += 1
                        if h + 1 < H and 0 <= w + i < W:
                                if matrix[h + 1][w + i] == "#":
                                        bomb += 1
                for i in range(-1, 2, 2): 
                        if 0 <= w + i < W:
                                if matrix[h][w + i] == "#":
                                        bomb += 1
                if matrix[h][w] == "#":
                        ans += "#" 
                else:
                        ans += str(bomb)
                if W - 1 > 0:
                        if w % (W - 1) == 0 and w != 0:
                                ans += "\n"
                else:
                                ans += "\n"
print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
lst = [input() for _ in range(h)]
nlst = [[0] * (w + 2) for _ in range(h + 2)]
for i in range(h):
    for j in range(w):
        if lst[i][j] == '#':
            nlst[i][j] += 1
            nlst[i][j + 1] += 1
            nlst[i][j + 2] += 1
            nlst[i + 1][j] += 1
            nlst[i + 1][j + 2] += 1
            nlst[i + 2][j] += 1
            nlst[i + 2][j + 1] += 1
            nlst[i + 2][j + 2] += 1

output = [''] * h
for i in range(h):
    for j in range(w):
        if lst[i][j] == '.':
            output[i] += str(nlst[i + 1][j + 1])
        else:
            output[i] += '#'
print(*output, sep='\n')