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 = [int(x) for x in input().split()]
s_list = [input() for _ in range(h)]

def count_bomb_num(i, j):
    bomb_num = 0
    for k in range(-1, 2):
        if j == 0 and k == -1:
            continue
        if j == w - 1 and k == 1:
            continue
        if s_list[i][j + k] == "#":
            bomb_num += 1
    return bomb_num

count_list = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if i == 0:
            count_list[i][j] = count_bomb_num(i, j)
        if i < h - 1:
            count_list[i + 1][j] = count_bomb_num(i + 1, j)
        
        if s_list[i][j] == "#":
            print("#", end="")
        else:
            bomb_num = count_list[i][j]
            if i > 0:
                bomb_num += count_list[i - 1][j]
            if i < h - 1:
                bomb_num += count_list[i + 1][j]
            print(bomb_num, end="")
    print()

Submission status: Accepted

Solution code:
import numpy as np

H, W = list(map(int, input().split()))
S = ["." + input() + "." for i in range(H)]
S = ["." * (W + 2)] + S + ["." * (W + 2)]
# print(H)
# print(W)
a = []
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == ".":
            # print(i, j, S[i][j])
            tmp = 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]
            c = str(tmp.count("#"))
            a.append(c)
        else:
            a.append("#")

a = np.reshape(a, (H, W))
for raw in a:
    ls = ""
    for item in raw:
        ls += item
    print(ls)

Submission status: Accepted

Solution code:
import numpy as np
from scipy import signal
H, W = list(map(int, input().split()))
com = np.full((1,W),'#')
Mat = np.zeros((H,W))
MAP = np.empty((H,W),int)
mask = [[1,1,1],
        [1,0,1],
        [1,1,1]]
for i in range(H):
    Mat[i] = list(input())==com

MAP = signal.convolve2d(Mat,mask,'same')
outMAP = np.where(Mat,'#',MAP.astype('int'))

for j in outMAP:print(*j,sep='')
# print(MAP)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
zlst = [0] * (W + 2)
b1 = []
b1.append(zlst)
for h in range(H):
  lst1 = [0]
  str1 = input()
  for s in str1:
    if s == '.':
      lst1.append(0)
    else:
      lst1.append(1)
  lst1.append(0)
  b1.append(lst1)
b1.append(zlst)

#nboard = [[] for i in range(H)]
for i in range(H):
  nstr = ''
  for j in range(W):
    if b1[i + 1][j + 1] == 1:
      nstr = nstr + '#'
    else:
      nstr = nstr + str(
        b1[i][j] + b1[i][j+1] + b1[i][j+2] + b1[i+1][j] + 
        b1[i+1][j+2] + b1[i+2][j] + b1[i+2][j+1] + b1[i+2][j+2])
  print(nstr)

Submission status: Accepted

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

for i in range(h):
    result = []
    for j in range(w):
        if s[i][j] == '#':
            result.append('#')
        elif s[i][j] == '.':
            cnt = 0
            for y in range(-1, 2):
                for z in range(-1, 2):
                    if 0 <= i+y <= h-1:
                        if 0 <= j+z <= w-1:
                            if s[i+y][j+z] == '#':
                                cnt += 1
            result.append(str(cnt))
    print(*result, sep='')

Submission status: Accepted

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

def dfs(x,y):
    if 0 <= x < h and 0 <= y < w:
        if ans[x][y] != "#":
            ans[x][y] += 1
        

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i][j] = '#'
            dfs(i+1, j)
            dfs(i+1, j+1)
            dfs(i+1, j-1)
            dfs(i-1, j+1)
            dfs(i-1, j)
            dfs(i-1, j-1)
            dfs(i, j+1)
            dfs(i, j-1)

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

Submission status: Accepted

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

Submission status: Accepted

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

Rmin = 0
Rmax = 0
Cmin = 0
Cmax = 0

#行方向への探索
for r in range(H):
    #列方向への検索
    for c in range(W):
        #爆弾の場合、周りを加算
        if S[r][c] == ".":
            S[r][c] = 0
        elif S[r][c] == "#":
            if r == 0 and H == 1:
                Rmin = r
                Rmax = 1
            elif r == 0:    
                Rmin = r
                Rmax = r+2
            elif r == H-1:
                Rmin = r-1
                Rmax = r+1
            else:
                Rmin = r-1
                Rmax = r+2

            if c == 0 and W ==1 :
                Cmin = c
                Cmax = 1
            elif c == 0:
                Cmin = c
                Cmax = c+2
            elif c == W-1:
                Cmin = c-1
                Cmax = c+1
            else:
                Cmin = c-1
                Cmax = c+2
            for i in range(Rmin,Rmax):
                for t in range(Cmin,Cmax):
                    if S[i][t] == "." or S[i][t] == 0:
                        S[i][t] = 1
                    elif not S[i][t] == "#":
                        S[i][t] += 1

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

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] != "#":
            num = 0
            
            if i > 0:
                if j > 0:
                    if S[i-1][j-1] == "#":
                        num += 1
                if S[i-1][j] == "#":
                    num += 1
                if j < W-1:
                    if S[i-1][j+1] == "#":
                        num += 1
            if j > 0:
                if S[i][j-1] == "#":
                    num += 1
            if j < W-1:
                if S[i][j+1] == "#":
                    num += 1
            if i < H-1 :
                if j > 0:
                    if S[i+1][j-1] == "#":
                        num += 1
                if S[i+1][j] == "#":
                    num += 1
                if j < W-1:
                    if S[i+1][j+1] == "#":
                        num += 1
            
            S[i][j] = str(num)

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

Submission status: Accepted

Solution code:
# For taking integer inputs.
import math
 
 
def inp():
    return(int(input()))
 
 
# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))
 
 
# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))
 
 
# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))
 
 
def valid(x, y): return True if x >= 0 and x < row and y >= 0 and y < col else False
 
 
fx = [+0, +0, +1, -1, -1, +1, -1, +1]
fy = [-1, +1, +0, +0, +1, +1, -1, -1]
row, col = invr()
mat = []
for _ in range(row):
    mat.append(instr())
 
for i in range(row):
    for j in range(col):
        if mat[i][j] == ".":
            count = 0
            for k in range(len(fx)):
                nx = i + fx[k]
                ny = j + fy[k]
                if valid(nx, ny):
                    if mat[nx][ny] == "#":
                        count += 1
            mat[i][j] = str(count)
 
for r in mat:
    for i in r:
        print(i, end="")
    print()

Submission status: Accepted

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

dx = [1,1,1,0,-1,-1,-1,0]
dy = [0,1,-1,1,0,1,-1,-1]
ans = [[0] * (w + 2) for _ in range(h + 2)]

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

for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == '.':
            for A in range(8):
                if(s[i + dy[A]][j + dx[A]]=='#'):
                    ans[i][j] += 1
            

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

Submission status: Accepted

Solution code:
def mine(n,m):
    try:
        if n >= 0 and n<=H and m>=0 and m<=W:
            s[n][m] += 1
    except:
        None


H,W = map(int,input().split())
s = [[0 if i == "." else "#" for i in input()]for j in range(H)]
      
for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            mine(i-1,j-1)
            mine(i-1,j)
            mine(i-1,j+1)
            mine(i,j+1)
            mine(i,j-1)
            mine(i+1,j-1)
            mine(i+1,j)
            mine(i+1,j+1)
        else:
            None
      
for k in s:
    a = list(map(str,k))
    print("".join(a))

Submission status: Accepted

Solution code:
def p(h,w):
    try:
        if h >= 0 and h<= H and w >= 0 and w <= W:
            S[h][w] += 1
    except:
        None

H,W = map(int,input().split())
S = [[0 if i == "." else "#" for i in input()] for _ in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            p(i+1, j)
            p(i+1, j+1)
            p(i+1, j-1)
            p(i-1, j)
            p(i-1, j+1)
            p(i-1, j-1)
            p(i, j+1)
            p(i, j-1)
            '''
            for k in S:
                a = list(map(str, k))
                print("".join(a))
            print()
            '''
        else:
            None
for k in S:
    a = list(map(str,k))
    print("".join(a))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
s.append("."*(w+2))
for _ in range(h):
    s.append("."+input()+".")
s.append("."*(w+2))
for i in range(1, h+1):
    ans = ""
    for j in range(1, w+1):
        if s[i][j] == ".":
            ans += 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("#"))
        else:
            ans += "#"
    print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
t=[(-1,1),(-1,0),(-1,-1),(0,1),(0,-1),(1,1),(1,0),(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]==".":
            tmp=0
            for x,y in t:
                if 0<=i+x<=h-1 and 0<=j+y<=w-1:
                    if s[i+x][j+y]=="#":
                        tmp+=1
            s[i][j]=str(tmp)
for i in range(h):
    print("".join(s[i]))

Submission status: Accepted

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

s=[]
s.append("."*(w+2))
for i in range(h):
    s.append("."+input()+".")
s.append("."*(w+2))

#print(s)
k=[]
for i in range(h):
    p=[]
    for j in range(w):
        cnt=0
        if s[i+1][j+1]==".":
            if s[i][j]=="#":
                cnt+=1
            if s[i][j+1]=="#":
                cnt+=1
            if s[i][j+2]=="#":
                cnt+=1
            if s[i+1][j]=="#":
                cnt+=1
            if s[i+1][j+2]=="#":
                cnt+=1
            if s[i+2][j]=="#":
                cnt+=1
            if s[i+2][j+1]=="#":
                cnt+=1
            if s[i+2][j+2]=="#":
                cnt+=1
            p.append(cnt)
        else:
            p.append("#")
    k.append(p)
    
for i in range(h):
    for j in range(w):
        print(k[i][j],end="")
    print()

Submission status: Accepted

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

x = [1,-1,1,-1,0,0,1,-1]
y = [1,-1,0,0,1,-1,-1,1]
n_s = []
for i in range(h):
    n_l = []
    for j in range(w):
        cnt=0
        for k in range(8):
            if l[i][j]=="#":
                cnt = "#"
                break
            elif 0<=i+x[k]<h and 0<=j+y[k]<w:
                if l[i+x[k]][j+y[k]]=="#":
                    cnt+=1
            else:
                continue
        n_l.append(str(cnt))
    n_s.append(n_l)

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

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    S = [list(input().strip("\n")) for _ in range(H)]
    round = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
    for h in range(H):
        for w in range(W):
            if S[h][w] == ".":
                count = 0
                for dh, dw in round:
                    if 0<=h+dh<H and 0<=w+dw<W and S[h+dh][w+dw] == "#":
                        count += 1
                S[h][w] = str(count)
    for h in range(H): print("".join(S[h]))

    return 0

if __name__ == "__main__":
    solve()

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] == ".":
            cnt = 0
            for i in range(max(h-1, 0), min(h+2, H)):
                for j in range(max(w-1, 0), min(w+2, W)):
                    if S[i][j] == "#":
                        cnt += 1
            S[h][w] = str(cnt)
    print("".join(S[h]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
c=["."+input()+"." for i in range(h)]
s=["."*(w+2)]
s.extend(c)
b=["."*(w+2)]
s.extend(b)
sc=[[0for _ in range(w+2)] for i in range(h+2)]
# print(sc)

for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]=="#":
            if sc[i-1][j-1]!="#":
                sc[i-1][j-1]+=1
            if sc[i][j-1]!="#":
                sc[i][j-1]+=1
            if sc[i+1][j-1]!="#":
                sc[i+1][j-1]+=1
            if sc[i-1][j]!="#":
                sc[i-1][j]+=1
            if sc[i+1][j]!="#":
                sc[i+1][j]+=1
            if sc[i-1][j+1]!="#":
                sc[i-1][j+1]+=1
            if sc[i][j+1]!="#":
                sc[i][j+1]+=1
            if sc[i+1][j+1]!="#":
                sc[i+1][j+1]+=1
            sc[i][j]="#"
for i in range(1,h+1):
    ne="".join([str(_) for _ in sc[i]])
    ne=ne[1:w+1]
    print(ne)

Submission status: Accepted

Solution code:
def print2(A,S): #print2(int or str a[:][:], str)
    for i in range(len(A)):
        b = [str(n) for n in A[i][:]]
        print(S.join(b))

h,w = map(int,input().split())
s = [0]*(h+2)
s[0] = ["."]*(w+2)
s[-1] = ["."]*(w+2)
for i in range(h):
    s[i+1] = ["."] + list(input()) + ["."]
ans = [[0 for j in range(w)] for i in range(h)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "#":
            ans[i-1][j-1] = "#"
        else:
            for y in range(-1,2):
                for x in range(-1,2):
                    if s[i+y][j+x] == "#":
                        ans[i-1][j-1] += 1
print2(ans,"")

Submission status: Accepted

Solution code:
import numpy as np
h,w = (map(int,input().split(' ')))
x = []
for i in range(h):
  x.append(input())
  
y = np.zeros((h,w),dtype=int)
#print(y)           
for n,i in enumerate(x):
  for j in range(w):
    if i[j] == '#':
#同列
      if j >= 1:
        if x[n][j-1] != '#':
          y[n][j-1]  +=1  
      if j < w-1:
        if x[n][j+1] != '#':
   	        y[n][j+1]  +=1  
#前列 
      if n > 0:
        if x[n-1][j] !='#':
          y[n-1][j] +=1
        if j >= 1:
          if x[n-1][j-1] !='#':
            y[n-1][j-1] +=1
        if j < w-1:
          if x[n-1][j+1] !='#':
            y[n-1][j+1] +=1
#後列
      if n < h-1:
        if x[n+1][j] != '#':  
          y[n+1][j] +=1
        if j >= 1:
          if x[n+1][j-1] != '#':
            y[n+1][j-1] +=1
        if j < w-1:
          if x[n+1][j+1] != '#':
            y[n+1][j+1] +=1

y2 = y.astype(np.unicode)
            
for n,i in enumerate(x):
  for n2,j in enumerate(i):
    if j == '#':
      y2[n][n2] ='#'
  print(''.join(list(y2[n])))

Submission status: Accepted

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

for y in range(h):
  for x in range(w):
    if s[y][x]=='.':
      cnt=0
      for dy,dx in [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]:
        ny=y+dy
        nx=x+dx
        if 0<=ny<h and 0<=nx<w and s[ny][nx]=='#':
          cnt+=1
      s[y][x]=str(cnt)

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

Submission status: Accepted

Solution code:
#! env python
# -*- coding: utf-8 -*-

import os
import sys
from itertools import repeat


# ac_py.main
# Date: 2020/06/13
# Filename: main 
# Author: acto_mini

def main():
    H, W = map(int, input().split())

    S = list()
    ans = list()

    for i in range(H):
        S.append(input())
        ans.append(list(repeat(0, W)))

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

                # 左上
                if i > 0 and j > 0 and ans[i - 1][j - 1] != "#":
                    ans[i - 1][j - 1] += 1
                # 上
                if i > 0 and ans[i - 1][j] != "#":
                    ans[i - 1][j] += 1
                # 右上
                if i > 0 and j < W - 1 and ans[i - 1][j + 1] != "#":
                    ans[i - 1][j + 1] += 1

                # 左
                if j > 0 and ans[i][j - 1] != "#":
                    ans[i][j - 1] += 1
                # 右
                if j < (W - 1) and ans[i][j + 1] != "#":
                    ans[i][j + 1] += 1

                # 左下
                if i < (H - 1) and j > 0 and ans[i + 1][j - 1] != "#":
                    ans[i + 1][j - 1] += 1
                # 下
                if i < (H - 1) and ans[i + 1][j] != "#":
                    ans[i + 1][j] += 1
                # 右下
                if i < (H - 1) and j < W - 1 and ans[i + 1][j] != "#":
                    ans[i + 1][j + 1] += 1

    for i in range(H):
        print(*ans[i], sep="")
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
    H, W = [int(i) for i in input().split()]
    S = [list(input()) for _ in range(H)]
    for y in range(H):
        for x in range(W):
            if S[y][x] == ".":
                S[y][x] = 0
            else:
                S[y][x] = 9
    for y in range(H):
        for x in range(W):
            if S[y][x] < 9:
                continue
            if x - 1 >= 0:
                if y - 1 >= 0:
                    S[y - 1][x - 1] += 1
                S[y][x - 1] += 1
                if y + 1 < H:
                    S[y + 1][x - 1] += 1
            if y - 1 >= 0:
                S[y - 1][x] += 1
            S[y][x] += 1
            if y + 1 < H:
                S[y + 1][x] += 1
            if x + 1 < W:
                if y - 1 >= 0:
                    S[y - 1][x + 1] += 1
                S[y][x + 1] += 1
                if y + 1 < H:
                    S[y + 1][x + 1] += 1
    for y in range(H):
        for x in range(W):
            if S[y][x] < 9:
                S[y][x] = str(S[y][x])
            else:
                S[y][x] = "#"
        print("".join(S[y]))


resolve()

Submission status: Accepted

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

GRID = [["."] * (w + 2)]
for _ in range(h):
    GRID.append(["."] + list(input()) + ["."])
GRID.append(["."] * (w + 2))

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if GRID[i][j] == "#":
            continue
        cnt = 0
        for a in range(-1, 2):
            for b in range(-1, 2):
                if GRID[i + a][j + b] == "#":
                    cnt += 1
        GRID[i][j] = str(cnt)

for row in GRID[1:h + 1]:
    print("".join(row[1:w + 1]))

Submission status: Accepted

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

for y in range(h):
  for x in range(w):
    if s[y][x]=='.':
      cnt=0
      for dy,dx in [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]:
        ny=y+dy
        nx=x+dx
        if 0<=ny<h and 0<=nx<w and s[ny][nx]=='#':
          cnt+=1
      s[y][x]=str(cnt)

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S = [list(input()) for _ in range(H)]
result = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
  for j in range(W):
    if S[i][j]=='#':
      result[i][j] = '#'
      if i+1 < H and result[i+1][j]!='#': result[i+1][j] += 1
      if i-1 >= 0 and result[i-1][j]!='#': result[i-1][j] += 1
      if j+1 < W and result[i][j+1]!='#': result[i][j+1] += 1
      if j-1 >= 0 and result[i][j-1]!='#': result[i][j-1] += 1
      if i+1 < H and j+1 < W and result[i+1][j+1]!='#': result[i+1][j+1] += 1
      if i+1 < H and j-1 >= 0 and result[i+1][j-1]!='#': result[i+1][j-1] += 1
      if i-1 >= 0 and j+1 < W and result[i-1][j+1]!='#': result[i-1][j+1] += 1
      if i-1 >= 0 and j-1 >= 0 and result[i-1][j-1]!='#': result[i-1][j-1] += 1
[[print(result[i][j]) if j==W-1 else print(result[i][j],end='') for j in range(W)] for i in range(H)]

Submission status: Accepted

Solution code:
import sys

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


board = [
  list(input())
  for _ in range(H)]

def calc(board, x, y):
    num = 0
    if board[x][y] == '#':
        return '#'
    for i in (x-1, x, x+1):
        for j in (y-1, y, y+1):
            if i == x and j == y:
                continue
            elif 0 <= i <= H-1 and 0 <= j <= W-1:
                if board[i][j] == '#':
                    num += 1
    return str(num)

for i in range(H):
    for j in range(W):
        sys.stdout.write(calc(board, i, j))
    sys.stdout.write('\n')

Submission status: Accepted

Solution code:
# 20200612_yorukatsu_b.py
def canGo(h,w,y,x):
    if h==y and w==x:
        return False
    if h<0 or h>=H or w<0 or w>=W:
        return False
    if glid[h][w] == "#":
        return False
    return True

def search(h,w):
    dy=[-1,0,1]
    dx=[-1,0,1]
    if glid[h][w]=="#":
        mine[h][w] = -1
        for y in dy:
            for x in dx:
                if canGo(h+y,w+x,h,w):
                    mine[h+y][w+x]+=1

H,W = map(int,input().split())
glid =[]
for i in range(H):
    glid.append(list(input()))
# print(glid)
mine = [[0]*W for i in range(H)]
# print(mine)

for i in range(H):
    for j in range(W):
        search(i,j)
for i in mine:
    for j in i:
        print(j if j!=-1 else "#",end="")
    print()

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            cnt = 0
            for y, x in d:
                if 0 <= i + y < h and 0 <= j + x < w:
                    if s[i+y][j+x] == "#":
                        cnt += 1
            s[i][j] = str(cnt)
        else:
            continue

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
ans = [[0 for i in range(W)] for j in range(H)]
for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      ans[i][j] = '#'
      if i > 0 and j > 0:
        if ans[i-1][j-1] != '#':
          ans[i-1][j-1] += 1
      if i > 0:
        if ans[i-1][j] != '#':
          ans[i-1][j] += 1
      if i > 0 and j <= W - 2:
        if ans[i-1][j+1] != '#':
          ans[i-1][j+1] += 1
      if j > 0:
        if ans[i][j-1] != '#':
          ans[i][j-1] += 1
      if j <= W - 2:
        if ans[i][j+1] != '#':
          ans[i][j+1] += 1
      if i <= H - 2 and j > 0:
        if ans[i+1][j-1] != '#':
          ans[i+1][j-1] += 1
      if i <= H - 2:
        if ans[i+1][j] != '#':
          ans[i+1][j] += 1
      if i <= H - 2 and j <= W - 2:
        if ans[i+1][j+1] != '#':
          ans[i+1][j+1] += 1
for i in range(H):
  A = [str(a) for a in ans[i]]
  A = ''.join(A)
  print(A)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
L = [str(input()) for _ in range(H)]
ans = [[0] * W for _ in range(H)]
dh = [1, 1, 1, 0, 0, -1, -1, -1]
dw = [-1, 0, 1, -1, 1, -1, 0, 1]
for h in range(H):
    for w in range(W):
        if L[h][w] == '#':
            ans[h][w] = '#'
        else:
            for i in range(8):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and L[nh][nw] == '#':
                    ans[h][w] += 1
for row in ans:
    print(*row, sep='')

Submission status: Accepted

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

t=[[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            for k in [(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)]:
                if k[1]>=0 and k[1]<w and k[0]>=0 and k[0]<h:
                    if s[k[0]][k[1]]=='#':
                        t[i][j]+=1
        else:
            t[i][j]='#'

for i in range(h):
    print(*t[i],sep='')

Submission status: Accepted

Solution code:
def update(s, x, y, h, w):
    if s[x][y] == ".":
        s[x][y] = 0
        return
    if type(s[x][y]) is int:
        return

    for i in range(-1, 2):
        for j in range(-1, 2):
            if i == 0 and j == 0:
                continue
            if x + i not in range(0, H) or y + j not in range(0, W):
                continue

            if s[x + i][y + j] == ".":
                s[x + i][y + j] = 1
            elif type(s[x + i][y + j]) is int:
                s[x + i][y + j] += 1
            else:
                continue


if __name__ == "__main__":
    H, W = [int(x) for x in input().split(" ")]
    S = [list(input()) for _ in range(H)]
    for i in range(H):
        for j in range(W):
            update(S, i, j, H, W)
    for s in S:
        for item in s:
            print(item, end="")
        print()

Submission status: Accepted

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

for i in range(H):
    emp = ["."]
    S = input()
    for j in range(W):
        emp.append(S[j])
    emp.append(".")
    li.append(emp)

li1 = ["." for i in range(W+2)]
li.append(li1)

for i in range(1,H+1):
    stra = ""
    for j in range(1,W+1):
        ans = 0
        if li[i][j] == "#":
            stra += "#"
        else:
            if li[i-1][j-1] == "#":
                ans += 1
            if li[i-1][j] == "#":
                ans += 1
            if li[i-1][j+1] == "#":
                ans += 1
            if li[i][j-1] == "#":
                ans += 1
            if li[i][j+1] == "#":
                ans += 1
            if li[i+1][j-1] == "#":
                ans += 1
            if li[i+1][j] == "#":
                ans += 1
            if li[i+1][j+1] == "#":
                ans += 1
            stra += str(ans)
    print(stra)

Submission status: Accepted

Solution code:
from pprint import pprint

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

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

area = [[S[i][j] for j in range(W)] for i in range(H)]


def find_bomb(h, w):
    h_l = max(0, h - 1)
    h_upper = min(h + 1, H - 1)
    w_left = max(0, w - 1)
    w_right = min(w + 1, W - 1)

    if area[h][w] == '#':
        return '#'

    cnt = 0

    for i in range(h_l, h_upper + 1):
        for j in range(w_left, w_right + 1):
            if area[i][j] == '#':
                cnt += 1

    return str(cnt)


ans = [[find_bomb(i, j) for j in range(W)] for i in range(H)]

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