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())
ans = []
board = []
for _ in range(H):
    row = list(input())
    board.append(row)
for i in range(H):
    row = ""
    for q in range(W):
        neighbor = []
        if q > 0: #左側
            neighbor.append(board[i][q-1]) #真左
            if i > 0:
                neighbor.append(board[i-1][q-1]) #左上
            if i < (H - 1):
                neighbor.append(board[i+1][q-1]) #左下
        if i > 0: #真上
            neighbor.append(board[i-1][q])
        if q < (W - 1): #右側
            neighbor.append(board[i][q+1]) #真右
            if i > 0:
                neighbor.append(board[i-1][q+1]) #右上
            if i < (H - 1):
                neighbor.append(board[i+1][q+1]) #右下
        if i < (H - 1): #真下
            neighbor.append(board[i+1][q])
        if board[i][q] == "#":
            row += board[i][q]
        else:
            row += str(neighbor.count("#"))
    print(row)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
ans = []
board = []
for _ in range(H):
    row = list(input())
    board.append(row)
for i in range(H):
    row = ""
    for q in range(W):
        neighbor = []
        if q > 0: #左側
            neighbor.append(board[i][q-1]) #真左
            if i > 0:
                neighbor.append(board[i-1][q-1]) #左上
            if i < (H - 1):
                neighbor.append(board[i+1][q-1]) #左下
        if i > 0: #真上
            neighbor.append(board[i-1][q])
        if q < (W - 1): #右側
            neighbor.append(board[i][q+1]) #真右
            if i > 0:
                neighbor.append(board[i-1][q+1]) #右上
            if i < (H - 1):
                neighbor.append(board[i+1][q+1]) #右下
        if i < (H - 1): #真下
            neighbor.append(board[i+1][q])
        if board[i][q] == "#":
            row += board[i][q]
        else:
            row += str(neighbor.count("#"))
    print(row)

Submission status: Accepted

Solution code:
import sys
r,c = map(int,input().split())
masume = [[0 for i in range(c)] for j in range(r)]
for i in range(r):
	masume[i] = list(input())

for ri in range(r):
	for ci in range(c):
		if masume[ri][ci] == '#':
			sys.stdout.write(masume[ri][ci])
			continue
		count = 0
		for ar in [-1,0,1]:
			for ac in [-1,0,1]:
				chkr = ri + ar
				chkc = ci + ac
				if chkr >= 0 and chkr < r and chkc >= 0 and chkc < c and masume[chkr][chkc] == '#':
					count+= 1
		sys.stdout.write(str(count))
	print("")

Submission status: Accepted

Solution code:
import numpy as np
h, w = map(int, input().split())
num_array = np.zeros((h, w))
space_array = np.ones((h, w))

mine_map = np.array(
    [np.array([0 if i == "." else 1 for i in input()]) for j in range(h)])
space_array = mine_map.copy()

for i, s in enumerate(mine_map):
    for j, m in enumerate(s):
        if m == 1: # m=mass
            il, iu = i-1, i+2
            jl, ju = j-1, j+2
            il, jl = max(il, 0), max(jl, 0)
            iu, ju = min(iu, h), min(ju, w)
            num_array[il:iu, jl:ju] += 1

for i, s in enumerate(space_array):
    out = ["#" if m == 1 else str(int(num_array[i,j])) for j, m in enumerate(s)]
    print("".join(out))

Submission status: Accepted

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

for i in range(h):
    l = ''
    for j in range(w):
        if m[i][j] == '#':
            l += '#'
        else:
            l += str(sum([t[max(0,j-1):min(w,j+2)].count('#') for t in m[max(0,i-1):min(h,i+2)]]))
    print(l)

Submission status: Accepted

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

mylist=[[0 for j in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            if i-1 >= 0 and j-1 >= 0:
                mylist[i-1][j-1]+=1
            if i-1 >= 0:
                mylist[i-1][j]+=1
            if i-1 >= 0 and j+1 <= w-1:
                mylist[i-1][j+1]+=1
            if j-1 >= 0:
                mylist[i][j-1]+=1
            if j+1 <= w-1:
                mylist[i][j+1]+=1
            if i+1 <= h-1 and j-1 >= 0:
                mylist[i+1][j-1]+=1
            if i+1 <= h-1:
                mylist[i+1][j]+=1
            if i+1 <= h-1 and j+1 <= w-1:
                mylist[i+1][j+1]+=1

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0] * (n)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


def solve():
    H, W = il()
    S = imi(H, str)
    from itertools import product

    dst = ((i, j) for i, j in product(range(-1, 2), repeat=2))
    dst = list(dst)
    B = [[0] * W for _ in range(H)]
    sharp_pos = []
    for i, j in product(range(H), range(W)):
        if S[i][j] == "#":
            B[i][j] = "#"
            sharp_pos.append((i, j))
    for i, j in sharp_pos:
        for d in dst:
            x = i + d[1]
            y = j + d[0]
            if 0 <= x < H and 0 <= y < W:
                if S[i][j] == "#" and S[x][y] == ".":
                    B[x][y] += 1
    for b in B:
        print("".join(map(str, b)))


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [0 for _ in range(H)]
for i in range(H):
    S[i] = 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):
        count = 0
        if S[i][j] == '#':
            continue
        else:
            for k in range(8):
                ni = i + dx[k]
                nj = j + dy[k]
                if 0 <= ni and ni < H and 0 <= nj and nj < W:
                    if S[ni][nj] == '#':
                        count += 1
            S[i][j] = str(count)

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


h, w = LI()
M = [SI() for _ in range(h)]

d = ((0, 1), (-1, 1), (-1, 0), (-1, -1),
     (0, -1), (1, -1), (1, 0), (1, 1))

ans = []
for y in range(h):
    row = ''
    for x in range(w):
        cnt = 0
        if M[y][x] == '#':
            row += '#'
            continue

        for dy, dx in d:
            yy, xx = y + dy, x + dx
            if 0 <= yy < h and 0 <= xx < w:
                if M[yy][xx] == '#':
                    cnt += 1
        row += str(cnt)
    ans.append(row)

print(*ans, sep='\n')

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
s=[input()for i in range(a)]
for i in range(a):
    for j in range(b):
        c=0
        if s[i][j]==".":
            p=s[i]
            s[i]=p[:j]+str(sum([t[max(0,j-1):min(b,j+2)].count("#")for t in s[max(0,i-1):min(a,i+2)]]))+p[j+1:]
for i in range(a):
    print(s[i])

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
s=["."*(b+2)]+["0"+input()+"0"for i in range(a)]+["."*(b+2)]
for i in range(1,a+1):
    for j in range(1,b+1):
        c=0
        if s[i][j]==".":
            if s[i-1][j]=="#":
                c+=1
            if s[i][j-1]=="#":
                c+=1
            if s[i+1][j]=="#":
                c+=1
            if s[i][j+1]=="#":
                c+=1
            if s[i-1][j-1]=="#":
                c+=1
            if s[i-1][j+1]=="#":
                c+=1
            if s[i+1][j-1]=="#":
                c+=1
            if s[i+1][j+1]=="#":
                c+=1
            t=s[i]
            s[i]=t[:j]+str(c)+t[j+1:]
for i in range(1,a+1):
    t=s[i]
    print(t[1:b+1])

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([t[max(0,j-1):min(w,j+2)].count('#') for t in s[max(0,i-1):min(h,i+2)]]))
    print(l)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
matrix = []
for i in range(h):
  matrix.append(list(input()))
output = [''] * h
for i in range(h):
  matrix[i] = ['.'] + matrix[i] + ['.']
blank = ['.'] * (w + 2)
matrix = [blank] + matrix + [blank]
for i in range(h):
  for j in range(w):
    if (matrix[i + 1][j + 1] == '#'):
      output[i] += '#'
    else:
      o = ord('0')
      for k in range(3):
        for l in range(3):
          if (matrix[i + k][j + l] == '#'):
            o += 1
      output[i] += chr(o)
for i in range(h):
  print(output[i])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[]
for i in range(h):
  l.append(input())
al=[]
for i in range(h):
  s=''
  for j in range(w):
    if l[i][j]=='#':
      s+='#'
      continue
    c=0
    if i>0 and j>0 and l[i-1][j-1]=='#':
      c+=1
    if i>0 and l[i-1][j]=='#':
      c+=1
    if i>0 and j<w-1 and l[i-1][j+1]=='#':
      c+=1
    if j>0 and l[i][j-1]=='#':
      c+=1
    if j<w-1 and l[i][j+1]=='#':
      c+=1
    if i<h-1 and j>0 and l[i+1][j-1]=='#':
      c+=1
    if i<h-1 and l[i+1][j]=='#':
      c+=1
    if i<h-1 and j<w-1 and l[i+1][j+1]=='#':
      c+=1
    s+=str(c)
  al.append(s)
for i in range(h):
  print(al[i])

Submission status: Accepted

Solution code:
def surround(h, w):
    ret = 0
    for i in [-1, 0, 1]:
        ret += int(ans[h-1][w+i]=="#")
        ret += int(ans[h+1][w+i]=="#")
    for i in [-1, 1]:
        ret += int(ans[h][w+i]=="#")
    return str(ret)


Height, Width = map(int, input().split())
ans = [list(input()) for _ in range(Height)]
ans = [["."]*Width] + ans + [["*"]*Width]
# print(ans)
for i in range(Height+2):
    ans[i] = ["."] + ans[i] + ["."]
for h in range(1, Height+1):
    for w in range(1, Width+1):
        if ans[h][w] == "#":
            continue
        ans[h][w] = surround(h, w)

ans = ans[1:-1]
for i in range(Height):
    print("".join(ans[i][1:-1]))

Submission status: Accepted

Solution code:
a, b = map(int, input().split())

A = []
A.append(list('.'*(b+2)))
for i in range(a):
    A.append(list('.'+input()+'.'))
A.append(list('.'*(b+2)))
count=0
for j in range(1, a+1):
    for k in range(1, b+1):
        if(A[j][k] == '#'):
            print('#', end='')
        else:
            for l in range(-1, 2):
                for m in range(-1, 2):
                    if(A[j+l][k+m]== '#'):
                        count+=1
            print(count, end='')
        count=0
    print()

Submission status: Accepted

Solution code:
from sys import stderr, setrecursionlimit
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, sep=" ", end="\n"):
    for item in args:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)

h, w = getInts()
grid = [['.' for _ in range(w+2)]]
for _ in range(h):
    tmp = ['.']
    tmp.extend(list(input()))
    tmp.append('.')
    grid.append(tmp)
grid.append(['.' for _ in range(w+2)])

for i in range(1,h+1):
    for j in range(1,w+1):
        if grid[i][j] == '#':
            continue
        count = 0
        for k in range(-1,2):
            for l in range(-1,2):
                if grid[i+k][j+l] == '#':
                    count += 1
        grid[i][j] = count
for l in grid[1:-1]:
    print(*l[1:-1], sep='')

Submission status: Accepted

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

game = []
game.append([0 for _ in range(W + 2)])
game.extend([[0 if c == '.' else -1 for c in line] for line in S])
game.append([0 for _ in range(W + 2)])

for y in range(H + 2):
    for x in range(W + 2):
        if game[y][x] == -1:
            for i in (-1, 0, 1):
                for j in (-1, 0, 1):
                    if game[y + i][x + j] != -1:
                        game[y + i][x + j] += 1

for line in game[1:-1]:
    print(''.join(map(str, line[1:-1])).replace('-1', '#'))

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([t[max(0,j-1):min(w,j+2)].count("#") for t in s[max(0,i-1):min(h,i+2)]]))
    print(l)
#コピペです。

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
ms=[]
for i in range(h):
    m=input()
    ms.append(list(m))
count=0
for i in range(h):
    for j in range(w):
        if ms[i][j]==".":
            if i>0 and j>0:
                if ms[i-1][j-1]=="#":
                    count+=1
            if i>0 :
               if ms[i-1][j]=="#":
                    count+=1
            if i>0 and w-1>j:
                if ms[i-1][j+1]=="#":
                    count+=1
            if j>0 :
                if ms[i][j-1]=="#":
                    count+=1
            if w-1>j :
                if ms[i][j+1]=="#":
                    count+=1
            if h-1>i and j>0 :
                if ms[i+1][j-1]=="#":
                    count+=1
            if h-1>i:
                if ms[i+1][j]=="#":
                    count+=1
            if h-1>i and w-1>j:
                if ms[i+1][j+1]=="#":
                    count+=1
            ms[i][j]=str(count)
            count=0

for i in range(h):
    temp="".join([ms[i][j] for j in range(w)])
    print(temp)

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 += s[i][j]
        else:
            l += str(sum([t[max(0,j-1):min(j+2,w)].count('#')\
                     for t in s[max(0,i-1):min(h,i+2)]]))
    print(l)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h, w = [int(i) for i in input().split()]
s = [list(input()) for _ 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):
        count = 0
        if s[i][j] == '#':
            continue
        else:
            for d in range(8):
                if (0 <= i+dy[d] < h) and (0 <= j+dx[d] < w):
                    if s[i+dy[d]][j+dx[d]] == '#':
                        count += 1
            s[i][j] = str(count)

for i in range(h):
    print(''.join(s[i]))

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
map=[['.' for _ in range(W+2)] for _ in range(H+2)]
for i in range(H):
    s=list(input())
    map[i+1]=['.']+s+['.']
for i in range(H):
    for j in range(W):
        if map[i+1][j+1]=='.':
            cnt=0
            if map[i][j+1]=='#':
                cnt+=1
            if map[i][j+2]=='#':
                cnt+=1
            if map[i+1][j+2]=='#':
                cnt+=1
            if map[i+2][j+2]=='#':
                cnt+=1
            if map[i+2][j+1]=='#':
                cnt+=1
            if map[i+2][j]=='#':
                cnt+=1
            if map[i+1][j]=='#':
                cnt+=1
            if map[i][j]=='#':
                cnt+=1
            map[i+1][j+1]=str(cnt)
for i in range(H):
    for j in range(W):
        print(map[i+1][j+1],end='')
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
count = 0
l = [''] * H
S_new = S
for j in range(len(S)):
    S_new[j] = '.' + S[j] + '.'
S_new.insert(H, '.'*(W+2))
S_new.insert(0, '.'*(W+2))
# for i in range(len(S_new)):
#     print(S_new[i])
for i in range(1, H+1):
    for j in range(1, W+1):
        if S_new[i][j] == '#':
            l[i-1] += '#'
        else:
            for k in range(-1, 2):
                for s in range(-1, 2):
                    if S_new[i-k][j-s] == '#':
                        count += 1
            l[i-1] += str(count)
            count = 0
for i in range(len(l)):
    print(l[i])

Submission status: Accepted

Solution code:
g,r=map(int,input().split())
A=["."*(r+2)]
for i in range(g):
    w="."+input()+"."
    A.append(w)
A.append("."*(r+2))
for i in range(1,g+1):
    for j in range(1,r+1):
        if A[i][j]==".":
            B=[]
            for m in [i-1,i,i+1]:
                for k in [j-1,j,j+1]:
                    B.append(A[m][k])
            A[i]=A[i][1:r+1:].replace(".",str(B.count("#")),1)
            A[i]="."+A[i]+"."
for i in range(1,g+1):
    print(A[i][1:r+1:])

Submission status: Accepted

Solution code:
def abc075_b():
    h, w = map(int, input().split())
    s = [None] * (h + 2)
    ans = [[None] * w for _ in range(h)]

    s[0] = s[-1] = ["."] * (w + 2)
    for i in range(1, h + 1):
        s[i] = ["."] + list(input()) + ["."]

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

    for ln in ans:
        print("".join(ln))


abc075_b()

Submission status: Accepted

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

if h == 1 and w == 1:
    if s[0][0] == '#':
        v = '#'
    else:
        v = 0
    t[0][0] = str(v)
    print(''.join(t[0]))
    
elif h ==1 and w !=1:
    for k in range(w):
        if k == 0:
            u = s[0][1]
        elif k == w-1:
            u = s[0][k-1]
        else:
            u = s[0][k-1] +s[0][k+1]
        v = u.count('#')
        if s[0][k] == '#':
            v = '#'
        t[0][k] = str(v)
    print(''.join(t[0]))

elif h !=1 and w == 1:
    for j in range(h):
        if j == 0:
            u = s[1][0]
        elif j == h-1:
            u = s[j-1][0]
        else:
            u = s[j-1][0] + s[j+1][0]
        v = u.count('#')
        if s[j][0] == '#':
            v = '#'
        t[j][0] = str(v)
        print(''.join(t[j]))

else:
    for j in range(h):
        if j == 0:
            for k in range(w):
                if k == 0:
                    u = s[0][1] + s[1][0] + s[1][1]
                elif k == w-1:
                    u = s[0][k-1] +s[1][k-1] + s[1][k]
                else:
                    u = s[0][k-1] +s[0][k+1] + s[1][k-1] + s[1][k] + s[1][k+1]
                v = u.count('#')
                if s[j][k] == '#':
                    v = '#'
                t[j][k] = str(v)
        elif j == h-1:
            for k in range(w):
                if k == 0:
                    u = s[j][1] + s[j-1][0] + s[j-1][1]
                elif k == w-1:
                    u = s[j][k-1] +s[j-1][k-1] + s[j-1][k]
                else:
                    u = s[j][k-1] +s[j][k+1] + s[j-1][k-1] + s[j-1][k] + s[j-1][k+1]
                v = u.count('#')
                if s[j][k] == '#':
                    v = '#'
                t[j][k] = str(v)
        else:
             for k in range(w):
                if k == 0:
                    u = s[j][1] + s[j-1][0] + s[j-1][1] + s[j+1][0] + s[j+1][1]
                elif k == w-1:
                    u = s[j][k-1] +s[j-1][k-1] + s[j-1][k] + s[j+1][k-1] + s[j+1][k]
                else:
                    u = s[j][k-1] +s[j][k+1] + s[j-1][k-1] + s[j-1][k] + s[j-1][k+1] + s[j+1][k-1] + s[j+1][k] + s[j+1][k+1]
                v = u.count('#')
                if s[j][k] == '#':
                    v = '#'
                t[j][k] = str(v)
        print(''.join(t[j]))

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(0,H):
    for j in range(0,W):
        count=0

        if le[i][j]=='#':
            answer[i][j]='#'
            continue

        if (i!=H-1):
            if(le[i+1][j]=='#'):
                count+=1

            if (j!=W-1):
                if(le[i+1][j+1]=='#'):
                    count+=1

            if (j!=0):
                if(le[i+1][j-1]=='#'):
                    count+=1


        if (i!=0):
            if(le[i-1][j]=='#'):
                count+=1
            if (j!=W-1):
                if(le[i-1][j+1]=='#'):
                    count+=1

            if (j!=0):
                if(le[i-1][j-1]=='#'):
                    count+=1


        if (j!=W-1):
            if(le[i][j+1]=='#'):
                count+=1

        if (j!=0):
            if(le[i][j-1]=='#'):
                count+=1

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


for i in range(0,H):
    aaa=''
    for j in range(0,W):
        aaa+=answer[i][j]
    print(aaa)

Submission status: Accepted

Solution code:
def add(i, j):
    global count, H, W
    if 0 <= i < H and 0 <= j < W:
        count[i][j] += 1

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

count = [[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, 1):
                for h in range(-1, 2, 1):
                    if k == h == 0:
                        continue
                    else:
                        add(i + k, j + h)

ans = [[""] * W for _ in range(H)]

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

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

Submission status: Accepted

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

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

dh = [-1, -1, -1, 0, 0, 1, 1, 1]
dw = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
	for j in range(W):
		if SL[i][j] == "#":
			pass
		elif SL[i][j] == ".":
			c = 0
			for k in range(8):
				if 0 <= i + dh[k] and i + dh[k] <= H - 1 and 0 <= j + dw[k] and j + dw[k] <= W - 1:
					if SL[i + dh[k]][j + dw[k]] == "#":
						c += 1
			SL[i][j] = str(c)
		else:
			pass

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

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(1,g+1):
  for j in range(1,r+1):
    if A[i][j]==".":
      B=[]
      for m in [i-1,i,i+1]:
        for k in [j-1,j,j+1]:
          B.append(A[m][k])
     
      A[i]=A[i][1:r+1:].replace(".",str(B.count("#")),1)
      A[i]="."+A[i]+"."
for i in range(1,g+1):
  print(A[i][1:r+1:])

Submission status: Accepted

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


def main():
    """Function."""
    h, w = map(int, input().split())

    s = []
    s.append(["!" for _ in range(w+2)])
    for _ in range(h):
        dum = str(input())
        dum = list(dum)
        dum.insert(0, "!")
        dum.append("!")
        s.append(list(dum))
    s.append(["!" for _ in range(w+2)])

    for _i in range(h):
        for _j in range(w):
            i = _i + 1
            j = _j + 1
            if s[i][j] == "#":
                pass
            else:
                counter = 0
                if s[i-1][j-1] == "#":
                    counter += 1
                if s[i-1][j] == "#":
                    counter += 1
                if s[i-1][j+1] == "#":
                    counter += 1
                if s[i][j-1] == "#":
                    counter += 1
                if s[i][j+1] == "#":
                    counter += 1
                if s[i+1][j-1] == "#":
                    counter += 1
                if s[i+1][j] == "#":
                    counter += 1
                if s[i+1][j+1] == "#":
                    counter += 1
                s[i][j] = str(counter)

    for i in range(h):
        dum = s[i+1][1:-1]
        dum_2 = "".join(dum)
        print(dum_2)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    coord = []
    coord.append([0] * (w + 2))
    for _ in range(h):
        coord.append([0] + list(input()) + [0])
    coord.append([0] * (w + 2))

    dys = [-1, -1, 0, 1, 1, 1, 0, -1]
    dxs = [0, 1, 1, 1, 0, -1, -1, -1]

    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if coord[y][x] == '#':
                continue

            coord[y][x] = 0
            for dy, dx in zip(dys, dxs):
                if coord[y + dy][x + dx] == '#':
                    coord[y][x] += 1

    for row in coord[1:-1]:
        print(''.join(map(str, row[1:-1])))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def listToString(_list, split=""):
    maped_list = map(str, _list)  # mapで要素すべてを文字列に
    mojiretu = split.join(maped_list)
    return mojiretu


h, w = [int(i) for i in input().split()]
s = [[0 for _ in range(w + 2)] for _ in range(h + 2)]
table = [[0 for _ in range(w + 2)] for _ in range(h + 2)]

for i in range(h):
    _s = input()
    s[i + 1][1:1+w] = [_s[i] for i in range(len(_s))]

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == "#":
            table[i + 1][j - 1] += 1
            table[i + 1][j] += 1
            table[i + 1][j + 1] += 1
            table[i][j + 1] += 1
            table[i][j - 1] += 1
            table[i - 1][j - 1] += 1
            table[i - 1][j] += 1
            table[i - 1][j + 1] += 1
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == "#":
            table[i][j] = "#"
for i in range(1, h + 1):
    print(listToString(table[i][1:w+1]))

Submission status: Accepted

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

def add_bomb(area, i, j):
    if i+1 < h:
        area[i+1][j] += 1
        if j+1 < w:
            area[i+1][j+1] += 1
        if j-1 >= 0:
            area[i+1][j-1] += 1

    if i-1 >= 0:
        area[i-1][j] += 1
        if j+1 < w:
            area[i-1][j+1] += 1
        if j-1 >= 0:
            area[i-1][j-1] += 1

    if j+1 < w:
        area[i][j+1] += 1
    if j-1 >= 0:
        area[i][j-1] += 1

    return area

for i in range(h):
    for j, x in enumerate(list(input())):
        if x == '#':
            area = add_bomb(area, i, j)
            bomb[i][j] = 0

for a_ls, b_ls in zip(area, bomb):
    print(''.join([str(a) if b == 1 else '#' for a, b in zip(a_ls, b_ls)]))

Submission status: Accepted

Solution code:
# coding: utf-8
H, W = map(int, input().split())

# 一回り大きい並びになるように、"." をパディングする
lst = ["."*(W+2)] + ["." + input() + "." for _ in range(H)] + ["."*(W+2)]

# lst と同じ形で、要素が全部 0 のリスト
lst2 = []
for _ in range(H+2):
    lst2.append([0 for _ in range(W+2)])

# パディングの内側を3x3の窓で、「. の周囲に # があるか」探索する
for i in range(1, H+1):
    for j in range(1, W+1):
        cnt = 0
        if lst[i][j] == ".":
            cnt += lst[i-1][j-1:j+2].count("#")
            cnt += lst[i][j-1:j+2].count("#")
            cnt += lst[i+1][j-1:j+2].count("#")
            lst2[i][j] = str(cnt)
        else:
            lst2[i][j] = "#"

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