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 = [input() for _ in range(H)]
A = []
for i in range(H):
    A.append([0]*(W))
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            A[i][j] = '#'
            if i-1 >= 0 and j-1 >= 0 and A[i-1][j-1] != '#' : A[i-1][j-1] +=1 
            if i-1 >= 0 and A[i-1][j] != '#' : A[i-1][j] +=1 
            if i-1 >= 0 and j+1 < W and A[i-1][j+1] != '#' : A[i-1][j+1] +=1 
            if j-1 >= 0 and A[i][j-1] != '#' : A[i][j-1] +=1 
            if j+1 < W and A[i][j+1] != '#' : A[i][j+1] +=1 
            if i+1 < H and j-1 >= 0 and A[i+1][j-1] != '#': A[i+1][j-1] +=1 
            if i+1 < H and A[i+1][j] != '#': A[i+1][j] +=1 
            if i+1 < H and j+1 < W and A[i+1][j+1] != '#' : A[i+1][j+1] +=1 
for i in range(H):
    line = []
    for j in range(W):
        line.append(str(A[i][j]))
    print(''.join(line))

Submission status: Accepted

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

padding = ['.']*(W+2)
S.insert(0, padding)
S.append(padding)
for i in range(1, H+1):
    S[i].insert(0, '.')
    S[i].append('.')

for i in range(1, H+1):
    for j in range(1,W+1):
        if S[i][j]=='.':
            S_part= S[i-1][j-1:j+2]+S[i][j-1:j+2]+S[i+1][j-1:j+2]
            S_cnt = S_part.count('#')
            S[i][j]=S_cnt
for i in range(1,H+1):
    print(*S[i][1:W+1],sep='')

Submission status: Accepted

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

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)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  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(N)]
"""


H, W = map(int, sys.stdin.readline().split())
x = ["x" for _ in range(W + 2)]
DL = [x]

for _ in range(H):
    L = list(sys.stdin.readline().rstrip())
    L = ["x"] + L + ["x"]
    DL.append(L)
DL.append(x)

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if DL[i][j] == ".":
            cnt = 0
            for k in range(i - 1, i + 2):
                for l in range(j - 1, j + 2):
                    if DL[k][l] == "#":
                        cnt += 1
            DL[i][j] = cnt

ans = []
for d in DL[1:len(DL) - 1]:
    print(''.join([str(x) for x in d if x != "x"]))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(H):
    l = ''
    for j in range(W):
        if S[i][j] == '#':
            l += '#'
        else:
            l += str(sum([s[max(0, j-1):min(W, j+2)].count('#') for s in S[max(0, i-1):min(H, i+2)]]))
    print(l)

Submission status: Accepted

Solution code:
h, w = map(int,input().split())
a = [list(input()) for _ in range(h)]
b = [[0 for _ in range(w)]for _ in range(h)]
H = [-1,-1,-1, 0, 0, 1, 1, 1]
W = [-1, 0, 1,-1, 1,-1, 0, 1]
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            b[i][j] = "#"
            for k in range(len(H)):
                if i+H[k] < 0 or i+H[k] >= h:continue
                elif j+W[k] < 0 or j+W[k] >= w:continue
                elif a[i+H[k]][j+W[k]] == "#":continue
                b[i+H[k]][j+W[k]] += 1
for i in range(h):
    print(*b[i],sep="")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
grid = ["."*(w+2)]+["."+input()+"." for _ in range(h)]+["."*(w+2)]
for y in range(1,h+1):
	for x in range(1,w+1):
		if grid[y][x]=="#":
			print("#",end="")
		else:
			print(sum(grid[i][j]=="#" for i in range(y-1,y+2) for j in range(x-1,x+2)),end="")
	print("")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h,w=[int(i) for i in input().split()]
s=[input() for _ in range(h)]
	
#upper upperleft left lowerleft lower lowerright right upperright  
move=[[-1,0],[-1,-1],[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1]]

for y in range(h):
	for x in range(w):
		cnt=0
		if s[y][x]=='#':continue
		
		for i in range(8):
			yi=y+move[i][0]
			xi=x+move[i][1]
			if 0<=xi<w and 0<=yi<h:
				if s[yi][xi]=='#':cnt+=1
			else:continue
		sl=list(s[y])
		sl[x]=str(cnt)
		s[y]=''.join(sl)
print("\n".join(s))

Submission status: Accepted

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

SS='.'*(W+2)
S=['.'+input()+'.' for _ in range(H)]

SS=['.'*(W+2)]

SS.extend(S)

SE=['.'*(W+2)]

SS.extend(SE)

for i in range(1,H+1):
    for j in range(1, W+1):
        if SS[i][j]=='#':
            print('#',end='')
        else:   
            print(str((SS[i-1][j-1]+SS[i][j-1]+SS[i+1][j-1]+SS[i-1][j]+SS[i+1][j]+SS[i-1][j+1]+SS[i][j+1]+SS[i+1][j+1]).count('#')),end='')
        if j==W:
            print('')

Submission status: Accepted

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

init = []
for i in range(h):
    # x = input()
    init.append([])
    for j in range(w):
        init[i].append(0)

for i in range(h):
    for j in range(w):
        if mass[i][j] != "#":
            # up
            if i + 1 < h and mass[i + 1][j] == "#":
                init[i][j] += 1
                # down
            if i - 1 >= 0 and mass[i - 1][j] == "#":
                init[i][j] += 1
                # right
            if j + 1 < w and mass[i][j + 1] == "#":
                init[i][j] += 1
                # left
            if j - 1 >= 0 and mass[i][j - 1] == "#":
                init[i][j] += 1
            if j + 1 < w and i + 1 < h and mass[i + 1][j + 1] == "#":
                init[i][j] += 1

            if i - 1 >= 0 and j - 1 >= 0 and mass[i - 1][j - 1] == "#":
                init[i][j] += 1

            if i + 1 < h and j - 1 >= 0 and mass[i + 1][j - 1] == "#":
                init[i][j] += 1

            if i - 1 >= 0 and j + 1 < w and mass[i - 1][j + 1] == "#":
                init[i][j] += 1
        else:
            init[i][j] = "#"
res = []
for i in range(h):
    for j in range(w):
        res.append(str(init[i][j]))
    print(''.join(res))
    res = []

Submission status: Accepted

Solution code:
h,w=[int(i) for i in input().split()]
l=['n'*(w+2),'n'*(w+2)]
for _ in range(h):
	a=input()
	l.insert(1,'n'+a+'n')
l.reverse()
ans=['#'*w for i in range(h)]
for i in range(1,h+1):
	for j in range(1,w+1):
		cnt=0
		if l[i][j]=='#':continue
		if l[i][j+1]=='#':cnt+=1
		if l[i][j-1]=='#':cnt+=1
		if l[i-1][j-1]=='#':cnt+=1
		if l[i-1][j]=='#':cnt+=1
		if l[i-1][j+1]=='#':cnt+=1
		if l[i+1][j-1]=='#':cnt+=1
		if l[i+1][j]=='#':cnt+=1
		if l[i+1][j+1]=='#':cnt+=1
		ans_=list(ans[i-1])
		ans_[j-1]=str(cnt)
		ans_="".join(ans_)
		ans[i-1]=ans_
print('\n'.join(ans))

Submission status: Accepted

Solution code:
H,W=map(int,input().split());S=[input()for _ in range(H)];T=[""]*H
for i,l in enumerate(S):
 for j,c in enumerate(l):
  T[i]+=str(sum(S[i+p][j+q]=="#"for p in[-1,0,1]for q in[-1,0,1]if(0<=i+p<H)&(0<=j+q<W)))if c=="."else c
print("\n".join(T))

Submission status: Accepted

Solution code:
#インポート
import sys

#入力用
def ILI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def ISI(): return map(int, sys.stdin.readline().rstrip().split())
def II(): return int(sys.stdin.readline().rstrip())
def ISS(): return sys.stdin.readline().rstrip().split()
def IS(): return sys.stdin.readline().rstrip()


dx = [-1,  0,  1, -1, 1, -1, 0, 1]
dy = [-1, -1, -1,  0, 0,  1, 1, 1]
H, W = ISI()
l = [list(IS()) for i in range(H)]
for i in range(H):
    for j in range(W):
        if l[i][j] != "#":
            cnt = 0
            for d in range(8):
                ni = i + dy[d]
                nj = j + dx[d]
                if 0 > ni or ni >= H: continue
                if 0 > nj or nj >= W: continue
                if l[ni][nj] == "#":
                    cnt += 1
            l[i][j] = str(cnt)
for i in range(H):
    print("".join(l[i]))

Submission status: Accepted

Solution code:
import itertools
H,W = map(int, input().strip().split(' '))

S = [list('.'*(W+2))]
S += [list('.'+input().strip()+'.') for _ in range(H)]
S.append(list('.'*(W+2)))

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

for y in range(1,H+1):
  print(*S[y][1:-1], sep='')

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    H, W = map(int, input().split())
    
    bomb = "#"
    empty = "."
    empty_W = empty * (W + 2)
    
    s = []
    s.append(empty_W)
    for i in range(H):
        s_sub = input()
        s_sub = empty + s_sub + empty
        s.append(s_sub)
    s.append(empty_W)
    
    t = ["" for i in range(H)]
    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if s[i][j] == empty:
                sij = 0
                for k in range(i - 1, i + 2):
                    for l in range(j - 1, j + 2):
                        if s[k][l] == bomb:
                            sij += 1
                t[i - 1] = t[i - 1] + str(sij)
            else:
                t[i - 1] = t[i - 1] + bomb
            
    for i in range(H):
        print(t[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    H, W = map(int, input().split())
    s_list = []
    answer = [[0] * W for i in range(H)]
    for i in range(H):
        s_list.append(list(input()))

    for h, s_h in enumerate(s_list):
        for w, s_h_w in enumerate(s_h):
            count = 0
            if s_h_w == '.':
                continue
            else:
                for x in range(-1,2):
                    target_x = w + x
                    if target_x < 0 or target_x >= W:
                        continue
                    for y in range(-1,2):
                        target_y = h + y
                        if target_y < 0 or target_y >= H:
                            continue
                        if s_list[target_y][target_x] != '#':
                            answer[target_y][target_x] += 1
                        else:
                            answer[target_y][target_x] = '#'

    for i in answer:
        print("".join(map(str,i)))

resolve()

Submission status: Accepted

Solution code:
import itertools

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

for i in range(h):
    for j in range(w):
        cnt = 0
        if s[i][j] != "#":
            for dx, dy in itertools.product([-1, 0, 1], repeat=2):
                y = i+dy
                x = j+dx
                if y < 0 or y >= h:
                    continue
                if x < 0 or x >= w:
                    continue
                if s[y][x] == "#":
                    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 = [input() for i 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:
import sys
input = sys.stdin.readline

def main():
    H, W = map(int, input().split())
    s = [input().strip() for i in range(H)]

    ans = [[0] * W for i in range(H)]
    direction = [
        [-1, -1], [0, -1], [1, -1],
        [-1, 0],           [1, 0],
        [-1, 1],  [0, 1],  [1, 1]
    ]
    for y in range(H):
        for x in range(W):
            if s[y][x] == ".":
                for dx, dy in direction:
                    tx, ty = x+dx, y+dy
                    if 0 <= tx and tx < W and 0 <= ty and ty < H:
                        if s[ty][tx] == "#":
                            ans[y][x] += 1
    for y in range(H):
        for x in range(W):
            if s[y][x] == "#":
                print("#", end="")
            else:
                print(ans[y][x], end="")
        print()


if __name__ == "__main__":
    main()

Submission status: Accepted

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


def output_string8(table, x, y):

    string8 = []

    string8.append(table[x-1][y+1])
    string8.append(table[x][y+1])
    string8.append(table[x+1][y+1])
    string8.append(table[x-1][y])
    string8.append(table[x+1][y])
    string8.append(table[x-1][y-1])
    string8.append(table[x][y-1])
    string8.append(table[x+1][y-1])

    return ''.join(string8)


H, W = map(int, input().split())
S = []

S.append('X' * (W+2))

for _ in range(H):
    line = 'X' + input() + 'X'
    S.append(line)

S.append('X' * (W+2))

BOMB = '#'

for y in range(1, H + 1):
    for x in range(1, W + 1):

        ch = S[y][x]

        if ch == BOMB:
            print(BOMB, end='')
            continue

        st = output_string8(S, y, x)
        print(st.count(BOMB), end='')

    print(end='\n')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(h):
    s.append(input())
def cnt(i,j):
    tmp=0
    if i!=0:
        if s[i-1][j]=='#':
            tmp+=1
    if i!=h-1:
        if s[i+1][j]=='#':
            tmp+=1
    if j!=0:
        if s[i][j-1]=='#':
            tmp+=1
    if j!=w-1:
        if s[i][j+1]=='#':
            tmp+=1
    if i!=0 and j!=0:
        if s[i-1][j-1]=='#':
            tmp+=1
    if i!=0 and j!=w-1:
        if s[i-1][j+1]=='#':
            tmp+=1
    if i!=h-1 and j!=0:
        if s[i+1][j-1]=='#':
            tmp+=1
    if i!=h-1 and j!=w-1:
        if s[i+1][j+1]=='#':
            tmp+=1
    return tmp

t=[]
for i in range(h):
    tmp=[]
    for j in range(w):
        if s[i][j]=='.':
            tmp.append(cnt(i,j))
        else:
            tmp.append('#')
    t.append(tmp)
for i in range(h):
    print(*t[i],sep='')

Submission status: Accepted

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

for i in range(h):
    mines_id = [m_i for m_i, word in enumerate(s[i]) if word == '#']
    if mines_id == []:
        continue
    else:
        for mine_id in mines_id:
            if i - 1 >= 0:
                ans[i - 1][mine_id] += 1
            if i + 1 <= h - 1:
                ans[i + 1][mine_id] += 1
            if mine_id - 1 >= 0:
                ans[i][mine_id - 1] += 1
            if mine_id + 1 <= w - 1:
                ans[i][mine_id + 1] += 1
            if i - 1 >= 0 and mine_id - 1 >= 0:
                ans[i - 1][mine_id - 1] += 1
            if i + 1 <= h - 1 and mine_id + 1 <= w - 1:
                ans[i + 1][mine_id + 1] += 1
            if i + 1 <= h - 1 and mine_id - 1 >= 0:
                ans[i + 1][mine_id - 1] += 1
            if i - 1 >= 0 and mine_id + 1 <= w - 1:
                ans[i - 1][mine_id + 1] += 1

for i in range(h):
    mines_id = [m_i for m_i, word in enumerate(s[i]) if word == '#']
    if mines_id == []:
        continue
    else:
        for mine_id in mines_id:
            ans[i][mine_id] = '#'

for a in ans:
    print(''.join(map(str, a)))

Submission status: Accepted

Solution code:
H, W=map (int, input().split(' '))
z={}

for i in range(H):
    z[i]=str(input())



for h in range(H):
    for w in range(W):
        if z[h][w:w+1] == "#":
            print('#', end="")
            continue
        p=0
        for dy in [-1, 0, 1]:
            y=h+dy
            if h + dy < 0 or h + dy >= H:
                continue
            for dx in [-1, 0, 1]:
                x=w+dx
                if x < 0 or x >= W:
                    continue
                if x == w and y ==h:
                    continue
                
                if z[y][x:x+1] == "#":
                    p+=1
        print(p,end="")
    print("\n", end="")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
map=[[ int(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]=='#':
			map[i][j]='#'
		else:
			for k in range(-1,2):
				for p in range(-1,2):
					if 0<=i+k and i+k<h and 0<=j+p and j+p<w and s[i+k][j+p]=='#':
						map[i][j]+=1
			map[i][j]=str(map[i][j])
for i in range(h):	
	print(''.join(map[i]))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
L = [list(input()) for _ in range(h)] 
for i in range(h):
  for k in range(w):
    if L[i][k]=="#":
      continue
    count=0
    for v in range(-1, 2):
      if i+v < 0 or i+v > h-1:
        continue
      for t in range(-1, 2):
        if k+t < 0 or k+t > w-1:
          continue
        if L[i+v][k+t]=="#":
          count+=1
    L[i][k] = str(count)
for c in range(h):
  print("".join(L[c]))

Submission status: Accepted

Solution code:
from itertools import product
#%%
def resolve():
    h,w=map(int,input().split())
    A=[list('.'+input()+'.') for _ in range(h)]
    A=[['.']*(w+2)]+A+[['.']*(w+2)]

    for i,j in product(range(1,h+1),range(1,w+1)):
        if A[i][j]=='.':
            A[i][j]=((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]=='#'))

    for i in range(1,h+1):
        print(*A[i][1:w+1],sep='')
resolve()

Submission status: Accepted

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

h,w = map(int,input().split())
S = [[] for _ in range(h)]
for i in range(h):
    S[i] = list(input())
ANS = [[0]*(w+2) for _ in range(h+2)]
for i in range(h):
    for j in range(w):
        if S[i][j] == "#":
            ANS[i+1][j+1] = "#"
            for k,l in [(i,j),(i,j+1),(i,j+2),(i+1,j),(i+1,j+2),(i+2,j),(i+2,j+1),(i+2,j+2)]:
                if not ANS[k][l] == "#": ANS[k][l] += 1
for i in range(1,h+1):
    a = map(str,ANS[i][1:-1])
    print("".join(a))

Submission status: Accepted

Solution code:
# ABC075b
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

ans = [[0] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if (s[i][j] == '#'):
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if (0 <= i+k < h and 0 <= j+l < w and ans[i+k][j+l] != '#'):
                        ans[i+k][j+l] += 1
            ans[i][j] = '#'

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
List1 = [list(map(str, input())) for row in range(H)]
List2 = [[0 for col in range(W)] for row in range(H)]
for row in range(H):
    for col in range(W):
        if List1[row][col] == "#":
            List2[row][col] = "#"
        else:
            c = 0
            for rdf in range(-1, 2):
                if 0 <= row + rdf < H:
                    for cdf in range(-1, 2):
                        if 0 <= col + cdf < W:
                            if List1[row + rdf][col + cdf] == "#":
                                c += 1
            List2[row][col] = c
for row in range(H):
    print(*List2[row], sep="")

Submission status: Accepted

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

def count_bomb(h_i, w_i):
    sq = ""
    for i in range(3):
        sq += s[h_i + i - 1][w_i - 1: w_i + 2]
    return sq.count("#")

for h_i in range(1, h + 1):
    for w_i in range(1, w + 1):
        if s[h_i][w_i] == ".":
            print(count_bomb(h_i, w_i), end="")
        else:
            print("#", end="")
    print()

Submission status: Accepted

Solution code:
#ABC 075 B Minesweeper

#数字の入力
#外枠を作ってカウントをやりやすくする
h, w = map(int, input().split())
s = []
waku = "a"*(w+2)
s.append(waku)
for i in range(h):
    kari = "a" + input() + "a"
    s.append(kari)
s.append(waku)
#ここまでで枠付きの文字列を入力した。調べるべきはs[1~h][1~w]
#for i in range(h+2):
#    print(s[i])
new_s = []
new_kari = ""
for i in range(1,h + 1):
    for j in range(1,w+1):
        count = 0
        if s[i][j] == ".":
            for k in range(-1,2):#周辺の1行ずれを探すために-1,0，1を発生させる
                for l in range(-1,2):
                    if s[i+k][j+l] == "#":
                        count += 1
            new_kari += str(count)
        else:
            new_kari += "#"
    new_s.append(new_kari)
    new_kari = ""
for i in range(h):
    print(new_s[i])

Submission status: Accepted

Solution code:
line1 = input().split()
h,w = int(line1[0]), int(line1[1])

input_mass = []
for _ in range(h):
    s = input()
    t = [s[i] for i in range(len(s))]
    input_mass.append(t)

for high in range(h):
    for width in range(w):
        if input_mass[high][width] == '#':
            print('#',end='')
        else:
            count = 0
            for i in [high-1,high,high+1]:
                for j in [width-1,width,width+1]:
                    if 0<=i<h and 0<=j<w:
                        if input_mass[i][j] == '#':
                            count += 1
            print(count,end='')
    print()

Submission status: Accepted

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

after = []
for i in range(h):
    row = ''
    for j in range(w):
        tmp = ''
        if before[i][j] == '.':
            for i2 in (i-1, i, i+1):
                for j2 in (j-1, j, j+1):
                    if (i2 == i and j2 == j) or (0 > i2) or (h <= i2) or (0 > j2) or (w <= j2):
                        continue
                    else:
                        tmp += before[i2][j2]
            row += str(tmp.count('#'))
        else:
            row += '#'
    after.append(row)

for row in after:
    print(row)

Submission status: Accepted

Solution code:
import sys
import  math
import bisect
from statistics import *
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

h, w = na()
li = []
for i in range(h):
    li.append(input())
for i in range(h):
    for j in range(w):
        ct = 0
        if li[i][j] == "#":
            print("#", end="")
        else:
            for p in [li[i-1+k][j-1+l] for k in range(3) for l in range(3) if 0<=i-1+k<=h-1 and 0<=j-1+l<=w-1]:
                if p == "#":
                    ct += 1
            print(ct, end="")
    print()

Submission status: Accepted

Solution code:
import sys
import  math
import bisect
from statistics import *
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

h, w = na()
li = []
for i in range(h):
    li.append(input())
for i in range(h):
    for j in range(w):
        ct = 0
        if li[i][j] == "#":
            print("#", end="")
        else:
            ppp = []
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if 0 <= i + k <= h-1 and 0 <= j + l <= w-1:
                        ppp.append(li[i+k][j+l])
            for p in ppp:
                if p == "#":
                    ct += 1
            print(ct, end="")
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
Slist = list()
HWlist =list()
for i in range(0,H):
    Slist.append(str(input()))
    HWlist.append(list(Slist[i]))

Reslist = list()
for h in range(0,H):
    Reslist.append("")

for i in range(0,H):
    for j in range(0,W):
        if HWlist[i][j] != "#":
            count = 0
            if i-1 >= 0:
                if HWlist[i-1][j] == "#":
                    count = count + 1
            if i+1 < H:
                if HWlist[i+1][j] == "#":
                    count = count + 1
            if j-1 >= 0:
                if HWlist[i][j-1] == "#":
                    count = count + 1
            if j+1 < W:
                if HWlist[i][j+1] == "#":
                    count = count + 1
            if i-1 >= 0 and j-1>=0:
                if HWlist[i-1][j-1] == "#":
                    count = count +1
            if i-1 >=0 and j+1<W:
                if HWlist[i-1][j+1] == "#":
                    count = count +1
            if i+1 <H and j-1>=0:
                if HWlist[i+1][j-1] == "#":
                    count = count +1
            if i+1 <H and j+1<W:
                if HWlist[i+1][j+1] == "#":
                    count = count +1
            Reslist[i] = Reslist[i] + str(count)
        else:
            Reslist[i] = Reslist[i] + "#"
for l in range (0,H):
    print(Reslist[l])

Submission status: Accepted

Solution code:
#全日本汚いコード選手権Pythonの部
H, W = map(int, input().split())
S = []
for i in range(H):
  S.append(list(input()))
L = [[0 for w in range(W)] for h in range(H)]

if H == 1 and W == 1:
  if S[0][0] == ".":
    L[0][0] = 0

elif H == 1:
  
  if S[0][1]=="#":
    L[0][0]+=1
  if S[0][W-2]=="#":
    L[0][W-1]+=1
  for w in range(1,W-1):
    if S[0][w-1]=="#":
      L[0][w]+=1
    if S[0][w+1]=="#":
      L[0][w]+=1

elif W == 1:
  
  if S[1][0]=="#":
    L[0][0]+=1
  if S[H-2][0]=="#":
    L[H-1][0]+=1
  for h in range(1,H-1):
    if S[h-1][0]=="#":
      L[h][0]+=1
    if S[h+1][0]=="#":
      L[h][0]+=1
  
  
else:

  if S[0][1]=="#":
    L[0][0]+=1
  if S[1][0]=="#":
    L[0][0]+=1
  if S[1][1]=="#":
    L[0][0]+=1
  if S[H-1][1]=="#":
    L[H-1][0]+=1
  if S[H-2][0]=="#":
    L[H-1][0]+=1
  if S[H-2][1]=="#":
    L[H-1][0]+=1
  if S[0][W-2]=="#":
    L[0][W-1]+=1
  if S[1][W-1]=="#":
    L[0][W-1]+=1
  if S[1][W-2]=="#":
    L[0][W-1]+=1
  if S[H-1][W-2]=="#":
    L[H-1][W-1]+=1
  if S[H-2][W-1]=="#":
    L[H-1][W-1]+=1
  if S[H-2][W-2]=="#":
    L[H-1][W-1]+=1

  for w in range(1,W-1):
    if S[0][w-1]=="#":
      L[0][w]+=1
    if S[0][w+1]=="#":
      L[0][w]+=1
    if S[1][w-1]=="#":
      L[0][w]+=1
    if S[1][w]=="#":
      L[0][w]+=1
    if S[1][w+1]=="#":
      L[0][w]+=1
    if S[H-1][w-1]=="#":
      L[H-1][w]+=1
    if S[H-1][w+1]=="#":
      L[H-1][w]+=1
    if S[H-2][w-1]=="#":
      L[H-1][w]+=1
    if S[H-2][w]=="#":
      L[H-1][w]+=1
    if S[H-2][w+1]=="#":
      L[H-1][w]+=1

  for h in range(1,H-1):
    if S[h-1][0]=="#":
      L[h][0]+=1
    if S[h+1][0]=="#":
      L[h][0]+=1
    if S[h-1][1]=="#":
      L[h][0]+=1
    if S[h][1]=="#":
      L[h][0]+=1
    if S[h+1][1]=="#":
      L[h][0]+=1
    if S[h-1][W-1]=="#":
      L[h][W-1]+=1
    if S[h+1][W-1]=="#":
      L[h][W-1]+=1
    if S[h-1][W-2]=="#":
      L[h][W-1]+=1
    if S[h][W-2]=="#":
      L[h][W-1]+=1
    if S[h+1][W-2]=="#":
      L[h][W-1]+=1

  for w in range(1,W-1):
    for h in range(1,H-1):
      if S[h-1][w-1]=="#":
        L[h][w]+=1
      if S[h-1][w]=="#":
        L[h][w]+=1
      if S[h-1][w+1]=="#":
        L[h][w]+=1
      if S[h][w-1]=="#":
        L[h][w]+=1
      if S[h][w+1]=="#":
        L[h][w]+=1
      if S[h+1][w-1]=="#":
        L[h][w]+=1
      if S[h+1][w]=="#":
        L[h][w]+=1
      if S[h+1][w+1]=="#":
        L[h][w]+=1

C = [["" for w in range(W)] for h in range(H)]

for w in range(W):
  for h in range(H):
    if S[h][w]==".":
      C[h][w]=str(L[h][w])
    else:
      C[h][w]="#"

o = ""
for c in C:
  o = ""
  for cc in c:
    o += cc
  print(o)