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())
b=["n"*(w+2)]
for i in range(h):b.append("n"+input()+"n")
b.append(b[0])
def num(i,j):
  a=0
  def same(i,j):
    global b
    if b[i][j]=="#":return 1
    return 0
  a+=same(i-1,j-1)
  a+=same(i-1,j)
  a+=same(i-1,j+1)
  a+=same(i,j-1)
  a+=same(i,j+1)
  a+=same(i+1,j-1)
  a+=same(i+1,j)
  a+=same(i+1,j+1)
  return a
for i in range(len(b)):
  if 0<i<h+1:
    for j in range(len(b[i])):
      if b[i][j]==".":b[i]=b[i][:j]+str(num(i,j))+b[i][j+1:]
for i in range(1,h+1):print(b[i][1:w+1])

Submission status: Accepted

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

S_dim = [[0] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        S_dim[i][j] = S[i][j:j + 1]

S_num = [[0] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        count = 0
        if S_dim[i][j] == ".":
            for dy in [-1, 0, 1]:
                for dx in [-1, 0, 1]:
                    if i + dy < 0 or i + dy >= H or j + dx < 0 or j + dx >= W:
                        continue
                    if S_dim[i + dy][j+ dx] == "#":
                        count += 1
            S_num[i][j] = str(count)
        else:
            S_num[i][j] = "#"

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
A=list('.'*(w+2))
MAP=[A]
for _ in range(h):
    MAP.append(list('_'+input()+'_'))
MAP.append(A)
for i in range(1,h+1):
    for j in range(1,w+1):
        if MAP[i][j]=='.':
            MAP[i][j]=str((MAP[i-1][j-1]+MAP[i-1][j]+MAP[i-1][j+1]+MAP[i][j-1]+MAP[i][j+1]
                            +MAP[i+1][j-1]+MAP[i+1][j]+MAP[i+1][j+1]).count('#'))
for i in range(1,h+1):
    print(''.join(MAP[i][1:w+1]))

Submission status: Accepted

Solution code:
H, W = map(int, (input().split()))
# Hが縦、Wが横!
rows = []
for _ in range(0, H):
    rows.append(input())

x = [0, 1, -1,  0,  1, -1, 1, -1]
y = [1, 1,  1, -1, -1, -1, 0,  0]
for row_i in range(0, H):
    result_col = ""
    # ここの処理で隣接部分を吐き出して,result_colに数値を詰める
    for col_i in range(0, W):
        if rows[row_i][col_i] == "#":
            result_col += "#"
            # これもし爆弾だったら周囲のマスに数値足して行ったほうが賢い?
            # いやどうせ九方向集めるなら一緒か。
            continue
        mines = 0
        for i in range(0, 8):
            dx = col_i + x[i]
            dy = row_i + y[i]
            if dx >= W or dx < 0 or dy >= H or dy < 0:
                continue
            if rows[dy][dx] == "#":
                mines += 1
        result_col += str(mines)
    print(result_col)

Submission status: Accepted

Solution code:
def minesweeper(H: int, W: int, S: list)->list:
    directions = [
        (-1, -1), (-1, 0), (-1, 1),
        (0, -1),           (0, 1),
        (1, -1),  (1, 0),  (1, 1),
    ]
    ret = [[''] * W for _ in range(H)]

    for h in range(H):
        for w in range(W):
            if S[h][w] == '#':
                ret[h][w] = '#'
                continue

            count = 0
            for dh, dw in directions:
                if h + dh < 0:
                    continue
                if H <= h + dh:
                    continue
                if w + dw < 0:
                    continue
                if W <= w + dw:
                    continue
                if S[h+dh][w+dw] == '#':
                    count += 1
            ret[h][w] = str(count)

    return ret


if __name__ == "__main__":
    H = 0
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    ans = minesweeper(H, W, S)
    for a in ans:
        print(''.join(a))

Submission status: Accepted

Solution code:
h,w= (int(i) for i in input().split())

s= [ ["_" for i in range(w + 2)] ]
for i in range(h):
    s.append(list("_" + input() + "_"))
s.append(["_" for i in range(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-1] + s[i-1][j] + s[i-1][j+1]
            + s[i][j-1] +  s[i][j+1]
            + s[i+1][j-1] + s[i+1][j] + s[i+1][j+1]).count("#"))


for i in range(1, h+1):
    print ("".join(s[i]).replace("_",""))

Submission status: Accepted

Solution code:
high,width = map(int, input().split())
s_list = [list(input()) for _ in range(high)]
tmp_list = [[0 for _ in range(width)] for _ in range(high)]
dir = [[-1,-1],[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0]]
for h in range(high):
    for w in range(width):
        if s_list[h][w] == '#':
            tmp_list[h][w] = '#'
            for x,y in dir:
                tate = h+x
                yoko = w+y
                if tate >= 0 and yoko >= 0 and tate < high and yoko < width:
                    if s_list[tate][yoko]=='.':
                        tmp_list[tate][yoko] += 1

for tmp in tmp_list:
    t = map(str, tmp)
    print(''.join(t))

Submission status: Accepted

Solution code:
def search(i,j):
  b = [-1,0,1]
  c = 0
  for dx in b:
    for dy in b:
      if dx == 0 and dy == 0:
        continue
      else:
        x = i + dx
        y = j + dy
        if 0 <= x < h and 0 <= y < w:
          if a[x][y] == "#":
            c += 1
  bomb = str(c)
  return bomb
      
h,w = map(int, input().split())
a = [list(input()) for i in range(h)]
for i in range(h):
  for j in range(w):
    if a[i][j] == "#":
      continue
    else:
      a[i][j] = search(i,j)
  print("".join(a[i]))

Submission status: Accepted

Solution code:
def CountMine(x, y):
    count = 0
    for k in range(3):
        for l in range(3):
            if S[x-1+k][y-1+l] == '#':
                count += 1
    return count
    
    

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

for i in range(H):
    for j in range(W):
        if S[i+1][j+1] == '.':
            print(CountMine(i+1, j+1), end="")
        else:
            print('#', end="")
    print('\n')

Submission status: Accepted

Solution code:
OFFSETS = [(-1,-1), (-1,0), (-1,1),
           (0,-1), (0,1),
           (1,-1), (1,0), (1,1)]

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

Ses = []
for _ in range(H):
    Ses.append(input())

replaced_Ses = []
for y in range(H):
    replaced_S = ""
    for x in range(W):
        if Ses[y][x] == "#":
            replaced_S += "#"
            continue

        count_bomb = 0
        for dx, dy in OFFSETS:
            now_x = x+dx
            now_y = y+dy
            if now_x < 0 or now_x >= W:
                continue
            if now_y < 0 or now_y >= H:
                continue
            if Ses[now_y][now_x] == "#":
                count_bomb += 1
        replaced_S += str(count_bomb)
    replaced_Ses.append(replaced_S)

for line in replaced_Ses:
    print(line)

Submission status: Accepted

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

ans = Ss.copy()
for y in range(H):
    for x in range(W):
        if ans[y][x] == "#":
            continue
        n = 0
        for dir in [[0, 1], [0, -1], [1, 0], [1, 1], [1, -1], [-1, 0], [-1, 1], [-1, -1]]:
            x2 = x + dir[0]
            y2 = y + dir[1]
            if x2 < 0 or x2 >= W or y2 < 0 or y2 >= H:
                continue
            n += 1 if Ss[y2][x2] == "#" else 0
        ans[y] = ans[y][:x] + str(n) + ans[y][x+1:]

print("\n".join(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

h,w = map(int,input().split())
str_list = [input() for i in range(h)]
result_list = ['' for i in range(h)]

for i in range(h):
    for j in range(w):
        if str_list[i][j] == '.':
            cnt = 0
            for x in range(-1,2):
                for y in range(-1,2):
                    try:
                        if i+x >= 0 and j+y >= 0 and str_list[i+x][j+y] == '#':
                            cnt += 1
                    except IndexError:
                        pass
            result_list[i] += str(cnt)
        elif str_list[i][j] == '#':
            result_list[i] += '#'

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

Submission status: Accepted

Solution code:
import sys
from itertools import product

def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  ans = [[0 for _ in range(w+2)] for _ in range(h+2)]
  for i in range(h):
    s = list(map(str, input().strip()))
    for j in range(w):
      if s[j] == '#':
        ans[i+1][j+1] = '#'
      
        for k in product([-1, 0, 1], repeat=2):
          if ans[i+1+k[0]][j+1+k[1]] != '#':
            ans[i+1+k[0]][j+1+k[1]] += 1
        
  for i in range(1, h+1):
    print(*ans[i][1:w+1], sep='')


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
l = list(map(list, [input() for i in range(H)]))
b = []
for i in range(H):
    for j in range(W):
        if l[i][j] == '#':
            b.append([i+1, j+1])

for i in range(H):
    for j in range(W):
        if l[i][j] == '.':
            l[i][j] = 0
            for k in range(len(b)):
                if int(b[k][0]) - 1 <= i+1 <= int(b[k][0]) + 1 and int(b[k][1]) - 1 <= j+1 <= int(b[k][1]) + 1:
                    l[i][j] = l[i][j] + 1
        print(l[i][j], end="")
    print('')

Submission status: Accepted

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

for h in range(H+2):
  for w in range(W+2):
    if S[h][w] == '.':
      S[h][w] = str([S[x][y] for x in [h-1,h,h+1] for y in [w-1,w,w+1]].count('#'))
      
for i in range(H):
  print(''.join(S[i+1]).replace('x',''))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w] != ".":
            continue
        num = 0
        if h != 0 and S[h - 1][w] == "#":
            num += 1
        if h != H - 1 and S[h + 1][w] == "#":
            num += 1
        if w != 0 and S[h][w - 1] == "#":
            num += 1
        if w != W - 1 and S[h][w + 1] == "#":
            num += 1
        if h != 0 and w != 0 and S[h - 1][w - 1] == "#":
            num += 1
        if h != H - 1 and w != 0 and S[h + 1][w - 1] == "#":
            num += 1
        if h != 0 and w != W - 1 and S[h - 1][w + 1] == "#":
            num += 1
        if h != H - 1 and w != W - 1 and S[h + 1][w + 1] == "#":
            num += 1
        S[h][w] = str(num)
    print("".join(S[h]))

Submission status: Accepted

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

direct_x = [1, 1, 0, -1, -1, -1, 0, 1]
direct_y = [0, 1, 1, 1, 0, -1, -1, -1]

for i in range(h):
    ans = ""
    for j in range(w):
        if s[i][j] == '.':
            count = 0
            for k in range(8):
                if 0 <= i+direct_y[k] < h and 0 <= j+direct_x[k] < w:
                    if s[i+direct_y[k]][j+direct_x[k]] == '#':
                        count += 1
            ans += str(count)
        else:
            ans += "#"
    ans_field.append(ans)

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
field = [list(input()) for _ in range(h)]
direct = []
for i in range(-1, 2):
    for j in range(-1, 2):
        if i or j:
            direct.append((i, j))
def count(x, y):
    ret = 0
    for dx, dy in direct:
        xa = x + dx
        ya = y + dy
        if 0 <= xa < h and 0 <= ya < w and field[xa][ya] == '#':
            ret += 1
    return ret
for i in range(h):
    for j in range(w):
        if field[i][j] == '.':
            print(count(i, j), end = '')
        else:
            print('#', end = '')
    print()

Submission status: Accepted

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

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

for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            for k in range(8):
                if 0 <= i+dy[k] < H and 0 <= j+dx[k] < W:
                    if s[i+dy[k]][j+dx[k]] == '#':
                        count += 1
                    else:
                        pass
                else:
                    pass
            cs.append(str(count))
            count = 0
        else:
            cs.append('#')

for i in range(1, H*W+1):
    print(cs[i-1], end='')
    if i % W == 0 and i % (H*W) != 0:
        print('\n')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(0, H):
    S.append(input())
for i in range(0, H):
    out = ""
    for j in range(0, W):
        if S[i][j]=="#":
            out = out+"#"
        else:
            count = 0
            for k in range(max(0, i-1), min(H, i+2)):
                for l in range(max(0, j-1), min(W, j+2)):
                    if S[k][l]=="#":
                        count = count+1
            out = out+str(count)
    print("{}".format(out))

Submission status: Accepted

Solution code:
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

if H == 1:
    if W == 1:
        if S[0][0] == '.':
            print('0')
        if S[0][0] == '#':
            print('#')
        exit()
    for w in range(1,W-1):
        s = S[0][w]
        if s == '.':
            count = 0
            if S[0][w-1] == '#': count+=1
            if S[0][w+1] == '#': count+=1
            S[0] = S[0][:w] + str(count) + S[0][w+1:]
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w+1] == '#': count+=1
            S[h] = str(count) + S[h][w+1:]
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            S[h] = S[h][:w] + str(count)
    for h in range(H):
        print(S[h])
    exit()
    
if W == 1:
    for h in range(1,H-1):
        w = 0
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            S[h] = str(count)
    for h in range(H):
        print(S[h])
    exit()

    


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=1
        S[0] = S[0][:w] + str(count) + S[0][w+1:]

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

for h in range(1,H-1):
    w = 0
    s = S[h][w]
    if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

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

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]
        
w = 0
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

w = W-1
h = 0
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = W-1
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

for h in range(H):
    print(S[h])

Submission status: Accepted

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

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

        si = 0 if i == 0 else i-1
        sj = 0 if j == 0 else j-1
        ei = w if i == w-1 else i+2
        ej = h if j == h-1 else j+2
        count = 0
        for j_ in range(sj,ej):
            for i_ in range(si,ei):
                if s[j_][i_] == "#":
                    count += 1
        r += str(count)
    result.append(r)

for j in range(h):
    print(result[j])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=[]
S.append(['0']*(w+2))
for i in range(h):
    X=['0']
    X.extend(list(input()))
    X.extend(['0'])
    S.append(X)
S.append(['0']*(w+2))    
    
for i in range(1,h+1):
    for j in range(1,w+1):
        cnt=0
        if S[i][j]=='.':
            for a in range(-1,2):
                for b in range(-1,2):
                    if a==0 and b==0: 
                        continue
                    if S[i+a][j+b]=='#':
                        cnt+=1
            S[i][j]=str(cnt)
    Y=''.join(S[i][1:w+1])
    print(Y)

Submission status: Accepted

Solution code:
args = input().split()

h, w = int(args[0]), int(args[1])

array = []

hoge = ""

for i in range(w + 2):
    hoge += "."

array.append(hoge)
for y in range(h):
    args = "." + input() + "."

    array.append(args)

array.append(hoge)

result = []
for iy in range(h):
    result = ""
    for ix in range(w):
        y = iy + 1
        x = ix + 1
        count = 0
        if array[y][x] == "#":
            result += "#"
        else:
            for fx in [-1, 0, 1]:
                for fy in [-1, 0, 1]:
                    if array[y + fy][x + fx] == "#":
                        count += 1
            result += str(count)
    print(result)

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if S[i][j] == '#':
            continue

        count = 0
        for x in dx:
            for y in dy:
                if 0<=i+y<h and 0<=j+x<w and S[i+y][j+x]=='#':
                    count += 1
        S[i][j] = str(count)

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
for i in range(h):
    T = ""
    for j in range(w):
        if s[i][j] == ".":
            count = 0
            if i < h - 1:
                if s[i + 1][j] == "#":
                    count += 1
                if j < w - 1:
                    if s[i + 1][j + 1] == "#":
                        count += 1
                if j > 0:
                    if s[i + 1][j - 1] == "#":
                        count += 1
            if i > 0:
                if s[i - 1][j] == "#":
                    count += 1
                if j < w - 1:
                    if s[i - 1][j + 1] == "#":
                        count += 1
                if j > 0:
                    if s[i - 1][j - 1] == "#":
                        count += 1
            if j < w - 1:
                if s[i][j + 1] == "#":
                    count += 1
            if j > 0:
                if s[i][j - 1] == "#":
                    count += 1
            T += str(count)
        else:
            T += "#"
    print(T)

Submission status: Accepted

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

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

for i in range(H):
    for j in range(W):
        if field[i][j] == '.':
            num = 0
            for k in range(8):
                ni = i + dx[k]
                nj = j + dy[k]
                if ni < 0 or ni >= H or nj < 0 or nj >= W:
                    continue
                if field[ni][nj] == '#':
                     num += 1
            field[i][j] = str(num)

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

Submission status: Accepted

Solution code:
def CountMine(x, y):
    count = 0
    for k in range(3):
        for l in range(3):
            if S[x-1+k][y-1+l] == '#':
                count += 1
    return count
    
    

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

for i in range(H):
    for j in range(W):
        if S[i+1][j+1] == '.':
            print(CountMine(i+1, j+1), end="")
        else:
            print('#', end="")
    print('\n')

Submission status: Accepted

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

S_out = [[0] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        edge0 = (i == 0)
        edge1 = (i == H - 1)
        edge2 = (j == 0)
        edge3 = (j == W -1)

        if S[i][j] == "#":
            S_out[i][j] = "#"
        else:
            S_out[i][j] = str(((S[i - 1][j - 1 + edge2:j + 2 - edge3] if not edge0 else "")
                           + S[i][j - 1 + edge2:j + 2 - edge3]
                           + (S[i + 1][j - 1 + edge2:j + 2 - edge3] if not edge1 else "")).count("#"))

print("\n".join(list(map(lambda x: "".join(x), S_out))))

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if(S[i][j] == '#'):
            continue
        count = 0
        for x in dx:
            for y in dy:
                if(not 0<=i+y<H or not 0<=j+x<W):
                    continue
                if(S[i+y][j+x] == '#'):
                    count += 1
        S[i][j] = str(count)
     
for s in S:
    print("".join(s))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = [list(input()) for i in range(h)]
#list(input())で1文字ずついじれるようにする

dx = [-1,0,1]
dy = [-1,0,1]
# dx,dyの組み合わせで8方向抑えられる

for i in range(h):
    for j in range(w):
        if S[i][j] == '#':
            continue

        count = 0
        for x in dx:
            for y in dy:
                if not 0<=i+y<h or not 0<=j+x<w:
                    continue
                #枠内のものだけ処理する
                if S[i+y][j+x] == '#':
                    count += 1
        S[i][j] = str(count)

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

Submission status: Accepted

Solution code:
from numpy import*
a,b=map(int,input().split())
c=["0"]*(b+2)
x=[list("0"+input().replace(".","0").replace("#","1")+"0")for i in range(a)]
x.insert(0,c)
x.append(c)
x=array(x).astype(int)
for s in range(1,1+a):
    tmp=""
    for t in range(1,1+b):
        if x[s][t]==1:
            tmp+="#"
        else:
            tmp+=str(sum(x[s-1:s+2,t-1:t+2]))
    print(tmp)

Submission status: Accepted

Solution code:
from numpy import*
a,b=map(int,input().split())
c=["0"]*(b+2)
x=[list("0"+input().replace(".","0").replace("#","1")+"0")for i in range(a)]
x.insert(0,c)
x.append(c)
y=zeros((a+2,b+2))
x=array(x).astype(int)
for s in range(1,1+a):
    tmp=""
    for t in range(1,1+b):
        if x[s][t]==1:
            tmp+="#"
        else:
            tmp+=str(sum(x[s-1:s+2,t-1:t+2]))
    print(tmp)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = []
def count(i,j):
    res = 0
    dx = [1,0,-1]
    dy = [1,0,-1]
    for k in range(3):
        for l in range(3):
            if 0 <= i + dy[k] <= H-1 and 0 <= j + dx[l] <= W-1:
                if S[i+dy[k]][j+dx[l]] == "#":
                    res += 1
    return res

for i in range(H):
    S.append(list(input()))
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = str(count(i,j))
for i in range(H):
    print("".join(S[i]))

Submission status: Accepted

Solution code:
""" AtCoder """

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

S = ['.' * (W + 2)]

for _ in range(H):
    S.append('.' + input() + '.')

S.append('.' * (W + 2))

x = [-1, 0, 1, 0, -1, 1, 1, -1]
y = [0, -1, 0, 1, -1, 1, -1, 1]

ans = [['#' for _ in range(W)] for _ in range(H)]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] != '#':
            count = 0
            for k in range(8):
                if S[i + y[k]][j+x[k]] == '#':
                    count += 1
            ans[i - 1][j - 1] = str(count)

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

Submission status: Accepted

Solution code:
import itertools

directions = list(itertools.product((-1, 0, 1), repeat=2))


def main():
    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 d in directions:
                if d[0] == 0 and d[1] == 0:
                    continue
                ni = i + d[0]
                nj = j + d[1]
                if 0 <= ni and 0 <= nj and ni < H and nj < W:
                    if S[ni][nj] == '#':
                        cnt += 1
            S[i][j] = str(cnt)
    for i in range(H):
        print("".join(S[i]))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(h):
    for j in range(w):

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

for i in range(h):
    for j in range(w):
        if s[i][0][j] == '#':
            a[i][j] = '#'

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

Submission status: Accepted

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

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

A = [[] for i in range(H)]
for i in range(H):
    for j in range(W):
        count = 0
        for k in range(8):
            ny = i + dy[k]
            nx = j + dx[k]
            if S[i][j]:
                if 0 <= ny < H and 0 <= nx < W:
                    if S[ny][nx] == "#":
                        count += 1
        A[i].append(str(count))

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
           A[i][j] = "#"
for i in range(H):
    print("".join(A[i]))