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=[]
cnt=0
for i in range(h):
    s.append(list(input()))
if h==1 and w==1:
    if s[0][0]=='#':
        print('#')
    else:
        print(0)
elif h==1:
    for i in range(w):
        if s[0][i]=='#':
            continue
        if i==0:
            if s[0][i+1]=='#':
                cnt+=1
            s[0][i]=str(cnt)
            cnt=0
        elif i==w-1:
            if s[0][i-1]=='#':
                cnt+=1
            s[0][i]=str(cnt)
            cnt=0
        else:
            if s[0][i+1]=='#':
                cnt+=1
            if s[0][i-1]=='#':
                cnt+=1
            s[0][i]=str(cnt)
            cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''
elif w==1:
    for i in range(h):
        if s[i][0]=='#':
            continue
        if i==0:
            if s[i+1][0]=='#':
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
        elif i==h-1:
            if s[i-1][0]=='#':
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
        else:
            if s[i+1][0]=='#':
                cnt+=1
            if s[i-1][0]=='#':
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''
else:
    for j in range(h):
        for k in range(w):
            if j==0:
                if s[j][k]=='#':
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k+1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k+1]=='#':
                            cnt+=1
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j+1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
            elif j==h-1:
                if s[j][k]=='#':
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k+1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k+1]=='#':
                            cnt+=1
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j-1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0 
            elif 0<j<h-1:
                if s[j][k]=='#':
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k+1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k+1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k-1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k+1]=='#':
                            cnt+=1
                        if s[j-1][k-1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k+1]=='#':
                            cnt+=1
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j+1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
S = [list(input()) for i in range(H)]
S=[[0 if j == "." else "#" for j in i] for i in S]

for i in range(H):
          for j in range(W):
            if S[i][j]=='#':

                try:S[i-1][j-1]
                except IndexError:pass
                else:
                    if  i-1>=0 and j-1>=0 and S[i-1][j-1]!='#':
                        S[i-1][j-1]+=1

                try:S[i-1][j]
                except IndexError:pass
                else:
                    if i-1>=0 and S[i-1][j]!='#':
                        S[i-1][j]+=1

                try:S[i-1][j+1]
                except IndexError:pass
                else:
                    if i-1>=0 and S[i-1][j+1]!='#':
                        S[i-1][j+1]+=1

                try:S[i][j-1]
                except IndexError:pass
                else:
                    if  j-1>=0 and S[i][j-1]!='#':
                        S[i][j-1]+=1
                        
                try:S[i][j+1]
                except IndexError:pass
                else:
                    if  S[i][j+1]!='#':
                        S[i][j+1]+=1

                try:S[i+1][j-1]
                except IndexError:pass
                else:
                    if j-1>=0 and S[i+1][j-1]!='#':
                        S[i+1][j-1]+=1

                try:S[i+1][j]
                except IndexError:pass
                else:
                    if S[i+1][j]!='#':
                        S[i+1][j]+=1

                try:S[i+1][j+1]
                except IndexError:pass
                else:
                    if S[i+1][j+1]!='#':
                        S[i+1][j+1]+=1
for i in S:
    print(*i,sep="")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
S.insert(0,[0]*W)
S.append([0]*W)
for i in range(len(S)):
    S[i].insert(0,0)
    S[i].append(0)
dx = [1,0,-1,0,1,-1,-1,1]
dy = [0,1,0,-1,1,1,-1,-1]
tmp = [[0] * W for i in range(H)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == "#":
            tmp[i-1][j-1] = "#"
            continue
        num = 0
        for k in range(len(dx)):
            if S[i+dx[k]][j+dy[k]] == "#":
                num += 1
                tmp[i-1][j-1] = num
for j in range(len(tmp)):
    print(*tmp[j],sep = "")

Submission status: Accepted

Solution code:
# ABC 075: B – Minesweeper
h, w = [int(s) for s in input().split()]
s = [input() for _ in range(h)]

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

            if i - 1 >= 0 and s[i - 1][j] == '#':
                bombs += 1
            if i - 1 >= 0 and j - 1 >= 0 and s[i - 1][j - 1] == '#':
                bombs += 1
            if j - 1 >= 0 and s[i][j - 1] == '#':
                bombs += 1
            if i + 1 < h and j - 1 >= 0 and s[i + 1][j - 1] == '#':
                bombs += 1
            if i + 1 < h and s[i + 1][j] == '#':
                bombs += 1
            if i + 1 < h and j + 1 < w and s[i + 1][j + 1] == '#':
                bombs += 1
            if j + 1 < w and s[i][j + 1] == '#':
                bombs += 1
            if i - 1 >= 0 and j + 1 < w and s[i - 1][j + 1] == '#':
                bombs += 1
        
            s[i] = s[i][:j] + str(bombs) + s[i][j + 1:]

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

Submission status: Accepted

Solution code:
dx=[-1,0,1,-1,1,-1,0,1]
dy=[-1,-1,-1,0,0,1,1,1]
h,w=map(int,input().split())
field = []
for i in range(h):
    field.append(list(input()))
for y in range(h):
    for x in range(w):
        if field[y][x]=="#":
            continue
        cnt=0
        for k in range(8):
            nx=x+dx[k]
            ny=y+dy[k]
            if nx<0 or nx>=w or ny<0 or ny>=h:
                continue
            if field[ny][nx]=="#":
                cnt+=1
        field[y][x]=str(cnt)
for i in range(h):
    print("".join(field[i]))

Submission status: Accepted

Solution code:
H,W = map(int,input().split(' '))
N = [[i for i in input()] for i in range(H)]
lst = [-1,0,1]
for i in range(H):
    for x in range(W):
        if N[i][x] == '.':
            ans = 0
            for i1 in lst:
                for x1 in lst:
                    if (i+i1 >= 0 and x+x1 >= 0) and (i+i1 < H and x+x1 < W):
                        if N[i+i1][x+x1] == "#":
                            ans = ans + 1
            N[i][x] = ans

[print(''.join([str(x) for x in i])) for i in N]

Submission status: Accepted

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

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

for i in range(1,H+1):
    S = list(input())
    S_list[i][1:W+1] = S
    
ans = [["#" for i in range(W+2)] for j in range(H+2)]
    
for i in range(1,H+1):
    for j in range(1,W+1):
        if S_list[i][j] != "#":            
            ans[i][j] = S_list[i-1][j-1:j+2].count("#") + S_list[i][j-1:j+2].count("#") + S_list[i+1][j-1:j+2].count("#")
            
for i in range(1,H+1):
    print(''.join(map(str, ans[i][1:-1])))

Submission status: Accepted

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


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


def f(i, j):
    x = 0
    for h in range(max(0, i-1), min(i+2, H)):
        for w in range(max(0, j-1), min(j+2, W)):
            if S[h][w] == "#":
                x += 1
    return x


def main():
    for y in range(H):
        s = ""
        for x in range(W):
            if S[y][x] == ".":
                s += str(f(y, x))
            else:
                s += "#"
        print(s)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
ls=[]
ls.append("."*(W+2))
for i in range(H):
    ls.append("."+input()+".")
ls.append("."*(W+2))
lans=[]
for i in range(1,H+1):
    clans=""
    for j in range(1,W+1):
        if ls[i][j]==".":
            clans+=str((ls[i-1][j-1]=="#")+(ls[i-1][j]=="#")+(ls[i-1][j+1]=="#")+(ls[i][j-1]=="#")+(ls[i][j+1]=="#")+(ls[i+1][j-1]=="#")+(ls[i+1][j]=="#")+(ls[i+1][j+1]=="#"))
        else:
            clans+=ls[i][j]
    lans.append(clans)
for i in range(H):
    print(lans[i])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
ss = []
ls = []

# 一回り大きくする
ss.append(['.']*(w+2))
for _ in range(h):
    ss.append(['.']+list(input())+['.'])
ss.append(['.']*(w+2))

for i in range(1, h+1):
    for j in range(1, w+1):
        if ss[i][j] == '.':
            n = (
                ss[i-1][j-1],
                ss[i-1][j],
                ss[i-1][j+1],
                ss[i][j-1],
                ss[i][j+1],
                ss[i+1][j-1],
                ss[i+1][j],
                ss[i+1][j+1],
            ).count('#')
            print(n, end='')
        else:
            print('#', end='')
    print()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
for y in range(H):
    for x in range(W):
        if S[y][x] == ".":
            cnt = 0
            for c in range(8):
                nx = x + dx[c]
                ny = y + dy[c]
                if 0 <= nx <= W-1 and 0<= ny <= H-1:
                    if S[ny][nx] == "#":
                        cnt += 1
            S[y][x] = str(cnt)
for i in range(H):
    print(("".join(S[i])))

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
data=[list(input()) for i in range(H)]
for w in range(W):
    for h in range(H):
        if data[h][w]==".":
            xlist=[0]
            if w!=0:xlist+=[-1]
            if w!=W-1:xlist+=[1]
            ylist=[0]
            if h!=0:ylist+=[-1]
            if h!=H-1:ylist+=[1]
            data[h][w]=str([data[h+y][w+x] for x in xlist for y in ylist].count("#"))
for i in data:
    print("".join(i))

Submission status: Accepted

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

def count(T,h,w):
    re=0
    re+=[T[h+1][w],T[h+1][w+1],T[h+1][w-1],T[h][w+1],T[h][w-1],\
         T[h-1][w],T[h-1][w+1],T[h-1][w-1]].count('#')
    return re

ans=['']*(H+2)
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=='#':
            ans[i]+='#'
        else:
            ans[i]+=str(count(S,i,j))
            
for i in ans:
    if i!='':
        print(i)

Submission status: Accepted

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

mines=['' for _ in range(h)]

#print(board)

for row in range(h):
    for col in range(w):
        if board[row][col] != '#':
            counts = 0
            dx = [x for x in range(-1, 2) if (col+x>=0 and col+x<w)]
            dy = [y for y in range(-1, 2) if (row+y>=0 and row+y<h)]
            #print('row:{}, col:{}, dx:{}, dy:{}'.format(row,col,dx,dy))
            for x in dx:
                for y in dy:
                    #print('row+y:{}, col+x:{}'.format(row+y, col+x))
                    if board[row+y][col+x] == '#':
                        counts += 1
            mines[row] += str(counts)
        else:
            mines[row] += '#'
for mine in mines:
    print(mine)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
from bisect import bisect_left,bisect_right
import sys,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

def check(x,y):
    res = 0
    for j in dy:
        for i in dx:
            if s[y][x] == '#':
                return '#'
            if x + i >= w or y + j >= h or x + i < 0 or y + j < 0:
                continue
            if s[y+j][x+i] == '#':
                res += 1
    return res
dx = [-1,0,1]
dy = [-1,0,1]
h,w = inpl()
s = [''] * h
for i in range(h):
    s[i] = input()
res = []
for y in range(h):
    L = ''
    for x in range(w):
        L += str(check(x,y))
    res.append(L)
for i in range(h):
    print(res[i])

Submission status: Accepted

Solution code:
h,w=[int(i) for i in input().split()]
s=[input() for i in range(h)]
ans=""
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="#":
            ans+="#"
        else:
            for y,x in [(i+1,j-1),(i+1,j),(i+1,j+1),(i-1,j-1),(i-1,j),(i-1,j+1),(i,j+1),(i,j-1)]:
                if x>=0 and y>=0 and y<h and x<w:
                    if s[y][x]=="#":
                        cnt+=1
            ans+=str(cnt)
    ans+="\n"
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
Slist = [["."] * (W + 2), ]
for _ in range(H):
    line = list(input())
    line.insert(0, ".")
    line.append(".")
    Slist.append(line)
Slist.append(["."] * (W + 2))
# print(Slist)
point_tuple = ((-1, -1), (-1, 0), (-1, 1),
               (0, -1), (0, 1),
               (1, -1), (1, 0), (1, 1))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        # print(Slist[i][j], i, j)
        if Slist[i][j] == ".":
            counter = 0
            for a, b in point_tuple:
                if Slist[i - a][j - b] == "#":
                    counter += 1
            Slist[i][j] = str(counter)

for i in range(1, H + 1):
    print("".join(Slist[i][1:-1]))

Submission status: Accepted

Solution code:
import sys
from collections import deque

h, w = (int(x) for x in sys.stdin.readline().split())
strings = deque(sys.stdin.read().split())

for i in range(h):
    strings[i] = ''.join(['-', strings[i], '-'])
strings.append('-' * (w + 2)); strings.appendleft('-' * (w + 2))

ans = []
for i in range(1, h + 1):
    s = ''
    for j in range(1, w + 1):
        current = strings[i][j]
        if current == '#': s += '#'
        elif current == '.':
            count = 0
            for t in range(i-1, i+2):
                for u in range(j-1, j+2):
                    if strings[t][u] == '#': count += 1
            s += str(count)
    ans.append(s)

for s in ans: print(s)

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
s = [input() for i in range(H)]
for i in range(H):
    l = ""
    for j in range(W):
        if s[i][j] == "#":
            l += "#"
        else:
            l += str(sum([t[max(0,j-1):min(W,j+2)].count("#") for t in s[max(0,i-1):min(H,i+2)]]))
    print(l)

Submission status: Accepted

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

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

eight_dir = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))

for i in range(H):
    cur_str = ''
    for j in range(W):
        if s_list[i][j] == '#':
            cur_str += "#"
        else:
            count = 0
            for k in range(8):
                search_x = j + eight_dir[k][1]
                search_y = i + eight_dir[k][0]
                if 0 <= search_x < W and 0 <= search_y < H:
                    if s_list[search_y][search_x] == '#':
                        count += 1
            cur_str += str(count)
#     out_list.append(cur_str)
    print(cur_str)

Submission status: Accepted

Solution code:
h, w = map(int,input().split())
s = [input() for i in range(h)]
for i in range(h):
  l = ""
  for j in range(w):
    if s[i][j] == "#":
      l += "#"
    else:
      l += str(sum([t[max(0,j-1):min(w,j+2)].count("#") for t in s[max(0,i-1):min(h,i+2)]]))
  print(l)

Submission status: Accepted

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

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

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

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

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

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[input() for _ in range(H)]
A=[]
for i in range(H):
    tmp=''
    for j in range(W):
        if S[i][j]=='#':
            tmp+='#'
        else:
            cnt=0
            for m in range(max(i-1,0),min(i+1,H-1)+1):
                for n in range(max(j-1,0),min(j+1,W-1)+1):
                    if S[m][n]=='#':
                        cnt+=1
            tmp+=str(cnt)
    A.append(tmp)
for a in A:
    print(a)

Submission status: Accepted

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


def check_num(i, j):
    a = i - 1
    b = i + 1
    c = j - 1
    d = j + 1
    if i == 0:
        a = i
    if i == h-1:
        b = i
    if j == 0:
        c = j
    if j == w-1:
        d = j
    count = 0
    for p in range(a, b+1):
        for q in range(c, d+1):
            if p == i and q == j:
                continue
            if mine_area[p][q] == '#':
                count += 1
    return count


for _ in range(h):
    mine_line = list(input())
    mine_area.append(mine_line)

for i in range(h):
    for j in range(w):
        if mine_area[i][j] == '#':
            mine_area[i][j] = '#'
        else:
            mine_area[i][j] = check_num(i, j)

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

Submission status: Accepted

Solution code:

import numpy as np
h, w = map(int, input().rstrip("\n").split())

array = np.array([[" "] * (w + 2)] + [list(" " + input().rstrip("\n") + " ") for _ in range(h)] + [[" "] * (w + 2)])

list_ = []

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if array[i][j] == "#":
            list_.append("#")
        else:
            list_.append(str(sum(sum(array[i - 1:i + 2, j - 1:j + 2] == "#"))))
    list_.append("\n")
list_.pop()

print("".join(list_))

Submission status: Accepted

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

temp = ['.'*(w+2)]+['.'+input()+'.' for _ in range(h)]+['.'*(w+2)]

# print(temp)

ans_list = []

for h_i in range(1, h+1):
  w_list = []
  for w_i in range(1, w+1):
    if temp[h_i][w_i] == '.':
      top_1 = temp[h_i-1][w_i-1]
      top_2 = temp[h_i-1][w_i]
      top_3 = temp[h_i-1][w_i+1]
      left = temp[h_i][w_i-1]
      right = temp[h_i][w_i+1]
      bottom_1 = temp[h_i+1][w_i-1]
      bottom_2 = temp[h_i+1][w_i]
      bottom_3 = temp[h_i+1][w_i+1]

      count = [top_1,top_2,top_3,left,right,bottom_1,bottom_2,bottom_3].count('#')
      w_list.append(count)
    else:
      w_list.append('#')
   
  ans_list.append(w_list)

for ans_i in ans_list:print(*ans_i, sep='')

Submission status: Accepted

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

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

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if g[i][j] == ".":
            c = 0
            for (dx, dy) in d:
                if g[i + dx][j + dy] == "#":
                    c += 1
            g[i][j] = c
for i in range(1, H + 1):
    print(*g[i][1:(W + 1)], sep="")

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
fd = [list(input()) for i in range(h)]
	
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):
		if fd[y][x] == '#':
			continue
		c = 0
		
		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 fd[ny][nx] == '#':
				c += 1
		fd[y][x] = str(c)
		
for y in range(h):
	print(''.join(fd[y]))

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            c = 0
            for k in range(max(i-1, 0), min(i+1, h-1)+1):
                for l in range(max(j-1, 0), min(j+1, w-1)+1):
                    if s[k][l] == '#':
                        c += 1
            s[i][j] = str(c)

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

Submission status: Accepted

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

import itertools

for i in range(h):
  ans=""
  for j in range(w):
    if s[i][j]=="#":
      ans+="#"
      continue
    temp=0
    for dx,dy in itertools.product([-1,0,1],repeat=2):
      if i+dx<0 or i+dx>=h:
        continue
      if j+dy<0 or j+dy>=w:
        continue
      if s[i+dx][j+dy]=="#":
        temp+=1
    ans+=str(temp)
  print(ans)

Submission status: Accepted

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


for i in range(1, h+1):
  for j in range(1, w+1):
    counter = 0
    if S[i][j] == ".":
      if S[i-1][j-1] == "#":
        counter += 1
      if S[i-1][j] == "#":
        counter += 1
      if S[i-1][j+1] == "#":
        counter += 1
      if S[i][j-1] == "#":
        counter += 1
      if S[i][j+1] == "#":
        counter += 1
      if S[i+1][j-1] == "#":
        counter += 1
      if S[i+1][j] == "#":
        counter += 1
      if S[i+1][j+1] == "#":
        counter += 1
      S[i][j] = counter

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:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict

H, W = (int(i) for i in sys.stdin.readline().rstrip().split())
S = (sys.stdin.readline().rstrip() for _ in range(H))

dd = {}
for i, x in enumerate(S):
  for j, y in enumerate(x):
    dd[(i, j)] = 1 if y == '#' else 0

all_pos = {}
for pos in sorted(dd.keys()):
  x, y = pos
  state = 0
  state += dd.get((x + 1, y), 0)
  state += dd.get((x - 1, y), 0)
  state += dd.get((x, y + 1), 0)
  state += dd.get((x, y - 1), 0)
  state += dd.get((x + 1, y + 1), 0)
  state += dd.get((x - 1, y - 1), 0)
  state += dd.get((x + 1, y - 1), 0)
  state += dd.get((x - 1, y + 1), 0)
  all_pos[(x, y)] = state
  
for i in range(H):
  for j in range(W):
    if dd[(i, j)]:
      print('#', end='')
    else:
      print(all_pos[(i, j)], end='')
  print()

Submission status: Accepted

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

def check_bomb(x, y):
    cnt = 0
    if S[y][x] == '#':
        return '#'
    if x-1 >= 0:
        if S[y][x-1] == '#':
            cnt += 1
    if x+1 < W:
        if S[y][x+1] == '#':
            cnt += 1
    if y-1 >= 0:
        if x-1 >= 0:
            if S[y-1][x-1] == '#':
                cnt += 1
        if x+1 < W:
            if S[y-1][x+1] == '#':
                cnt += 1
        if S[y-1][x] == '#':
                cnt += 1
    if y+1 < H:
        if x-1 >= 0:
            if S[y+1][x-1] == '#':
                cnt += 1
        if x+1 < W:
            if S[y+1][x+1] == '#':
                cnt += 1
        if S[y+1][x] == '#':
                cnt += 1
    return str(cnt)

for y in range(H):
    for x in range(W):
        print(check_bomb(x, y), end='')
    print('')