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 = []
ans = []
for i in range(h):
    s.append(list(input()))
    ans.append([0]*w)

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            ans[i][j] = "#"
            continue
        for k in range(i-1, i+2):
            for l in range(j-1, j+2):
                if k >= 0 and l >= 0 and k < h and l < w:
                    if s[k][l] == "#":
                        ans[i][j] += 1
for i in range(h):
    for j in range(w):
        print(str(ans[i][j]), end = "")
    print("")

Submission status: Accepted

Solution code:

h, w = map(int, input().split())
ml = [input() for i in range(h)]
ll = [[0]*w for i in range(h)]

for i in range(h):
    for j in range(w):
        if ml[i][j] == ".":
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if 0 <= i+k < h and 0 <= j+l < w:
                        if ml[i+k][j+l] == "#":
                            ll[i][j] += 1
        else:
            ll[i][j] = "#"

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[]
for i in range(H):
    S.append(list(input()))
dx=[1,0,-1,0,1,-1,-1,1]
dy=[0,-1,0,1,1,-1,1,-1]
for i in range(H):
    for j in range(W):
        if(S[i][j]=='.'):
            cnt=0
            for k in range(8):
                x=i+dx[k]
                y=j+dy[k]
                if(x<0 or x>=H or y<0 or y>=W):
                    continue
                else:
                    if(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 = [0] * h
inp = [[0] * (w+2) for _ in range(h+2)]
ans = [['#'] * w for _ in range(h)]
for i in range(h):
    s[i] = input()
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            inp[i+1][j+1] = 1

#print()
'''
for i in range(h+2):
    for j in range(w+2):
        print(inp[i][j], end="")
    print()
'''
#print()

for i in range(h):
    for j in range(w):
        if inp[i+1][j+1] == 0:
            ans[i][j] = str(inp[i][j] + inp[i][j+1] + inp[i][j+2] + inp[i+1][j] + inp[i+1][j+2] + inp[i+2][j] + inp[i+2][j+1] + inp[i+2][j+2])

for i in range(h):
    for j in range(w):
        print(ans[i][j], 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,0],[-1,1],[0,1],[1,1],[1,-1]]
for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            cnt=0
            for k in d:
                nx,ny=i-k[0],j-k[1]
                if 0<=nx<H and 0<=ny<W and S[nx][ny]=='#':
                    cnt+=1
            S[i][j]=str(cnt)
for s in S:
    print(''.join(s))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
#左右に'.'を余分に付けて標準入力からリストを作成
grid = [list('.'+input()+'.') for i in range(H)]
#上下にも付けてやる
grid.insert(0, list('.' * (W + 2)))
grid.append(list('.' * (W + 2)))
#答えを入れるリスト
ans = []
 
#付け足した行列はカウントしない
for i in range(1, H + 1):
  for j in range(1, W + 1):
    if grid[i][j] == '#':
      ans.append('#')
    else:
      #8方向にある'#'の数をカウント
      #付け足したのは'.'なので条件分岐は不要
      #str型で出力しなければjoinできない
      ans.append(str((grid[i-1][j-1:j+2]+grid[i][j-1:j+2]+grid[i+1][j-1:j+2]).count('#')))
  #一行終わったので改行を入れる
  ans.append('\n')
print(''.join(ans)) #リストをつないで出力

Submission status: Accepted

Solution code:
# 下，右，上，左，右下，右上，左下，左上
dy = [1, 0, -1, 0, 1, -1, 1, -1]
dx = [0, 1, 0, -1, 1, 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):
            ny = y + dy[k]
            nx = x + dx[k]

            if ny < 0 or H <= ny or nx < 0 or W <= nx:
                continue

            if field[ny][nx] == "#":
                cnt += 1

        field[y][x] = str(cnt)

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
def minesweeper(Map, px, py):
  cnt = 0
  for i in range(9):
    if i != 4:
      dx, dy = i % 3 - 1, (i // 3) % 3 - 1
      if px + dx >= 0 and py + dy >= 0 and px + dx < len(Map) and py + dy < len(Map[0]):
        if Map[px+dx][py+dy] == '#':
          cnt += 1
  return cnt
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      S[i] = S[i][:j] + str(minesweeper(S, i, j)) + S[i][j+1:]
for i in range(H):
  print(S[i])

Submission status: Accepted

Solution code:
import math
import string


def readints():
    return list(map(int, input().split()))


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


h, w = map(int, input().split())
boad = [None]*h
for i in range(h):
    boad[i] = list(input())
# print(boad)

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h, w = (int(x) for x in input().split())
s = []
for _ in range(h): s.append(input())
s_num = [[0 for _ in range(w)] for _ in range(h)]
d = [-1, 0, 1]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#": continue
        else:
            c = 0
            for h_i in d:
                if i + h_i == -1 or i + h_i == h: continue
                else:
                    for w_i in d:
                        if j + w_i == -1 or j + w_i == w or (h_i == 0 and w_i == 0): continue
                        if s[i+h_i][j+w_i] == "#": c += 1
            s_num[i][j] = "{}".format(c)
for h_i in range(h):
    for w_i in range(w):
        if s[h_i][w_i] == "#": s_num[h_i][w_i] = "#"
        else: continue
for i in range(h): print("".join(s_num[i]))

Submission status: Accepted

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

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


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

Submission status: Accepted

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

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

counter = [[0 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 sl[i - 1][j - 1] == '#':
            counter[i - 1][j - 1] += 1
            counter[i - 1][j] += 1            
            counter[i - 1][j + 1] += 1
            counter[i    ][j - 1] += 1
            counter[i    ][j + 1] += 1
            counter[i + 1][j - 1] += 1
            counter[i + 1][j] += 1
            counter[i + 1][j + 1] += 1

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if sl[i - 1][j - 1] == '#':
            counter[i][j] = '#'
        else:
            counter[i][j] = str(counter[i][j])

for i in range(1, h + 1):
    print(''.join(counter[i][1:-1]))

Submission status: Accepted

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

n = [[0] * w for i in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            if i != 0:
                n[i-1][j] += 1
                if j != 0:
                    n[i-1][j-1] += 1
                if j != w-1:
                    n[i-1][j+1] += 1
            if i != h-1:
                n[i+1][j] += 1
                if j != 0:
                    n[i+1][j-1] += 1
                if j != w-1:
                    n[i+1][j+1] += 1
            if j != 0:
                n[i][j-1] += 1
            if j != w-1:
                n[i][j+1] += 1


for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            n[i][j] = "#"
    print(''.join(map(str, n[i])))

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy3
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    H,W=MI()
    S=[[] for _ in range(H+2)]
    S[0]="."*(W+2)
    
    for i in range(H):
        S[i+1]=list("." + input() + ".")
    S[-1]="."*(W+2)
    
    
    dx=[0,0,1,1,1,-1,-1,-1]
    dy=[1,-1,1,0,-1,1,0,-1]
    
    def se(ii,jj):
        cnt=0
        for k in range(8):
            i=ii+dx[k]
            j=jj+dy[k]
            if S[i][j]=="#":
                cnt+=1
        return cnt
    
    for i in range(1,H+1):
        for j in range(1,W+1):
            if S[i][j]==".":
                S[i][j]=se(i,j)
                
    for i in range(1,H+1):
        print(''.join(map(str, S[i][1:-1])))
            
            
        

main()

Submission status: Accepted

Solution code:
#N,Wを入力
H,W = map(int, input().split())

trout = []

for i in range(H):
    trout.append(list(input()))

# 配列の比較用の変数
h = H-1
w = W-1

for i in range(H):
    for j in range(W):
        
        c = 0
        # "#"だったらスルー
        if trout[i][j] == "#":
            pass
        
        else:
            # 左上
            if i>=1 and j>=1 and trout[i-1][j-1] == "#":
                c += 1
            # 真上
            if i>=1 and trout[i-1][j] == "#":
                c += 1
            # 右上
            if i>=1 and j<w and trout[i-1][j+1] == "#":
                c += 1
            # 左
            if j>=1 and trout[i][j-1] == "#":
                 c += 1
            # 右
            if j<w and trout[i][j+1] == "#":
                c += 1
            # 左下
            if i<h and j>=1 and trout[i+1][j-1] == "#":
                c += 1
            # 真下
            if i<h and trout[i+1][j] == "#":
                c += 1
            #右下
            if i<h and j<w and trout[i+1][j+1] == "#":
                c += 1
                
            trout[i][j] = str(c)
            
for i in range(H):
    print(''.join(trout[i]))

Submission status: Accepted

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

for y in range(h):
    for x in range(w):
        if l[y][x] == ".":
            c = 0
            for ya in range(-1, 2):
                for xa in range(-1, 2):
                    if 0 <= y + ya < h and 0 <= x + xa < w:
                        p = y + ya
                        q = x + xa
                        if l[p][q] == "#":
                            c += 1
            l[y][x] = str(c)

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [[' ' for i in range(w + 2)] for i in range(h + 2)]
for i in range(h):
    t = str(input())
    for j in range(w):
        s[i + 1][j + 1] = t[j]
# 周囲計算
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == '#': continue
        cnt = 0
        for y in range(i-1, i+2):
            for x in range(j-1, j+2):
                if s[y][x] == '#': cnt += 1
        s[i][j] = str(cnt)
for i in range(h):
    for j in range(w):
        print(s[i+1][j+1], end="")
    print()

Submission status: Accepted

Solution code:
from itertools import  product
h,w=map(int,input().split())

s=[]
for i in range(h):
    s.append(list(map(str,input())))

#print(s)
for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            print(s[i][j],end="")
        else:
            count=0
            for z in product((0, 1, -1), repeat=2):
                x,y=z
                
                if x!=0 or y!=0:
                    if 0<=(i-x) and (i-x)<h and 0<=(j-y) and (j-y)<w and s[i-x][j-y]=='#':
                        count+=1
            print(count,end="")
    print()

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
d = {'.':0, '#':1}
s = [['.']*(w+2)]
for i in range(h):
  s += [list('.' + input() + '.')]
s += [['.']*(w+2)]
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j] == '#':
      continue
    b = 0
    for x in range(-1, 2):
      for y in range(-1, 2):
        if s[i+x][j+y] == '#':
          b += 1
    s[i][j] = b
for i in range(1, h+1):
  for j in range(1, w+1):
    print(s[i][j], end='')
  print()
print()

Submission status: Accepted

Solution code:
y, x = map(int, input().split())
arr = [list(input()) for _ in range(y)]
for i in range(y):
    for j in range(x):
        if arr[i][j] == '#':
            continue
        num = 0
        for yi in range(-1, 2):
            for xj in range(-1, 2):
                if 0 <= j + xj < x and 0 <= i + yi < y:
                    xn = j + xj
                    yn = i + yi
                    if arr[yn][xn] == '#':
                        num += 1
        arr[i][j] = str(num)
for a in arr:
    print("".join(a))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=[]
s=[]
for i in range(w+2):
    s.append(0)
S.append(s)
for i in range(h):
    si="0"+input()+"0"
    ssi=[]
    for j in range(w+2):
        ssi.append(si[j])
    S.append(ssi)
S.append(s)
for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j]==".":
            countij=0
            seqij=[]
            seqij.append(S[i-1][j-1])
            seqij.append(S[i-1][j])
            seqij.append(S[i-1][j+1])
            seqij.append(S[i][j-1])
            seqij.append(S[i][j+1])
            seqij.append(S[i+1][j-1])
            seqij.append(S[i+1][j])
            seqij.append(S[i+1][j+1])
            for k in range(8):
                if seqij[k]=="#":
                    countij+=1
            S[i][j]=countij
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:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]

check_i = []
check_j = []
for i in range(H):
    if H == 1:
        check_i = [0]
    elif i == 0:
        check_i = [0, 1]
    elif i == H-1:
        check_i = [i-1, i]
    else:
        check_i = [i-1, i, i+1]
    for j in range(W):
        if S[i][j] == "#":
            continue

        if W == 1:
            check_j = [0]
        elif j == 0:
            check_j = [0, 1]
        elif j == W-1:
            check_j = [j-1, j]
        else:
            check_j = [j-1, j, j+1]
        
        count = 0
        for k in check_i:
            for l in check_j:
                if S[k][l] == "#":
                    count += 1
        S[i][j] = str(count)

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=["!"*(m+2)]+["!"+input()+"!" for i in range(n)]+["!"*(m+2)]
ans=[["0" for i in range(m)] for c in range(n)] 
for i in range(n):
  for d in range(m):
    if l[i+1][d+1]==".":
      ret=0
      for a,b in[[1,1],[1,0],[0,1],[-1,-1],[-1,0],[0,-1],[1,-1],[-1,1]] :
        if l[i+1+a][d+1+b]=="#":
          ret+=1
        ans[i][d]=str(ret)
    else:ans[i][d]="#"
for i in range(n):
  print("".join(ans[i]))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
b=[]
for i in range(n):
  b.append(input())
ans=[[0]*m for i in range(n)]
d=[-1,0,1]
for i in range(n):
  for j in range(m):
    if b[i][j]=='#':
      for x in d:
        for y in d:
          X=i+x
          Y=j+y
          if 0 <= X < n and 0 <= Y < m:
            ans[X][Y]+=1
import sys
for i in range(n):
  for j in range(m):
    if b[i][j]=='#':
      sys.stdout.write('#')
    else:
      sys.stdout.write(str(ans[i][j]))
  sys.stdout.write('\n')

Submission status: Accepted

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

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

ans = []
for i in range(1,H+1):
    for j in range(1,W+1):
        if maze[i][j] == "#":
            continue
        line = []
        bomb = 0
        for dy,dx in d:
            if maze[i+dy][j+dx] == "#":
                bomb += 1
        maze[i][j] = str(bomb)

for i in range(1,H+1):
    print("".join(map(str,maze[i][1:W+1])))

Submission status: Accepted

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

for x in range(1,W+1):
    for y in range(1,H+1):
        if S[y][x] == "#":
            ans[y-1][x-1] = "#"
        else:
            c = 0
            for dx in range(-1,2):
                for dy in range(-1,2):
                    if S[y+dy][x+dx] == "#":
                        c += 1
            ans[y-1][x-1] = c

for e in ans:
    print(*e,sep="")

Submission status: Accepted

Solution code:
def main():
    H, W = (int(i) for i in input().split())
    A = [input() for i in range(H)]
    dxy = ((-1, 1), (0, 1), (1, 1), (1, 0),
           (1, -1), (0, -1), (-1, -1), (-1, 0))
    c = [["0"]*W for h in range(H)]
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#":
                c[h][w] = "#"
            else:
                cur = 0
                for dx, dy in dxy:
                    if not(0 <= h + dy < H and 0 <= w + dx < W):
                        continue
                    if A[h + dy][w + dx] == "#":
                        cur += 1
                c[h][w] = str(cur)
    for h in range(H):
        print("".join(c[h]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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


mx = [0, 1, 1, 1, 0, -1, -1, -1]
my = [1, 1, 0, -1, -1, -1, 0, 1]
for hi in range(h):
    for wi in range(w):
        if s[hi][wi] == '#':
            print('#', end="")
            continue
        cnt = 0
        for mi in range(8):
            nx = wi + mx[mi]
            ny = hi + my[mi]
            if (nx >= w) or (ny >= h):
                continue
            if (nx < 0) or (ny < 0):
                continue
            if s[ny][nx] == '.':
                continue
            cnt += 1
        print(cnt, end="")
    print("")

Submission status: Accepted

Solution code:
import math
import string


def readints():
    return list(map(int, input().split()))


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


h, w = map(int, input().split())
a = [None]*h
for i in range(h):
    a[i] = input()
# print(a)

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

for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            print('#', end="")
        else:
            count = 0
            for k in range(8):
                x = i+dx[k]  # (i,j)の位置からk移動させたときのx座標
                y = j+dy[k]  # (i,j)の位置からk移動させたときのy座標
                if x < 0 or x >= h or y < 0 or y >= w:
                    # x,yが盤面の中に入っていて'#'のとき
                    continue

                if a[x][y] == '#':
                    count += 1

            print(count, end="")

    print('\n', end="")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
hwl = []
for _ in range(h):
  l = list(input())
  hwl.append(l)
  
for i in range(h):
  for j in range(w):
    if hwl[i][j] == ".":
      cnt = 0
      if i != 0 and j !=0 and hwl[i-1][j-1] == "#":
        cnt +=1
      
      if i != 0 and hwl[i-1][j] == "#":
        cnt +=1
      
      if i != 0 and j < w-1 and  hwl[i-1][j+1] == "#":
        cnt +=1
        
      if j !=0 and hwl[i][j-1] == "#":
        cnt +=1
      
      if j < w-1 and hwl[i][j+1] == "#":
        cnt +=1
        
      if i < h-1 and j != 0 and hwl[i+1][j-1] == "#":
        cnt +=1
      
      if i <h-1 and hwl[i+1][j] == "#":
        cnt +=1
        
      if i < h-1 and j < w-1 and hwl[i+1][j+1] == "#":
        cnt +=1
        
      hwl[i][j] = str(cnt)

  print("".join(hwl[i]))

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        count = 0
        # １．ドットの値だった時の処理で、
        if ans[h][w] == ".":
            for i in range(-1,2,1):
                for j in range(-1,2,1):
                    # ２．前後左右斜めをドットの位置を起点にして-1,0,1まで繰り返し値を取得を試みる
                    target_h = h+i
                    target_w = w+j
                    # ３．指定されたマスの範囲を超える場合は算出しないようにして、#の値をカウントする
                    if target_h >= 0 and target_h <= H-1 and target_w >= 0 and target_w <= W-1:
                        if ans[target_h][target_w] == "#":
                            count += 1
            ans[h][w] = count
# print(ans)
for row in ans:
    # リストをjoinでstringへ結合、それを行ごとに繰り返す
    print(''.join(list(map(str,row))))

Submission status: Accepted

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

    ans = ''
    for hi in range(1, h + 1):
        for wi in range(1, w + 1):
            c = convert(s, hi, wi)
            ans += c

        ans += '\n'

    print(ans, end='')


def convert(s, hi, wi):
    if s[hi - 1][wi - 1] == '#':
        return '#'

    count = 0
    for dh in [-1, 0, 1]:
        for dw in [-1, 0, 1]:
            hp = hi + dh
            wp = wi + dw
            if hp < 1 or hp > len(s) or wp < 1 or wp > len(s[0]) or s[hp - 1][wp - 1] == '.':
                continue
            if s[hp - 1][wp - 1] == '#':
                count += 1

    return str(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    matrix = [list(input()) for _ in range(H)]
    for r in matrix:
        r.insert(0, ".")
        r.append(".")
    matrix.insert(0, ["."] * (W + 2))
    matrix.append(["."] * (W + 2))

    count_matrix = [[0] * W for _ in range(H)]
    for r in range(1, H + 1):
        for c in range(1, W + 1):
            if matrix[r][c] == "#":
                count_matrix[r-1][c-1] = "#"
                continue
            for p_r in range(r-1, r+2):
                for p_c in range(c-1, c+2):
                    if matrix[p_r][p_c] == "#":
                        count_matrix[r-1][c-1] += 1
    print("\n".join(["".join([str(v) for v in r]) for r in count_matrix]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [input() for _ in range(h)]
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
dy = [-1, -1, -1, 0, 0, 1, 1, 1]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            print('#', end='')
        else:
            cnt = 0
            for k in range(8):
                if i+dx[k] < 0 or i+dx[k] > h-1:
                    continue
                if j+dy[k] < 0 or j+dy[k] > w-1:
                    continue
                if s[i+dx[k]][j+dy[k]] == '#':
                    cnt += 1
            print(cnt, end='')
    print()

Submission status: Accepted

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

for h in range(H) :
    for w in range(W) :
        if S[h][w] == "#" :
            pass
        else :
            k = 0
            for a in range(-1,2) :
                for b in range(-1,2) :
                    if h+a >= 0 and w+b >= 0 and S[h+a:] :
                        if S[h+a:][0][w+b:] :
                            if S[h+a][w+b] == "#" :
                                k += 1
            S[h][w] = str(k)

ans = ["".join(list(S[h])) for h in range(H)]
for h in range(H) :
    print(ans[h])

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
SS = [input() for _ in range(h)]
ret = ''
for i in range(h):
    for j in range(w):
        if SS[i][j] == '#':
            ret += '#'
            continue
        count = 0
        for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
            ny = i + dy
            nx = j + dx
            if 0 <= nx < w and 0 <= ny < h and SS[ny][nx] == '#':
                count += 1
        ret += str(count)
    ret += '\n'
print(ret)

Submission status: Accepted

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

s = []
for _ in range(h):
    s.append(list(input()))

def count(s, i, j, h, w):
    ans = 0
    if i > 0 and j > 0 and s[i - 1][j - 1] == '#':
        ans += 1
    if i > 0 and s[i - 1][j] == '#':
        ans += 1
    if i > 0 and j < w - 1 and s[i - 1][j + 1] == '#':
        ans += 1
    if j > 0 and s[i][j - 1] == '#':
        ans += 1
    if j < w - 1 and s[i][j + 1] == '#':
        ans += 1
    if i < h - 1 and j > 0 and s[i + 1][j - 1] == '#':
        ans += 1
    if i < h - 1 and s[i + 1][j] == '#':
        ans += 1
    if i < h - 1 and j < w - 1 and s[i + 1][j + 1] == '#':
        ans += 1
    return ans

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

Submission status: Accepted

Solution code:
import copy
H, W = map(int,input().split())
mp = [list(input()) for i in range(H)]
ans = copy.deepcopy(mp)
for i in range(H):
    mp[i].insert(0,'.')
    mp[i].append('.')
lo = ['.' for i in range(W+2)]
mp.insert(0,lo)
mp.append(lo)
def check(x,y):
    cnt = 0
    if mp[x][y] == '#':
        return '#'
    else:
        for i in range(3):
            for k in range(3):
                if mp[x-1+i][y-1+k] == '#':
                    cnt += 1
        return str(cnt)

for i in range(1,H+1):
    for k in range(1,W+1):
        ans[i-1][k-1] = check(i,k)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H, W = (int(i) for i in input().split(" "))
status = []
for i in range(H):
    status.append([k for k in input()])
new_status = [["#" for k in range(W)] for i in range(H)]
for i in range(H):
    for k in range(W):
        if status[i][k] == ".":
            count = 0
            if i - 1 >= 0 and k - 1 >= 0:
                if status[i - 1][k - 1] == "#":
                    count += 1
            if i - 1 >= 0:
                if status[i - 1][k] == "#":
                    count += 1
            if i - 1 >= 0 and k + 1 < W:
                if status[i - 1][k + 1] == "#":
                    count += 1
            if k - 1 >= 0:
                if status[i][k - 1] == "#":
                    count += 1
            if k + 1 < W:
                if status[i][k + 1] == "#":
                    count += 1
            if i + 1 < H and k - 1 >= 0:
                if status[i + 1][k - 1] == "#":
                    count += 1
            if i + 1 < H:
                if status[i + 1][k] == "#":
                    count += 1
            if i + 1 < H and k + 1 < W:
                if status[i + 1][k + 1] == "#":
                    count += 1
            new_status[i][k] = str(count)
for i in new_status:
    print("".join(i))