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,m = map(int,input().split())
s = [input() for i in range(h)]
ans = [[] for i in range(h)]

for i in range(h):
    for j in range(m):
        if s[i][j] == "#":
            ans[i].append("#")
        else:
            count = 0
            s1=[[i-1,j],[i+1,j],[i,j-1],[i,j+1]]
            s2=[[i-1,j+1],[i+1,j+1],[i+1,j-1],[i-1,j-1]]
            for temp in s1 :
                i2=temp[0];j2=temp[1]
                if i2<0 or h-1<i2 or j2<0 or m-1<j2:
                    continue

                if s[i2][j2]=="#":
                    count+=1

            for temp in s2 :
                i2=temp[0];j2=temp[1]
                if i2<0 or h-1<i2 or j2 <0 or m-1<j2 :
                    continue

                if s[i2][j2]=="#":
                    count+=1
            ans[i].append(count)

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

Submission status: Accepted

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

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

for i in range(H):
  for j in range(W):
    if arr[i][j]!='.': continue
    # replace
    bom=0
    for row_offs in [-1,0,1]:
      for col_offs in [-1,0,1]:
        if row_offs==0 and col_offs==0:   continue
        if i+row_offs<0 or H<=i+row_offs: continue
        if j+col_offs<0 or W<=j+col_offs: continue  
        if arr[i+row_offs][j+col_offs]=='#':
          bom+=1
    arr[i][j]=bom
    
for arr_ in arr:
  print(*arr_,sep="")

Submission status: Accepted

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


def q(i, j):
    if not 0 <= i < H or not 0 <= j < W:
        return 0
    return 1 if S[i][j] == "#" else 0


for i in range(H):
    for j in range(W):
        if S[i][j] != ".":
            continue
        c = 0
        c += q(i-1, j-1)
        c += q(i-1, j)
        c += q(i-1, j+1)
        c += q(i, j-1)
        c += q(i, j+1)
        c += q(i+1, j-1)
        c += q(i+1, j)
        c += q(i+1, j+1)
        S[i][j] = str(c)

for s in S:
    print("".join(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=[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:
import numpy as np
h,w=input().split()
h,w=int(h),int(w)
t1=[]
for i in range(h):
    s1=list(input())
    t1.append(s1)

t2=[[0 for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        if t1[i][j]=="#":
            for k in range(3):
                for l in range(3):
                    if k==1 and l==1:
                        continue
                    if i+k-1>=0 and j+l-1>=0 and i+k-1<h and j+l-1<w and t1[i+k-1][j+l-1]!="#":
                        t2[i+k-1][j+l-1]+=1
            t2[i][j]="#"
for i in range(h):
    for j in range(w):
        print(t2[i][j],end="")
    print("")

Submission status: Accepted

Solution code:
import itertools
# Minesweeper
# input
H, W = map(int, input().split())
R = [[0 for i in range(W+2)] for j in range(H+2)]
memo = []

# processing
for h in range(1, H+1):
    for w, s in zip(range(1, W+1), input()):
        if s == '#':
            memo.append([h, w])
            for i, j in itertools.product([-1, 0, 1], [-1, 0, 1]):
                R[h+i][w+j] += 1

for h, w in memo:
    R[h][w] = '#'

# output
for S in R[1:-1]:
    print(''.join([str(e) for e in S[1:-1]]))

Submission status: Accepted

Solution code:

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

# 番兵
R = []
R.append('.' + '.' * W + '.')
for c in S:
    R.append(['.'] + c + ['.'])

R.append('.' + '.' * W + '.')

#for line in R:
#    print(*line, sep="")

ret = []
for h in range(H):
    for w in range(W):
        count = 0
        # Rはxyともに+1したとこに対応
        if S[h][w] != '#':
            # 上
            if R[h][w] == '#': count += 1
            if R[h][w+1] == '#': count += 1
            if R[h][w+2] == '#': count += 1

            # 左右
            if R[h+1][w] == '#': count += 1
            if R[h+1][w+2] == '#': count += 1

            # 下
            if R[h+2][w] == '#': count += 1
            if R[h+2][w+1] == '#': count += 1
            if R[h+2][w+2] == '#': count += 1

            S[h][w] = str(count)

for line in S:
    print(*line, sep="")

Submission status: Accepted

Solution code:
import sys
h, w = map(int, input().split())
s = [input() for _ in range(h)]
if h == 1 and w == 1:
    if s[0] == '#':
        print('#')
    else:
        print(0)
    sys.exit()
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '#':
            if w == 1:
                if i == 0:
                    count = s[1][0].count('#')
                elif i == h-1:
                    count = s[-2][0].count('#')
                else:
                    count = s[i-1][0].count('#') + s[i+1][0].count('#')
            elif h == 1:
                if j == 0:
                    count = m[1].count('#')
                elif j == w-1:
                    count = m[-2].count('#')
                else:
                    count = m[j-1].count('#') + m[j+1].count('#')
            else:
                if i == 0:
                    if j == 0:
                        count = s[i+1][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[i+1][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
                elif i == h-1:
                    if j == 0:
                        count = s[-2][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[-2][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i-1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
                else:
                    if j == 0:
                        count = s[i-1][:2].count('#') + s[i+1][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[i-1][-2:].count('#') + s[i+1][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i-1][j-1:j+2].count('#') + s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            m = m.replace(m[j], str(count), 1)
    print(m)

Submission status: Accepted

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

#print(sl[1][1])
ans=[[] for i in range(h)]

for i in range(h) :
  for j in range(w):
    #subs=
    if sl[i][j]=="#" :
      ans[i].append("#")
    else :
      count=0
      search1=[[i-1,j],[i+1,j],[i,j-1],[i,j+1]]
      search2=[[i-1,j+1],[i+1,j+1],[i+1,j-1],[i-1,j-1]]
      for temp in search1 :
        i2=temp[0];j2=temp[1]
        if i2<0 or h-1<i2 or j2 <0 or w-1<j2 :
          continue
        if sl[i2][j2]=="#" :
          count+=1
      for temp in search2 :
        i2=temp[0];j2=temp[1]
        if i2<0 or h-1<i2 or j2 <0 or w-1<j2 :
          continue
        if sl[i2][j2]=="#" :
          count+=1
      ans[i].append(count)

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

Submission status: Accepted

Solution code:
import math
import sys
from itertools import accumulate
from functools import reduce
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

sys.setrecursionlimit(10000)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())
    = [list(list(sys.stdin.readline().rstrip())) for i in range(h)] #文字列を分解して二次元配列入力

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    h,w = map(int, sys.stdin.readline().split())
    s = [list(list(sys.stdin.readline().rstrip())) for i in range(h)]
    for i in range(h):
        for j in range(w):
            c=0
            if s[i][j] == "#":
                pass
            else:
                if 0<i and 0<j and s[i-1][j-1] == "#":#左上
                    c +=1
                if 0<j and s[i][j-1] == "#":#左
                    c +=1
                if 0<i and s[i-1][j] == "#":
                    c +=1
                if i<h-1 and j<w-1 and s[i+1][j+1] == "#":
                    c +=1
                if j<w-1 and s[i][j+1] == "#":
                    c +=1
                if i<h-1 and s[i+1][j] == "#":
                    c +=1
                if 0<i and j<w-1 and s[i-1][j+1] == "#":
                    c +=1
                if 0<j and i<h-1 and s[i+1][j-1] == "#":
                    c +=1
                s[i][j] = c
    for i in range(h):
        print(*s[i], sep='')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
sij=[["." for i in range(w+2)]]
sij+=[list("."+input()+".") for i in range(h)]
sij.append(["." for i in range(w+2)])
#print(*sij,sep='\n')
for i in range(1,h+1):
    for j in range(1,w+1):
        cnt=0
        if sij[i][j]=="#":
            continue
        for ii in range(-1,2):
            for jj in range(-1,2):
                if ii==jj==0:
                    continue
                if sij[i+ii][j+jj]=="#":
                    cnt+=1
        sij[i][j]=str(cnt)
for i in range(1,h+1):
    for j in range(1,w+1):
        print(sij[i][j],end="")
    print()

Submission status: Accepted

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

for h in range(H):
    S[h].extend([0])
S.append([0 for _ in range(W+1)])

for h in range(H):
    for w in range(W):
        if S[h][w] == 0:
            near = [(h-1, w-1), (h, w-1), (h+1, w-1), (h-1, w), (h+1, w), (h-1, w+1), (h, w+1), (h+1, w+1)]
            for nh, nw in near:
                if S[nh][nw] == '#':
                    S[h][w] += 1
for h in range(H):
    print(''.join([str(a) for a in S[h][:-1]]))

Submission status: Accepted

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

h_marume=lambda x: min(h-1, max(x, 0))
w_marume=lambda x: min(w-1, max(x, 0))

for i in range(h):
  for j in range(w):
    if slist[i][j]==".":
      t_set=[(h_marume(i-1),w_marume(j-1)),
             (h_marume(i-1),w_marume(j)),
             (h_marume(i-1),w_marume(j+1)),
             (h_marume(i),w_marume(j-1)),
             (h_marume(i),w_marume(j+1)),
             (h_marume(i+1),w_marume(j-1)),
             (h_marume(i+1),w_marume(j)),
             (h_marume(i+1),w_marume(j+1))
            ]
      t_set=list(set(t_set))
      print(sum([slist[t[0]][t[1]]=="#" for t in t_set]),end="")
    else:
      print("#",end="")
  print("\n",end="")

Submission status: Accepted

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

a = [[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] == "#":
            a[i][j] = "#"
            for p in [-1, 0, 1]:
                for q in [-1, 0, 1]:
                    if i + p >= 0 and j + q >= 0 and i + p < H and j + q < W and S[i + p][j + q] != "#":
                        a[i + p][j + q] += 1

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

Submission status: Accepted

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

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'
            continue
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if (I < 0 or H <= I):
                    continue
                for J in (j-1, j, j+1):
                    if (J < 0 or W <= J):
                        continue
                    if INPUT[I][J] == '#':
                        OUTPUT[i][j] += 1


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

Submission status: Accepted

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

A = ["." * (W+2)] + [("." + input()) + "." for _ in range(H)] + ["." * (W+2)]

for i in range(1,H+1):
  for j in range(1,W+1):
    if A[i][j] == "#":
      L ="#"
    else:
      L =sum([1 for x in range(-1, 2) for y in range(-1, 2) if A[i+x][j+y] == "#"])
    print (L, end = "")
  print()

Submission status: Accepted

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

dum='xx'
for i in range(W):
  dum=dum+'x'
  
string.append(dum)
for i in range(H):
  string.append('x'+input()+'x')

string.append(dum)

#print(string)  
  
for i in range(1,H+1):
  for j in range(1,W+1):
    if string[i][j]=='#':
      answer.append('#')
    else:
      count=0
      if string[i-1][j-1]=='#':
        count=count+1
      if string[i-1][j]=='#':
        count=count+1
      if string[i-1][j+1]=='#':
        count=count+1
      if string[i][j-1]=='#':
        count=count+1
      if string[i][j+1]=='#':
        count=count+1
      if string[i+1][j-1]=='#':
        count=count+1
      if string[i+1][j]=='#':
        count=count+1
      if string[i+1][j+1]=='#':
        count=count+1
      answer.append(count)

#print(answer)

for i in range(H):
  dum=''
  for j in range(W):
   dum=dum+str(answer[i*W+j])
  print(dum)

Submission status: Accepted

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

sheet = []

for _ in range(H):
	sheet.append(list(input()))

for i in range(H):
	for j in range(W):
		if sheet[i][j] == '.':
			sV = (i if i == 0 else i - 1)
			eV = (i + 1 if i == H - 1 else i + 2)
			sH = (j if j == 0 else j - 1)
			eH = (j + 1 if j == W - 1 else j + 2)
			count = 0
			
			for p in range(sV, eV):
				for q in range(sH, eH):
					if sheet[p][q] == '#':
						count += 1
			
			sheet[i][j] = count

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

Submission status: Accepted

Solution code:
gyou,retu=map(int,input().split())
l=[input() for _ in range(gyou)]
d=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for g in range(gyou):
  preans=str()
  for r in range(retu):
    s=0
    if l[g][r]=="#":
      preans += "#"
    else:
      for x in range(8):
        a,b=d[x]
        try:
          if l[g+a][r+b]=="#" and g+a>=0 and r+b>=0:
            s+=1
        except IndexError:
          s+=0
      preans += str(s)
  print(preans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [[j for j in input()] for i in range(h)]
for ki, k in enumerate(a):
    for ji, j in enumerate(k):
        if j == '#':
            print('#', end='')
        else:
            c = 0
            for m in range(3):
                for n in range(3):
                    if 0 <= ki - n + 1 < h and 0 <= ji - m + 1 < w:
                        if a[ki - n + 1][ji - m + 1] == '#':
                            c += 1
            print(c, end='')
    print()

Submission status: Accepted

Solution code:
h, w = [int(x) for x in input().split()]
grid = [[b for b in input()] for i in range(h)]
result = []
for i in range(h):
    row = ''
    for j in range(w):
        if grid[i][j] == '#':
            row += '#'
        else:
            row += str(sum([area[max(0, j - 1):min(w, j + 2)].count('#')
                            for area in grid[max(0, i - 1):min(h, i + 2)]]))
    result.append(row)
print('\n'.join(result))

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):
    l = []
    for j in range(1, w+1):
        if S[i][j] == '#':
            l.append('#')
            continue
        _s = S[i-1][j-1:j+2] + S[i][j-1] + S[i][j+1] + S[i+1][j-1:j+2]
        l.append(_s.count('#'))
    print(''.join(map(str, l)))

Submission status: Accepted

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

result = []
result.append(['0'] * (W))

for h in range(H):
    result.append(['0'] * W)
    s = input()

    for w in range(W):
        if s[w] == '#':

            result[h][w] = '#'

            if 0 < h and 0 < w:
                if result[h-1][w-1] != '#':
                    result[h-1][w-1] = str(int(result[h-1][w-1]) + 1)
            
            if 0 < h and w < W-1:
                if result[h-1][w+1] != '#':
                    result[h-1][w+1] = str(int(result[h-1][w+1]) + 1)

            if h < H-1 and w < W-1:
                if result[h+1][w+1] != '#':
                    result[h+1][w+1] = str(int(result[h+1][w+1]) + 1)
            
            if h < H-1 and 0 < w:
                if result[h+1][w-1] != '#':
                    result[h+1][w-1] = str(int(result[h+1][w-1]) + 1)

            if 0 < h:
                if result[h-1][w] != '#':
                    result[h-1][w] = str(int(result[h-1][w]) + 1)
            
            if h < H-1:
                if result[h+1][w] != '#':
                    result[h+1][w] = str(int(result[h+1][w]) + 1)
            
            if 0 < w:
                if result[h][w-1] != '#':
                    result[h][w-1] = str(int(result[h][w-1]) + 1)

            if w < W-1:
                if result[h][w+1] != '#':
                    result[h][w+1] = str(int(result[h][w+1]) + 1)

    if h > 0:
        print(''.join(result[h-1]))

print(''.join(result[-2]))

Submission status: Accepted

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

    #まわりに１つずつ空きますを広げる関数
def akimasu(s,h,w):
    F=[]
    F.append("."*(w+2))
    for i in s:
        F.append("."+i+".") 
    F.append("."*(w+2))
    return F

F=akimasu(S,h,w)

def hantei(f,y,x): # (y,x)の座標は1,1からh,wまでとする
    #判定用のfでは(y+1,x+1)が開始の点
    C=0
    for i in range(3):
        if f[y-1][x+i-1]=="#":
            C+=1
    for j in range(3):
        if f[y+1][x+j-1]=="#":
            C+=1
    if f[y][x-1]=="#":
        C+=1
    if f[y][x+1]=="#":
        C+=1
    if f[y][x]=="#":
        return "#"
    return str(C)

L=[] #爆弾数のリストh,wで
for H in range(1,h+1):
    X=[]
    for W in range(1,w+1):
        X.append(hantei(F,H,W))
    L.append(X)
for l in L:
    print("".join(l))

Submission status: Accepted

Solution code:
import numpy as np

def check(mass,x,y):
  cnt=0
  for in_i in [-1,0,1]:
    for in_j in [-1,0,1]:
      if mass[x+in_i][y+in_j]=="#":
        cnt+=1
  return cnt

a,b = map(int,input().split())
temp=["." for _ in range(b+2)]
mass=[temp]

for i in range(a):
  mass.append(["."]+list(input())+["."])
mass.append(temp)

for i in range(1,a+1):
  for j in range(1,b+1):
    if mass[i][j]==".":
      mass[i][j] = check(mass,i,j)
    
for i in range(1,a+1):
  temp = [str(temp_i) for temp_i in list(mass[i][1:b+1])]
  sentence = "".join(temp)
  print(sentence)

Submission status: Accepted

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

def check(a):
    d=[-1, 0, 1]
    for i in range(h):
        for j in range(w):

            count=0
            if not a[i][j]=="#":
                for x in d:
                    posx=j+x
                    for y in d:
                        posy=i+y
                        if posx>=0 and posx<w and posy>=0 and posy<h and a[posy][posx]=="#":
                            count+=1
                a[i][j]=count
    return a

mine=check(mine)
for i in mine:
    print(*i, sep="")

Submission status: Accepted

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

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

r = []
for y in range(1, h + 1):
  s = ''
  for x in range(1, w + 1):
    c = 0
    if f[y][x] == '#':
      s += '#'
    else:
      c += f[y - 1][x - 1: x + 2].count('#')
      c += f[y][x - 1: x + 2].count('#')
      c += f[y + 1][x - 1: x + 2].count('#')
      s += str(c)
  r.append(s)
print('\n'.join(r))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    H, W = map(int, input().split())
    S = [None]*H
    for i in range(H):
        S[i] = list(str(input()))
    S_num = [[0]*W for _ in range(H)]

    for h in range(H):
        for w in range(W):
            if S[h][w] == '#':
                if h-1 >= 0:
                    S_num[h-1][w] += 1
                    if w-1 >= 0:
                        S_num[h-1][w-1] += 1
                    if w+1 <= W-1:
                        S_num[h-1][w+1] += 1
                if h+1 <= H-1:
                    S_num[h+1][w] += 1
                    if w-1 >= 0:
                        S_num[h+1][w-1] += 1
                    if w+1 <= W-1:
                        S_num[h+1][w+1] += 1
                if w-1 >= 0:
                    S_num[h][w-1] += 1
                if w+1 <= W-1:
                    S_num[h][w+1] += 1
    for h in range(H):
        for w in range(W):
            if S[h][w] == '.':
                S[h][w] = str(S_num[h][w])
    for s in S:
        print(''.join(s))

Submission status: Accepted

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

arr = ['.' + s + '.' for s in arr]
arr = ['.'*(w+2)] + arr + ['.'*(w+2)]
arr = [list(s) for s in arr]
arr2 = []
for lst in arr:
    _lst = []
    for s in lst:
        if s=='.': 
            s = 0 
        else: 
            s = 1
        _lst.append(s)
    arr2.append(_lst)

arr = []
for i in range(1, h+1):
    lst = []
    for j in range(1, w+1):
        if arr2[i][j]==1:
            lst.append('#')
        else:
            cnt = sum(arr2[i][j-1:j+2]) + sum(arr2[i-1][j-1:j+2]) + sum(arr2[i+1][j-1:j+2]) - arr2[i][j]
            # print(i, j, arr2[i][j], cnt)
            lst.append(str(cnt))
    arr.append(lst)

arr = [''.join(lst) for lst in arr]
for s in arr:
    print(s)

Submission status: Accepted

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

ls = [['.' for _ in range(w+2)] for _ in range(h+2)]
for i in range(1,h+1):
  ls[i][1:w+1]=input()

for h in range(1,h+1):
    for w in range(1,w+1):
        if (ls[h][w]=='#'):
            continue
        c = 0
        for x in range(-1,2):
            for y in range(-1,2):
                if (x==0 and y==0):
                    continue
                elif (ls[h+x][w+y]=='#'):
                    c+=1
        ls[h][w]=str(c)
    else:
        print(''.join(ls[h][1:w+1]))

Submission status: Accepted

Solution code:
import sys
h, w = [int(i) for i in sys.stdin.readline().split()]
s = [sys.stdin.readline().strip() for _ in range(h)]
result = [[0] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            result[i][j] = '#'
            continue
        count = 0
        top = (i - 1)
        bottom = (i + 1)
        left = (j - 1)
        right = (j + 1)

        if top >= 0:
            if left >= 0:
                if s[top][left] == '#':
                    count += 1
            if s[top][j] == '#':
                count += 1
            if right < w:
                if s[top][right] == '#':
                    count += 1
        if bottom < h:
            if left >= 0:
                if s[bottom][left] == '#':
                    count += 1
            if s[bottom][j] == '#':
                count += 1
            if right < w:
                if s[bottom][right] == '#':
                    count += 1
        if left >= 0:
            if s[i][left] == '#':
                count += 1
        if right < w:
            if s[i][right] == '#':
                count += 1

        result[i][j] = str(count)

for r in result:
    print(''.join(r))

Submission status: Accepted

Solution code:
H,W=[int(x) for x in input().split()]
M=[['.' for _ in range(W+2)] for _ in range(H+2)]
for h in range(1,H+1):
  M[h][1:W+1]=list(input())

for h in range(1,H+1):
  for w in range(1,W+1):
    if M[h][w]=='#': continue
    c=0
    for x in range(-1,2):
      for y in range(-1,2):
        if x==0 and y==0:
          continue
        elif M[h+x][w+y]=='#':
          c+=1
    M[h][w]=str(c)
  else:
    print(''.join(M[h][1:W+1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S = [ input() for _ in range(H) ]
for h in range(H):
  L = ''
  for w in range(W):
    if S[h][w] == '#':
      L += '#'
    else:
      x = 0
      for dh in [ -1, 0, 1]:
        for dw in [-1, 0, 1]:
          if 0 <= h + dh < H and 0 <= w + dw < W and S[h+dh][w+dw] == '#':
            x += 1
      L += str(x)
  print(L)

Submission status: Accepted

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

s=['0'*(w+2)]

for i in range(h):
    s.append('0'+input()+'0')

s.append('0'*(w+2))


ans=[]
for i in range(1,h+1):
    tmp=[]
    for j in range(1,w+1):
        if s[i][j]=='#':
            tmp.append('#')
        else:
            tmp.append([s[i-1][j-1],s[i-1][j],s[i-1][j+1],\
            s[i][j-1],s[i][j],s[i][j+1],\
            s[i+1][j-1],s[i+1][j],s[i+1][j+1]].count('#'))
    ans.append(tmp)
for i in range(h):
    print(''.join(map(str,ans[i])))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
raw = ['/' * (w+2)]
for hi in range(h):
  raw.append('/' + input() + '/')
raw += ['/' * (w+2)]
#print(raw)
for hi in range(h):
  ans = ''
  pre = raw[hi]
  now = raw[hi+1]
  pst = raw[hi+2]
  for wi in range(w):
    if now[wi+1] == '#':
      cnt = '#'
    else:
      cnt = 0
      can = pre[wi:wi+3] + now[wi:wi+3] + pst[wi:wi+3]
      for j in range(9):
        if can[j] == '#':
          cnt += 1
    ans += str(cnt)
  print(ans)

Submission status: Accepted

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

bombMap=[]
result=[]
while i<H:
  tempLine=input()
  tempAray=[]
  tempResult=[]
  j=0
  
  while j<len(tempLine):
    tempAray.append(tempLine[j])
    tempResult.append(0)
    j=j+1
  bombMap.append(tempAray)
  result.append(tempResult)
  
  i=i+1

i=0
while i<H:
  j=0
  while j<W:
    if bombMap[i][j]=="#":
      result[i][j]="#"
      y=i-1
      while y< i+2:
        x=j-1
        while x<j+2:
          if 0<=y and 0<=x and y<H and x<W:
            
            if bombMap[y][x]!="#":
              #print(bombMap[y][x])
              result[y][x]=result[y][x]+1
              #print(result[y][x])
          x=x+1        
        y=y+1
    
    j=j+1  
  i=i+1
  
  
#print(bombMap)
#print(result)

i=0
while i<H:
  j=0
  temp=""
  while j<W:
    temp=temp+str(result[i][j])
    j=j+1
  print(temp)
  i=i+1

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if SN[i][j]=="#":
            SNc[i+1][j+1]-=100
            SNc[i][j]+=1
            SNc[i][j+1]+=1
            SNc[i][j+2]+=1
            SNc[i+1][j]+=1
            SNc[i+1][j+2]+=1
            SNc[i+2][j]+=1
            SNc[i+2][j+1]+=1
            SNc[i+2][j+2]+=1

for i in range(1,H+1):
    for j in range(1,W+1):
        if SNc[i][j]<0:
            print('#',end="")
        else:
            print(SNc[i][j],end="")
    print()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
matrix = [[x for x in input()] for y in range(h)]

def check_surround(matrix, x, y):
    if matrix[x][y] == '#':
        return '#'
    else:
        tmp = []
        tmp += check_index(matrix, x-1, y-1)
        tmp += check_index(matrix, x, y-1)
        tmp += check_index(matrix, x+1, y-1)
        tmp += check_index(matrix, x-1, y)
        tmp += check_index(matrix, x+1, y)
        tmp += check_index(matrix, x-1, y+1)
        tmp += check_index(matrix, x, y+1)
        tmp += check_index(matrix, x+1, y+1)
        return str(str(tmp).count('#'))

def check_index(matrix, x, y):
    if x > -1 and y > -1:
        try:
            return matrix[x][y]
        except:
            return str(0)
    else:
        return str(0)

for ri in range(h):
    s = ''
    for ni in range(w):
        s += check_surround(matrix, ri, ni)
    print(s)

Submission status: Accepted

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

ans = [[0 for x in range(w+2)] for y in range(h+2)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            ans[i][j] += 1
            ans[i+1][j] += 1
            ans[i+2][j] += 1
            ans[i][j+1] += 1
            ans[i+2][j+1] += 1
            ans[i][j+2] += 1
            ans[i+1][j+2] += 1
            ans[i+2][j+2] += 1

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