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())
masu = [input() for _ in range(h)]

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

for y in range(h):
  tmp = ""
  for x in range(w):
    num = 0
    if masu[y][x] == "#":
      tmp += "#"
      continue
      
    for k in range(8):
      cy = y + dy[k]
      cx = x + dx[k]
      if cy < 0 or cy >= h:
        continue
      if cx < 0 or cx >= w:
        continue
      if masu[cy][cx] == "#":
        num += 1
    tmp += str(num)
  ans.append(tmp)
  
for a in ans:
  print(a)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = [input() for i in range(h)]
ans = [["#"]*w for i in range(h)]
dx = [-1,0,1,-1,1,-1,0,1]
dy = [-1,-1,-1,0,0,1,1,1]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
        else:
            num = 0
            for k in range(8):
                ni = i + dy[k]
                nj = j + dx[k]
                if ni < 0 or h <= ni:
                    continue
                if nj < 0 or w <= nj:
                    continue
                if s[ni][nj] == "#":
                    num += 1
            ans[i][j] = str(num)
for l in ans:
    print(''.join(l))

Submission status: Accepted

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

grid = []

for h in range(H):
    inputs = input()
    _w = []
    for w in range(W):
        _w.append(inputs[w])
    grid.append(_w)

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

for i in range(H):
    for j in range(W):

        if grid[i][j] == '#': continue

        num = 0
        for k in range(8):
            ci = i + dy[k]
            cj = j + dx[k]

            if ci < 0 or H <= ci: continue
            if cj < 0 or W <= cj: continue
            if grid[ci][cj] == "#": num += 1
        
        grid[i][j] = "{}".format(num)

for g in range(len(grid)):
    print(''.join(grid[g]))

Submission status: Accepted

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

grid = []

for h in range(H):
    inputs = input()
    _w = []
    for w in range(W):
        _w.append(inputs[w])
    grid.append(_w)

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

for i in range(H):
    for j in range(W):

        if grid[i][j] == '#': continue

        num = 0
        for k in range(8):
            ci = i + dy[k]
            cj = j + dx[k]

            if ci < 0 or H <= ci: continue
            if cj < 0 or W <= cj: continue
            if grid[ci][cj] == "#": num += 1
        
        grid[i][j] = "{}".format(num)

for g in range(len(grid)):
    print(''.join(grid[g]))

Submission status: Accepted

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

ans_list = [0]*h
for i in range(h):
    ans_list_i = [0]*w
    for j in range(w):
        if map_list[i][j] == "#":
            ans_list_i[j] = "#"
            continue
        key = 0
        for a in range(i-1, i+2):
            if 0 <= a <= h-1:
                for b in range(j-1, j+2):
                    if 0 <= b <= w-1:
                        if map_list[a][b] == "#":
                            key += 1
        ans_list_i[j] = str(key)
    ans_list[i] = "".join(ans_list_i)
for i in range(h):
    print(ans_list[i])

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
l=[["."]*(b+2)]+[["."]+list(input())+["."] for _ in range(a)]+[["."]*(b+2)]
for i in range(1,a+1,1):
  for j in range(1,b+1,1):
    if l[i][j]!="#":
      l[i][j]=str(sum(l[x][y]=="#" for x in [i-1,i,i+1] for y in [j-1,j,j+1]))
for i in range(1,a+1,1):
  print("".join(l[i][1:-1]))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
f = list(list(str(input())) for i in range(H))

for i in range(H):
  for j in range(W):
    if f[i][j] == "#":
      continue
    
    count = 0
    if i > 0 and j > 0:
      if f[i-1][j-1] == "#":
        count+=1
    if i > 0:
      if f[i-1][j] == "#":
        count+=1
    if i > 0 and j < W-1:
      if f[i-1][j+1] == "#":
        count+=1
    if j > 0:
      if f[i][j-1] == "#":
        count+=1
    if j < W-1:
      if f[i][j+1] == "#":
        count+=1
    if i < H-1 and j > 0:
      if f[i+1][j-1] == "#":
        count+=1
    if i < H-1:
      if f[i+1][j] == "#":
        count+=1
    if i < H-1 and j < W-1:
      if f[i+1][j+1] == "#":
        count+=1
    
    f[i][j] = str(count)

for i in range(H):
  n = ""
  for j in range(W):
    n+=f[i][j]
  print(n)

Submission status: Accepted

Solution code:
import numpy as np

h,w = map(int, input().split())
s = list(input() for _ in range(h))

c = np.zeros((h, w))
c = c.tolist()

def minesweep(s,x, y, h, w):
    cnt = 0
    if(s[x][y] == "#"):
        return "#"
    else:
        for i in range(max(0, x-1), min(x+2, h)):
            for j in range(max(0, y-1), min(w, y+2)):
                if(s[i][j] == "#"):
                    cnt += 1
        return cnt
                
for i in range(h):
    for j in range(w):
        c[i][j] = minesweep(s, i, j, h, w)

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

Submission status: Accepted

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

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

def countUp(hi:int, wi:int):
    if hi >= 0 and hi < h and wi >= 0 and wi < w and s_list[hi][wi] == "#":
        return 1
    else:
        return 0

output_list = []
for hi in range(h):
    s = s_list[hi]
    output = ""
    for wi in range(w):
        if s[wi] == "#":
            output += "#"
        else:
            output += str(countUp(hi-1, wi-1) + countUp(hi-1, wi) + countUp(hi-1, wi+1) + countUp(hi, wi-1) + countUp(hi, wi+1) + countUp(hi+1, wi-1) + countUp(hi+1, wi) + countUp(hi+1, wi+1))

    output_list.append(output)

for o in output_list:
    print(o)

Submission status: Accepted

Solution code:
import numpy as np

H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]
#print(S)
M=np.zeros((H,W))
M=M.tolist()
def mapsearch(S,x,y,H,W):
    count=0
    if S[x][y]=="#":
        return "#"
    else:
        for i in range(max(0,x-1),min(H,x+2)):
            for j in range(max(0,y-1),min(W,y+2)):
                if S[i][j]=="#":
                    count=count+1

    return count

for i in range(H):
    for j in range(W):
        M[i][j]=mapsearch(S,i,j,H,W)
for i in M:print(*i, sep='')

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    dxs=[-1,0,1]
    dys=[-1,0,1]
    h,w=INTM()
    s=['.'*(w+2)]
    ans=[[0]*(w+2) for i in range(h+2)]
    for i in range(h):
        st='.'+STR()+'.'
        s.append(st)
    s.append('.'*(w+2))
    #print(s)
    #print(ans)
    for i in range(1,h+1):
        for j in range(1,w+1):
            if s[i][j]=='#':
                ans[i][j]='#'
                for dx in dxs:
                    for dy in dys:
                        if s[i+dx][j+dy]=='.':
                            #print(ans)
                            ans[i+dx][j+dy]=int(ans[i+dx][j+dy]+1)
    for i in range(1,h+1):
        print(*ans[i][1:w+1],sep='')
                
        
if __name__ == '__main__':
    do()

Submission status: Accepted

Solution code:
"""
author : halo2halo
date : 29, Jan, 2020
"""

import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, readline().split())
S = [readline().decode('utf8').rstrip() for _ in range(H)]

ans = [[0 if cell == '.' else '#' for cell in row] for row in S]
# print(ans)

for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            continue
        for dx, dy in itertools.product([-1, 0, 1], repeat=2):
            x = i + dx
            y = j + dy
            if x < 0 or x >= H:
                continue
            if y < 0 or y >= W:
                continue
            if S[x][y] == '#':
                continue

            ans[x][y] += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
H, W = map(int, input().split())
maze = []
for i in range(H):
    maze.append(input())
ans = [[0 for _ in range(W)] for _ in range(H)]

dx = [0, 0, 1, 1, 1, -1, -1, -1]
dy = [1, -1, 0, 1, -1, 0, 1, -1]
for i in range(H):
    for j in range(W):
        if maze[i][j] == "#":
            ans[i][j] = "#"
            continue
        cnt = 0
        for k in range(len(dx)):
            if 0 <= i + dx[k] <= H-1 and 0 <= j + dy[k] <= W-1:
                if maze[i+dx[k]][j+dy[k]] == "#":
                    cnt += 1
        ans[i][j] = str(cnt)
for i in range(H):
    print("".join(ans[i]))

Submission status: Accepted

Solution code:
from itertools import product

H, W = [int(i) for i in input().split()]
grid = [list(input()) for _ in range(H)]

for y, x in product(range(H), range(W)):
    if grid[y][x] == "#":
        continue
    n_bomb = 0
    for dy, dx in product([-1, 0, 1], repeat=2):
        if dy == 0 and dx == 0:
            continue
        ny, nx = y + dy, x + dx
        if not (0 <= ny < H and 0 <= nx < W):
            continue
        if grid[ny][nx] == "#":
            n_bomb += 1
    grid[y][x] = n_bomb
for l in grid:
    print(*l, sep="")

Submission status: Accepted

Solution code:
# 2020/01/27
# AtCoder Beginner Contest 075 - B

# Input
h, w = map(int, input().split())
slist = list()

# Make Initial Map
edge_s = ""
for i in range(w + 2):
    edge_s = edge_s + "."

# Top
slist.append(edge_s)

# Middle
for i in range(h):
    slist.append("." + input() + ".")

# Bottom
slist.append(edge_s)


# Make Answer Map
for i in range(1, h + 1):
    a_row = ""
    for j in range(1, w + 1):
        bcnt = 0
        if slist[i][j] == "#":
            a_row = a_row + "#"
        else:
            for k in range(i-1, i+2):
                for l in range(j-1, j+2):
                    if k == i and l == j:
                        continue
                    if slist[k][l] == "#":
                        bcnt = bcnt + 1
            a_row = a_row + str(bcnt)
    print(a_row)

Submission status: Accepted

Solution code:
# coding: utf-8
H,W=map(int,input().split())
picture=[]

for i in range(H):
    row=list(input())
    picture.append(row)

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):

        x=j
        y=i
        cnt=0
        if picture[y][x]=="#":
            continue
        
        for k in range(8):
            nx=x+dx[k]
            ny=y+dy[k]
            if 0<=nx<=(W-1) and 0<=ny<=(H-1):
                if picture[ny][nx]=="#":
                    cnt+=1
        picture[y][x]=str(cnt)
        
for i in range(H):
    print("".join(picture[i]))

Submission status: Accepted

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

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

ans = [[float("inf")]*(w+2) for _ in range(h+2)]

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

        b = 0
        for _dx, _dy in zip(dx, dy):
            x = i+_dx
            y = j+_dy
            if s[x][y] == "#":
                b += 1

        ans[i][j] = b

for i in range(1, h+1):
    for j in range(1, w+1):
        print("#" if ans[i][j] == float("inf") else ans[i][j], end="")
    print()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
def eight_search(x,y,maze): #i,jからmazeの周辺8つについて調べる
    cnt = 0
    for i in [-1,0,1]:
        for j in [-1,0,1]:
            if i != 0 or j != 0:
                if maze[x+i][y+j] == "#":
                    cnt += 1
    return cnt

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
h,w = map(int,input().split())
maze = [["@" for i in range(w+2)] for j in range(h+2)]
#print(maze)
for i in range(h):
    s = input()
    for j in range(w):
        maze[i+1][j+1] = s[j]
#print(maze)
for i in range(h):
    for j in range(w):
        if maze[i+1][j+1] == "#":
            print("#",end="")
        else:
            print(str(eight_search(i+1,j+1,maze)),end="")
    print()

Submission status: Accepted

Solution code:
def kazoe(x, y, mainlist):
    chousa = []
    cnt = 0
    for i, k in [(i, k) for i in range(-1, 2) for k in range(-1, 2)]:
        if len(mainlist) > x + i >= 0 and len(mainlist[x+i]) > y + k >= 0:
            chousa.append((x + i, y + k))
    for i in range(len(chousa)):
        cnt += 1 if mainlist[chousa[i][0]][chousa[i][1]] == "#" else 0
    return str(cnt)
mainlist = []
dstlist = []
x, y = map(int, input().split())
for i in range(x):
    mainlist.append(input())
dstlist = ["" for i in range(len(mainlist))]
for i, strings in enumerate(mainlist):
    for k, char in enumerate(strings):
        dstlist[i] += kazoe(i, k, mainlist) if char == "." else char
for i in dstlist:
    print(i)

Submission status: Accepted

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

t = [['#']*w for _ in range(h)]
dh = (-1, -1, -1, 0, 0, 1, 1, 1)
dw = (-1, 0, 1, -1, 1, -1, 0, 1)
for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            cnt = 0
            for y, x in zip(dh, dw):
                if 0 <= i+y < h and 0 <= j+x < w:
                    if s[i+y][j+x] == '#':
                        cnt += 1
            t[i][j] = str(cnt)

for l in t:
    print(''.join(l))

Submission status: Accepted

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

s_output = []
for i in range(h):
    s_output_row = ''
    for j in range(w):
        if s_input[i][j] == '.':
            count = 0
            for i_add in range(3):
                if (i + i_add - 1) < 0 or (i + i_add - 1) >= h:
                    continue
                for j_add in range(3):
                    if (j + j_add - 1) < 0 or (j + j_add - 1) >= w:
                        continue
                    if s_input[i + i_add - 1][j + j_add - 1] == '#':
                        count += 1
            s_output_row += str(count)
        else:
            s_output_row += s_input[i][j]
    s_output.append(s_output_row)

for s_print in s_output:
    print(s_print)

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            count = 0
            if i > 0:
                if j > 0 and S[i-1][j-1] == '#':
                    count += 1
                if S[i-1][j] == '#':
                    count += 1
                if j < W-1 and S[i-1][j+1] == '#':
                    count += 1

            if i < H-1:
                if j > 0 and S[i+1][j-1] == '#':
                    count += 1
                if S[i+1][j] == '#':
                    count += 1
                if j < W-1 and S[i+1][j+1] == '#':
                    count += 1

            if j > 0 and S[i][j-1] == '#':
                count += 1
            if j < W-1 and S[i][j+1] == '#':
                count += 1
            S[i][j] = str(count)
    print(''.join(S[i]))

Submission status: Accepted

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

field = [["."]*(W+2)]
for i in range(H):
    field.append(["."])
    field[-1].extend(list(input()))
    field[-1].append(".")
field.append(["."]*(W+2))

for i in range(1,H+1):
    for j in range(1,W+1):
        if field[i][j] == "#":
            print("#",end="")
        else:
            cnt = 0
            for x in [-1,0,1]:
                for y in [-1,0,1]:
                    if field[i+y][j+x] == "#":
                        cnt += 1
            print(cnt,end="")
    print("")

Submission status: Accepted

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

for y in range(H):
    for x in range(W):
        count = 0
        if filed[y][x] == '#':
            continue
        for k in range(8):
            ny = y + dy[k]
            nx = x + dx[k]
            if ny < 0 or H <= ny or nx < 0 or W <= nx:
                continue
            if filed[ny][nx] == '#':
                count += 1
        filed[y][x] = str(count)

for y in range(H):
    print("".join(filed[y]))

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        cnt = 0
        for m, n in zip(y, x):
            if 0 <= i + m < h and 0 <= j + n < w:
                if s[i + m][j + n] == '#':
                    cnt += 1

        if s[i][j] == '#':
            print('#', end='')
        else:
            print(cnt, end='')
    print()

Submission status: Accepted

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

ans = [[-1 for i in range(w)] for j in range(h)]

for i in range(h):
    for j in range(w):
        if v[i][j] == '#':
            continue
        cnt = 0
        for dy in range(-1, 2):
            for dx in range(-1, 2):
                if dy == 0 and dx == 0:
                    continue
                ny = i + dy
                nx = j + dx
                if ny < 0 or ny >= h or nx < 0 or nx >= w:
                    continue
                if v[ny][nx]=='#':
                    cnt+=1
        ans[i][j] = cnt

for i in range(h):
    for j in range(w):
        if j==w-1:
            if ans[i][j]==-1:
                print('#')
            else:
                print(ans[i][j])
        else:
            if ans[i][j]==-1:
                print('#',end="")
            else:
                print(ans[i][j],end="")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
graph = [list(input()) for _ in range(H)]
onesteps=((1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1))
for x in range(H):
    for y in range(W):
        count=0
        if graph[x][y]==".":
            for dx,dy in onesteps:
                if (0<=x+dx<H)and(0<=y+dy<W):
                    if graph[x+dx][y+dy]=="#":
                        count+=1
            graph[x][y]=str(count)
for line in graph:
    print("".join(line))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
graph = [list(input()) for _ in range(H)]
one_steps = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, 1), (1, -1), (-1, -1))
for x in range(H):
    for y in range(W):
        count = 0
        if graph[x][y] == ".":
            for dx, dy in one_steps:
                if (0 <= x + dx < H) and (0 <= y + dy < W):
                    if graph[x + dx][y + dy] == "#":
                        count += 1
            graph[x][y] = str(count)


for line in graph:
    print("".join(line))

Submission status: Accepted

Solution code:
#python3
around = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]

def main():
    h, w = map(int, input().split())
    a = [input() for _ in range(h)]
    field = [list(a[i]) for i in range(h)]

    for si in range(h):
        for sj in range(w):
            if field[si][sj] == '#':
                continue 
            cnt = 0
            for di, dj in around:
                i = si + di
                j = sj + dj  

                if (i < 0) | (i >= h) | (j < 0) | (j >= w) :
                    continue
                if field[i][j] == '.':
                    continue
                if field[i][j] == '#':
                    cnt += 1
            
            field[si][sj] = str(cnt)

    for i in field:
        print(''.join(i))  
     
main()

Submission status: Accepted

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

def f(a,b):
    for i in lst:
        for j in lst:
            th = a+i
            tw = b+j
            if 0 <= th <= h-1 and 0 <= tw <= w-1 and s[th][tw] != "#":
                ans[th][tw] += 1 

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            f(i,j)
            ans[i][j] = "#"

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

Submission status: Accepted

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

def f(a,b):
    for i in lst:
        for j in lst:
            th = a+i
            tw = b+j
            if not (0 <= th <= h-1 and 0 <= tw <= w-1 and s[th][tw] != "#"):
                continue
            ans[th][tw] += 1 

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            f(i,j)
            ans[i][j] = "#"

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

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]==".":
      x=[(i+1,j+1),(i,j+1),(i-1,j+1),(i+1,j),(i-1,j),(i+1,j-1),(i,j-1),(i-1,j-1)]
      x = [(p,q) for p,q in x if min(p,q)>=0 and p<h and q<w]
      y=0
      for p,q in x:
        if s[p][q]=="#":
          y+=1
      s[i][j]=str(y)
for i in range(h):
  print("".join(s[i]))

Submission status: Accepted

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


diffX=[1,1,1,0,0,-1,-1,-1]
diffY=[0,-1,1,1,-1,-1,0,1]
tmpL=[[0]*W]*H

for i in range(H):
    tmp=""
    for j in range(W):
        cnt=0
        if L[i][j]=='.':
            for p in range(0,8):
                if (i+diffY[p]<0 or i+diffY[p]>H-1):
                    cnt+=0
                elif (j+diffX[p]<0 or j+diffX[p]>W-1):
                    cnt+=0
                elif L[int(i+diffY[p])][int(j+diffX[p])]=='#':
                    cnt+=1
            tmpL[i][j]=cnt
        else:
            tmpL[i][j]='#'

        tmp=tmp+str(tmpL[i][j])
    print(tmp)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input()+"." if i!=h else "."*(w+1) for i in range(h+1)]
def count_8(i,j):
    global h,w,s
    cnt=str((s[i-1][j-1]=="#")+(s[i-1][j+1]=="#")+(s[i+1][j-1]=="#")+(s[i+1][j+1]=="#") \
        +(s[i-1][j]=="#")+(s[i][j-1]=="#")+(s[i+1][j]=="#")+(s[i][j+1]=="#"))
    return cnt
for i in range(h):
    s_sub=""
    for j in range(w):
        if s[i][j]=="#":
            s_sub+="#"
        else:
            s_sub+=count_8(i,j)
    print(s_sub)

Submission status: Accepted

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


def count_bomb(x, y):
    bomb = 0
    one_steps = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1))
    for dx, dy in one_steps:
        nx, ny = x + dx, y + dy
        if (0 <= nx < H) and (0 <= ny < W):
            if graph[nx][ny] == "#":
                bomb += 1
    return bomb


for x in range(H):
    for y in range(W):
        if graph[x][y] == ".":
            graph[x][y] = str(count_bomb(x, y))
for line in graph:
    print("".join(line))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
T = ["" for _ in range(H)]
for h in range(H):
    for w in range(W):        
        T[h] += str(sum(S[i][j]=="#" for i in range(max(0,h-1),min(H,h+2)) for j in range(max(0,w-1),min(W,w+2)))) if S[h][w] == "." else "#"
print(*T, sep="\n")

Submission status: Accepted

Solution code:
import sys
import numpy as np



class grid:
  def __init__(self, debug=False):
    # admit stdin
    hw = input().split()
    self.H = int(hw[0])
    self.W = int(hw[1])

    self.circuit = [[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1]]
    
    # char decomp, loop accessed as g[row][col]
    self.g = [[self.__rep2int(c) for c in input()] for j in range(self.H)]
    if debug:
      print(self.g)

  def __rep2int(self, c):
    return 0 if c=='.' else 1

  def __onBomb(self, i, j):
    return self.g[i][j]==1

  def __circuitSum(self, i, j, array):
    val = 0
    for delta in self.circuit:
      I = i+delta[0]
      J = j+delta[1]
      # if the circuit contains the coordinate out of the grid, do nothing
      if any([I<0, I>=self.H]) or any([J<0, J>=self.W]):
        continue
      else:
        val += array[I][J]
    return val

  def CountBombs(self):
    # tempg = ['' for j in range(self.H)]
    out_text = ''
    # sum
    for i in range(self.H):
      for j in range(self.W):
        # tempg[i] += '#' if self.__onBomb(i,j) else str(self.__circuitSum(i,j,self.g))
    # return tempg
        out_text += '#' if self.__onBomb(i,j) else str(self.__circuitSum(i,j,self.g))
      out_text += '\n'
    return out_text

g=grid()
print(g.CountBombs())

Submission status: Accepted

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

ans =[]
dx = [1, 0, -1,0,1,-1,-1,1]
dy = [0,1,0,-1,1,1,-1,-1]
for i in range(a):
    ans1=[]
    for j in range(b):
        if s[i][j]=="#":
            ans1.append('#')
            continue

        cnt=0
        for k in range(8):
            if (dx[k]+j>=0 and dy[k]+i>=0 and dx[k]+j<b and dy[k]+i<a):
                if (s[dy[k]+i][dx[k]+j]=='#'):
                    cnt+=1
        ans1.append(str(cnt))
    ans.append(ans1)

for i in range(a):
    for j in range(b):
        print(ans[i][j],end='')

    print()

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
s=[]
for i in range(a):
    s.append(input())
ans=[]
dx=[1, 0, -1, 0, 1, -1, -1, 1]
dy=[0, 1, 0, -1, 1, 1, -1, -1]
for i in range(a):
    anssss=[]
    for j in range(b):
        if s[i][j]=="#":
            anssss.append("#")
            continue
        count=0
        for k in range(8):
            if (dx[k]+j>=0 and dy[k]+i>=0 and dx[k]+j<b and dy[k]+i<a):
                if (s[dy[k]+i][dx[k]+j]=="#"):
                    count+=1
        anssss.append(str(count))
    ans.append(anssss)
for i in range(a):
    for j in range(b):
        print(ans[i][j],end="")
    print()

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
s=[]*a
for i in range(a):
    s.append(input())
ans=[]
dx=[1, 0, -1, 0, 1, -1, -1, 1]
dy=[0, 1, 0, -1, 1, 1, -1, -1]
for i in range(a):
    anssss=[]
    for j in range(b):
        if s[i][j]=="#":
            anssss.append("#")
            continue
        count=0
        for k in range(8):
            if (dx[k]+j>=0 and dy[k]+i>=0 and dx[k]+j<b and dy[k]+i<a):
                if (s[dy[k]+i][dx[k]+j]=="#"):
                    count+=1
        anssss.append(str(count))
    ans.append(anssss)
for i in range(a):
    for j in range(b):
        print(ans[i][j],end="")
    print()

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
s = []*H
flag = 0
tmp = 0
ans = ''
t = [-1,0,1]
for i in range(H):
    s.append(input())

for i in range(H):
    ans =''
    for j in range(W):
        if s[i][j] == '.':
            tmp = 0
            for k in t:
                for m in t:
                    try:
                        if s[i-k][j-m] == '#' and i-k != -1 and j-m != -1:
                            tmp += 1
                    except IndexError:
                        pass
            ans += str(tmp)
        else:
            ans += '#'
    print(ans)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
def count_8(i,j):
    global h,w,s
    cnt=0
    if i-1>=0 and j-1>=0:
        if s[i-1][j-1]=="#":
            cnt+=1
    if i-1>=0 and j+1<=w-1:
        if s[i-1][j+1]=="#":
            cnt+=1
    if i+1<=h-1 and j-1>=0:
        if s[i+1][j-1]=="#":
            cnt+=1
    if i+1<=h-1 and j+1<=w-1:
        if s[i+1][j+1]=="#":
            cnt+=1
    if i-1>=0:
        if s[i-1][j]=="#":
            cnt+=1
    if j-1>=0:
        if s[i][j-1]=="#":
            cnt+=1
    if i+1<=h-1:
        if s[i+1][j]=="#":
            cnt+=1
    if j+1<=w-1:
        if s[i][j+1]=="#":
            cnt+=1
    return str(cnt)
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            print("#",end="")
        else:
            print(count_8(i,j),end="")
    print()