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:
# ABC075B - Minesweeper
h, w = list(map(int, input().rstrip().split()))
lst = [list(input().rstrip()) for _ in range(h)]
emp_lst = ["."] * (w + 2)

for i in lst:
    i.append(".")
    i.insert(0, ".")

lst.append(emp_lst)
lst.insert(0, emp_lst)

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

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

h, w = map(int, input().split())
s_list = [list('.' * (w + 2))]
s_list.extend([list('.' + input() + '.') for i in range(h)])
s_list.append(list('.' * (w + 2)))

for i in range(1, h+1):
    for j in range(1, w+1):
        if s_list[i][j] == '.':
            cnt = 0
            cnt += s_list[i-1][j-1:j+2].count('#')
            cnt += s_list[i][j-1:j+2].count('#')
            cnt += s_list[i+1][j-1:j+2].count('#')
            s_list[i][j] = str(cnt)

for i in s_list[1:-1]:
    print(''.join(i[1:-1]))

Submission status: Accepted

Solution code:
import sys
from collections import Counter

h, w = map(int, input().split())
s_list = [list('.' * (w + 2))]
s_list.extend([list('.' + input() + '.') for i in range(h)])
s_list.append(list('.' * (w + 2)))

def check_mine(v):
    if v == '#':
        return 1
    else:
        return 0

for i in range(1, h+1):
    for j in range(1, w+1):
        if s_list[i][j] == '.':
            cnt = 0
            cnt += s_list[i-1][j-1:j+2].count('#')
            cnt += s_list[i][j-1:j+2].count('#')
            cnt += s_list[i+1][j-1:j+2].count('#')
            s_list[i][j] = str(cnt)

for i in s_list[1:-1]:
    print(''.join(i[1:-1]))

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    h, w = f()
    s = [input() for i in range(h)]
    d = [[0]*w for i in range(h)]

    for i in range(h):
        for j in range(w):
            if s[i][j] == '.':
                cnt = 0
                for x in range(-1,2):
                    for y in range(-1,2):
                        if i+x in range(h) and j+y in range(w):
                            if s[i+x][j+y] == '#':
                                cnt+=1
                d[i][j] = cnt
    
    for i in range(h):
        for j in range(w):
            if s[i][j] == '.':
                print(d[i][j], end = '' if j!=w-1 else '\n')
            else:
                print(s[i][j], end = '' if j!=w-1 else '\n')




solve()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

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

def check_mine(v):
    if v == '#':
        return 1
    else:
        return 0

for i in range(h):
    for j in range(w):
        if s_list[i][j] == '.':
            tmp = 0
            if i != 0:
                if j != 0:
                    tmp += check_mine(s_list[i-1][j-1])
                if j != w-1:
                    tmp += check_mine(s_list[i-1][j+1])
                tmp += check_mine(s_list[i-1][j])

            if i != h-1:
                if j != 0:
                    tmp += check_mine(s_list[i+1][j-1])
                if j != w-1:
                    tmp += check_mine(s_list[i+1][j+1])
                tmp += check_mine(s_list[i+1][j])

            if j != 0:
                tmp += check_mine(s_list[i][j-1])

            if j != w-1:
                tmp += check_mine(s_list[i][j+1])
            
            s_list[i][j] = str(tmp)

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

Submission status: Accepted

Solution code:
H, W = list(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] == '.':
            count = 0
            if y - 1 >= 0:
                if x - 1 >= 0 and field[y-1][x-1] == '#':
                    count += 1
                if field[y-1][x] == '#':
                    count += 1
                if x + 1 < W and field[y-1][x+1] == '#':
                    count += 1

            if y + 1 < H:
                if x - 1 >= 0 and field[y+1][x-1] == '#':
                    count += 1
                if field[y+1][x] == '#':
                    count += 1
                if x + 1 < W and field[y+1][x+1] == '#':
                    count += 1

            if x - 1 >= 0 and field[y][x-1] == '#':
                count += 1
            if x + 1 < W and field[y][x+1] == '#':
                count += 1

            field[y][x] = count

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

Submission status: Accepted

Solution code:
def main():
	H, W = map(int, input().split())
	S = ["-"*(W+2) for _ in range(H+2)]
	offset = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
	for i in range(1, H+1):
		S[i] = "-"+input()+"-"
	ans = [""]*H

	for i in range(1, H+1):
		for j in range(1, W+1):
			if S[i][j] != ".":
				ans[i-1] += S[i][j]
				continue
			count = 0
			for o in offset:
				if S[i+o[0]][j+o[1]] == "#":
					count += 1
			ans[i-1] += str(count)

	for i in range(H):
		print(ans[i])


if __name__ == '__main__':
	main()

Submission status: Accepted

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

dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,1,1,1,0,-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 0 <= x < H and 0 <= y < W and S[x][y] == "#":
                    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())
S = [list(input()) for i in range(H)]

for s in S:
    for i in range(len(s)):
        if s[i] == ".":
            s[i] = 0

for i in range(len(S)):
    for j in range(len(S[i])):
        if S[i][j] == "#":
            if i != 0:
                if S[i - 1][j] != "#":
                    S[i - 1][j] += 1
                if j != 0 and S[i - 1][j - 1] != "#":
                    S[i - 1][j - 1] += 1
                if j != W - 1 and S[i - 1][j + 1] != "#":
                    S[i - 1][j + 1] += 1
            if j != 0 and S[i][j - 1] != "#":
                S[i][j - 1] += 1
            if j != W - 1 and S[i][j + 1] != "#":
                S[i][j + 1] += 1
            if i != H - 1:
                if S[i + 1][j] != "#":
                    S[i + 1][j] += 1
                if j != 0 and S[i + 1][j - 1] != "#":
                    S[i + 1][j - 1] += 1
                if j != W - 1 and S[i + 1][j + 1] != "#":
                    S[i + 1][j + 1] += 1

for s in S:
    print("".join(map(str,s)))

Submission status: Accepted

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


def get_count(Ss,row,column):
    cs = 0
    for r in [-1,0,1]:
        for c in [-1,0,1]:
            if row + r < 0 or row + r >= H or column + c < 0 or column + c >= W:
                continue
            elif Ss[row + r][column + c] == '#':
                cs += 1
    return cs



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


for r in range(H):
    for c in range(W):
        if Ss[r][c] == '#':
            print('#',end ="")
        else:
            print(get_count(Ss,r,c),end="")
    print("")

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if b[i][j] == '#':
            n[i][j] += 1
            n[i+1][j] += 1
            n[i+2][j] += 1
            n[i][j+1] += 1
            n[i][j+2] += 1
            n[i+2][j+1] += 1
            n[i+2][j+2] += 1
            n[i+1][j+2] += 1

for i in range(H):
    for j in range(W):
        if b[i][j] == ".":
            b[i][j] = str(n[i+1][j+1])

print('\n'.join(map(lambda x : ''.join(x),b)))

Submission status: Accepted

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

def search(x, y):
    return sum((s[x + i][y + j] == '#') +0 for i in range(-1, 2) for j in range(-1, 2))

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

Submission status: Accepted

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

comp = [[0,-1],[0,1],[1,-1],[1,1],[1,0],[-1,-1],[-1,0],[-1,1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        c = 0
        for e in comp:
            x = j + e[1]
            y = i + e[0]
            if x < 0 or x > W-1:
                continue
            if y < 0 or y > H-1:
                continue
            if S[y][x] == '#':
                c += 1
        S[i][j] = str(c)
    print(''.join(S[i]))

Submission status: Accepted

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

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

ans = [[] for i in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            mine = 0
            for k in range(8):
                y = i + dy[k]
                x = j + dx[k]
                if 0 <= y <= H-1 and 0 <= x <= W-1 and S[y][x] == '#':
                    mine += 1
            ans[i].append(mine)
        else:
            ans[i].append('#')

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

Submission status: Accepted

Solution code:
D = [-1,0,1]

def count(y,x):
    res = 0
    for dy in D:
        for dx in D:
            ny = y+dy
            nx = x+dx
            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == "#":
                res += 1
    return res
    
[H,W] = list(map(int,input().split()))
S = [input() for i in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] != "#":
            cnt = count(i,j)
            S[i] = S[i][:j] + str(cnt) + S[i][j+1:]
for i in range(H):
    print(S[i])

Submission status: Accepted

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

dummy = ''
for i in range(W):
    dummy += '.'

extended = [dummy] + grid + [dummy]
extended = ['.'+v+'.' for v in extended]

result = []
for h in range(1, H+1):
    row = []
    for w in range(1, W+1):
        if extended[h][w] == '#':
            row.append('#')
        else:
            count = 0
            if extended[h-1][w-1] == '#': count+=1
            if extended[h-1][w] == '#': count+=1
            if extended[h-1][w+1] == '#': count+=1
            if extended[h][w-1] == '#': count+=1
            if extended[h][w+1] == '#': count+=1
            if extended[h+1][w-1] == '#': count+=1
            if extended[h+1][w] == '#': count+=1
            if extended[h+1][w+1] == '#': count+=1
            row.append(count)
    result.append(row)

out = '\n'.join([''.join(map(str,r)) for r in result])
print(out)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc075/tasks/abc075_b
hw_list = list(map(int, input().split()))
h_max = hw_list[0]
w_max = hw_list[1]

s_list = []

for i in range(h_max):
    work_list = input()
    s_list.append(work_list)
answer_list = []

for h in range(h_max):
    work_list = s_list[h]
    work_answer = []
    for w, target in enumerate(work_list):
        if target == '.':
            tonari_list = [(w-1, h-1), (w, h-1), (w+1, h-1),
                           (w-1, h), (w+1, h),
                           (w-1, h+1), (w, h+1), (w+1, h+1)]
            count = 0
            for x, y in tonari_list:
                if 0 <= x < w_max and 0 <= y < h_max:
                    if s_list[y][x] == '#':
                        count += 1
                else:
                    continue
            work_answer.append(str(count))
        else:
            work_answer.append('#')
    answer_list.append(work_answer)

for a_list in answer_list:
    print(''.join(a_list))

Submission status: Accepted

Solution code:
import copy

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for dir in range(8):
                if 0 <= i + dy[dir] < H and 0 <= j + dx[dir] < W:
                    cnt += (S[i + dy[dir]][j + dx[dir]] == "#")
                
            ans[i][j] = str(cnt)
for h in range(H):
    print("".join(ans[h]))

Submission status: Accepted

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

def count(ch):
  return 1 if ch == '#' else 0

for i in range(H):
  for j in range(W):
    num = 0
    if mine[i][j] == '#':
      continue
    else:
      for k in range(-1,2):
        for l in range(-1,2):
          if 0<=i+k<H and 0<=j+l<W:
            num += (1 if mine[i+k][j+l]=='#' else 0) 
      mine[i][j] = num
      
for i in range(H):
  for j in range(W):
    print(mine[i][j],end='')
  print()

Submission status: Accepted

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

def count(ch):
  return 1 if ch == '#' else 0

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
Mine=[]
for i in range(H):
  Mine.append(list(input()))
for i in range(H):
  answer=[]
  for j in range(W):
    if Mine[i][j]=='#':
      answer.append('#')
    else:
      count=0
      if i!=0:
        if j!=0:
          if Mine[i-1][j-1]=='#':
            count+=1
        if j!=W-1:
          if Mine[i-1][j+1]=='#':
            count+=1
        if Mine[i-1][j]=='#':
          count+=1
      if i!=H-1:
        if j!=0:
          if Mine[i+1][j-1]=='#':
            count+=1
        if j!=W-1:
          if Mine[i+1][j+1]=='#':
            count+=1
        if Mine[i+1][j]=='#':
          count+=1
      if j!=0:
        if Mine[i][j-1]=='#':
          count+=1
      if j!=W-1:
        if Mine[i][j+1]=='#':
          count+=1
      answer.append(str(count))
  print("".join(answer))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def calc(x, y):
    x1 = x - 1
    x2 = x - 1
    x3 = x - 1
    x4 = x
    x5 = x
    x6 = x + 1
    x7 = x + 1
    x8 = x + 1
    y1 = y - 1
    y2 = y
    y3 = y + 1
    y4 = y - 1
    y5 = y + 1
    y6 = y - 1
    y7 = y
    y8 = y + 1
    return x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8

h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(list(input()))
ans = []
for i in range(h * w):
    y = i % w
    x = i // w
    x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8 = calc(x, y)
    s_list = [[x1, y1], [x2, y2], [x3, y3], [x4, y4],
              [x5, y5], [x6, y6], [x7, y7], [x8, y8]]
    if s[x][y] == ".":
        c = 0
        for xx, yy in s_list:
            if ((xx >= 0) & (yy >= 0)) & ((xx < h) & (yy < w)):
                if s[xx][yy] == "#":
                    c += 1
        ans.append(str(c))
    else:
        ans.append("#")
sss = ""
for i, anss in enumerate(ans):
    if (i % w) == w - 1:
        sss += anss + "\n"
    else:
        sss += anss
print(sss[:-1])

Submission status: Accepted

Solution code:
h, w = [int(c) for c in input().split()]

board = [input() for _ in range(h)]

directions = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]]


def find_bom(board, row_i, i, direction):
    if -1 < row_i + direction[0] < h and -1 < i + direction[1] < w:
        if board[row_i+direction[0]][i+direction[1]] == "#":
            return 1
        else:
            return 0
    else:
        return 0


new_board = []
for i in range(h):
    new_row = ''
    for j in range(w):
        if board[i][j] != "#":
            bom_count = 0
            for d in directions:
                bom_count += find_bom(board, i, j, d)
            new_row += str(bom_count)
        else:
            new_row += '#'
    new_board.append(new_row)

print('\n'.join(new_board))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[input() for i in range(h)]
cl=[[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        if l[i][j]=="#":
            for a in {max(0,i-1),i,min(h-1,i+1)}:
                for b in {max(0,j-1),j,min(w-1,j+1)}:
                    cl[a][b]+=1
cl=[[str(cl[i][j]) if l[i][j]!="#" else "#" for j in range(w)] for i in range(h)]
for i in range(h):
    print("".join(cl[i]))

Submission status: Accepted

Solution code:
def checkmine(i,j):
    count=0
    for a in range(-1,2):
        for b in range(-1,2):
            if 0<=a+i<n and 0<=b+j<m:
                if field[a+i][b+j]=="#":count+=1
    return count

n,m=map(int,input().split())
field=[]
for i in range(n):field.append(list(input()))
for i in range(n):
    for j in range(m):
        if field[i][j]==".":field[i][j]=checkmine(i,j)
for i in field:print(''.join(map(str,i)))

Submission status: Accepted

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

def cnt(y,x):
    if y >= h or x >= w or y < 0 or x < 0:
        return 0
    if lis[y][x] == '#':
        return 1
    return 0

for y in range(h):
    for x in range(w):
        ans = 0
        if lis[y][x] == '.':
            ans += cnt(y+1,x-1)
            ans += cnt(y+1,x)
            ans += cnt(y+1,x+1)
            ans += cnt(y,x-1)
            ans += cnt(y,x+1)
            ans += cnt(y-1,x-1)
            ans += cnt(y-1,x)
            ans += cnt(y-1,x+1)
            ans_lis[y][x] = str(ans)
        else:
            ans_lis[y][x] = '#'
for x in range(h):
    print(''.join(ans_lis[x]))

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            ct = 0
            for k in range(8):
                x = j + [0, 1, 0, -1, 1, 1, -1, -1][k]
                y = i + [1, 0, -1, 0, 1, -1, 1, -1][k]
                if 0 <= x < W and 0 <= y < H:
                    if S[y][x] == '#':
                        ct += 1
            S[i][j] = str(ct)

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

Submission status: Accepted

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

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

def show(arr):
    text = ''
    for i in range(1, len(arr)-1):
        if i == 0:
            continue
        elif i == len(arr)-2:
            text += ''.join(arr[i][1:-1])
        else:
            text += ''.join(arr[i][1:-1]) + '\n'
    return text

print(show(exS))

Submission status: Accepted

Solution code:
line = input().rstrip().split(' ')
h = int(line[0])
w = int(line[1])

# 2d array which represents glid input
glid = []

for i in range(h):
    glid.append(list(input()))

for y in range(h):
    for x in range(w):
        # if the square is bomb
        if glid[y][x] == '#':
            continue
        
        num = 0
        for dx,dy in ((-1,-1), (0,-1), (1,-1), (-1,0), (1,0), (-1,1), (0,1), (1,1)):
            ny = y + dy
            nx = x + dx
            if ny < 0 or nx < 0 or ny >= h or nx >= w:
                continue
            if glid[ny][nx] == '#':
                num += 1
        glid[y][x] = num

# output
for y in range(h):
    line = ''
    for x in range(w):
        line += str(glid[y][x])
    print(line)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
# W: 一行の文字列
# H: 行数

glid = [list(input()) for i in range(h)]
ans = [[0] * w for i in range(h) ]

#print("{},{}".format(w, h))

for i in range(h):
    for j in range(w):
        # 各文字に対し探索を行う
        if glid[i][j] == '#':
            ans[i][j] = '#'
            continue
            
        count = 0
        for k in range(-1,2):
            if i+k < 0 or i+k >= h:
                continue
            for l in range(-1,2):
                if j+l < 0 or j+l >= w:
                    continue
                #print("{},{}".format(i+k, j+l))
                if glid[i+k][j+l] == '#':
                    count += 1
        ans[i][j] = count

for i in range(h):
  #print(ans[i])
  print(''.join(map(str, ans[i])))  
  #print("\n")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [["."] * (W + 2)] + [["."] + list(input()) + ["."] for _ in range(H)] + [["."] * (W + 2)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == "#":
            continue
        count = 0
        for dh in range(-1, 2):
            for dw in range(-1, 2):
                if S[h + dh][w + dw] == "#":
                    count += 1
        S[h][w] = str(count)
    print("".join(S[h][1:W+1]))

Submission status: Accepted

Solution code:
def count_bombs(S, h, w):
    count = 0
    for i in range(h-1, h+2):
        for j in range(w-1, w+2):
            if i < 0 or j < 0 or i >= len(S) or j >= len(S[0]):
                continue
            if S[i][j] == "#":
                count += 1
    return count


H, W = map(int, input().split())
S = [input() for _ in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            print("#", end="")
            continue
        print(count_bombs(S, h, w), end="")
    print()

Submission status: Accepted

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

def count(y, x):
	directions = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))
	c = 0
	for d in directions:
		px, py = x + d[0], y + d[1]
		if px < 0 or W <= px or py < 0 or H <= py:
			continue
		if stage[py][px] == "#":
			c += 1
	return c

for y in range(H):
	for x in range(W):
		if stage[y][x] == ".":
			stage[y][x] = count(y, x)

for s in stage:
	print(*s, sep="")

Submission status: Accepted

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

from itertools import product

for h, w in product(range(H), range(W)):
  if SS[h][w] != '#':
    bom = 0
    for dh, dw in product([-1, 0, 1], repeat=2):
      bh, bw = h+dh, w+dw
      if bh < 0 or H-1 < bh:
        continue
      if bw < 0 or W-1 < bw:
        continue
      if SS[bh][bw] == '#':
        bom += 1
    SS[h][w] = str(bom)
print(*[''.join(s) for s in SS], sep='\n')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[]
for i in range(H):
    S.append(list(input()))
for h in range(H):
    for w in range(W):
        #print('S[h][w] ',S[h][w])
        if S[h][w]=='#':
            continue
        x1 = max(0,w-1)
        x2 = min(W,w+2)
        y1 = max(0,h-1)
        y2 = min(H,h+2)
        cnt=0
        for h2 in range(y1,y2):
            for w2 in range(x1,x2):
                if S[h2][w2]=='#':
                    cnt+=1
        #print(h,w,cnt)
        S[h][w]=str(cnt)
for i in range(H):
    print(''.join(S[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
b=["n"*(w+2)]
for i in range(h):b.append("n"+input()+"n")
b.append(b[0])
def num(i,j):
  a=0
  def same(i,j):
    global b
    if b[i][j]=="#":return 1
    return 0
  a+=same(i-1,j-1)
  a+=same(i-1,j)
  a+=same(i-1,j+1)
  a+=same(i,j-1)
  a+=same(i,j+1)
  a+=same(i+1,j-1)
  a+=same(i+1,j)
  a+=same(i+1,j+1)
  return a
for i in range(len(b)):
  if 0<i<h+1:
    for j in range(len(b[i])):
      if b[i][j]==".":b[i]=b[i][:j]+str(num(i,j))+b[i][j+1:]
for i in range(1,h+1):print(b[i][1:w+1])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
T=["."+input()+"." for i in range(h)]
S="."*(w+2)
T.insert(0,S)
T.append(S)
Ans=["" for i in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    count=0
    if T[i][j]=="#":
      A="#"
    elif T[i][j]==".":
      for m in range(3):
        for n in range(3):
          if T[i-1+m][j-1+n]=="#":
            count+=1
          A=str(count)
    Ans[i-1]+=A
print(*Ans,sep="\n")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=[list(input()) for _ in range(h)]
dx=[-1,0,1]
dy=[-1,0,1]
for i in range(h):
    for j in range(w):
        count=0
        if S[i][j]=='#':
            continue
        else:
            for l in dx:
                for m in dy:
                    if i+l<0 or i+l>h-1 or j+m<0 or j+m>w-1:
                        continue
                    if S[i+l][j+m]=='#':
                        count+=1
        S[i][j]=str(count)
for i in S:
    k=''.join(i)
    print(k)

Submission status: Accepted

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

ans = [0] * H
for i in range(H):
    ans[i] = [0] * W

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

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 i in range(h)]
dx = [-1,0,1]
dy = [-1,0,1]
for i in range(h):
	for j in range(w):
		count = 0
		if s[i][j] == '#':
			continue
		if s[i][j] == '.':
			for l in dx:
				for m in dy:
					if i+l< 0 or i+l > h-1 or j+m < 0 or j+m > w-1:
						continue
					if s[i+l][j+m] == '#':
						count += 1
		s[i][j] = str(count)
for i in s:
	k = ''.join(i)
	print(k)

Submission status: Accepted

Solution code:
H, W = [int(i) for i in input().split()]
minelist = [list(input()) for i in range(H)]
dx = [-1, 0 , 1]
dy = [-1, 0 , 1]
for i in range(H):
    for j in range(W):
        count = 0
        if minelist[i][j] == "#":
            continue
        if minelist[i][j] == ".":
            for l in dx:
                for m in dy:
                    if (i + l < 0) or (i + l > H -1 ) or (j + m < 0) or (j + m > W -1):
                        continue
                    if minelist[i + l][j + m] == "#":
                        count += 1
        minelist[i][j] = str(count)
for i in minelist:
    k = "".join(i)
    print(k)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = ['.' + input() + '.' for i in range(H)]
S.insert(0, '.' * (W + 2))
S.insert(len(S), '.' * (W + 2))
def mine(x):
    if x == '#':
        count.append(1)
for i in range(1, H+1):
    tmp = ''
    for j in range(1, W+1):
        if S[i][j] == '#':
            tmp += '#'
        else:
            count = []
            mine(S[i-1][j-1]); mine(S[i-1][j]); mine(S[i-1][j+1])
            mine(S[i][j-1]);                    mine(S[i][j+1])
            mine(S[i+1][j-1]); mine(S[i+1][j]); mine(S[i+1][j+1])
            tmp += str(sum(count))
    print(tmp)