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:
def replace(s, r, c):
    for row in range(r - 1, r + 2):
        for column in range(c - 1, c + 2):
            if row < 0 or len(s) <= row:
                continue

            if column < 0 or len(s[row]) <= column:
                continue

            value = s[row][column]
            if value != "#":
                value += 1
                s[row][column] = value


h, w = map(int, input().split(" "))
s = []
for _ in range(h):
    row = input()
    tmp = []
    for i in range(w):
        tmp.append(row[i] if row[i] == "#" else 0)
    s.append(tmp)

for row in range(h):
    for column in range(w):
        if s[row][column] == "#":
            replace(s, row, column)

for row in range(h):
    res = ""
    for column in range(w):
        res += str(s[row][column])
    print(res)

Submission status: Accepted

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

la = ["a"] * (W+2)
lz = ["a"] * (W+2)

lall = [la]

for i in range(H):
    l = ["a"]
    s = list(input())
    l = l + s
    l.append("a")
    lall.append(l)

lall.append(lz)

for i in range(1, H+1):
    for j in range(1, W+1):
        if lall[i][j] == ".":
            cnt = lall[i-1][j-1:j+2].count("#")
            if lall[i][j-1] == "#":
                cnt += 1
            if lall[i][j+1] == "#":
                cnt += 1
            cnt += lall[i+1][j-1:j+2].count("#")
            lall[i][j] = cnt

for i in range(1, H+1):
    lans = list(map(str, lall[i]))
    ans = ''.join(lans[1:W+1])
    print(ans)

Submission status: Accepted

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

def count_bomb(h,w,S):
    if S[h][w]=="#":
        return "#"
    
    ret=0
    for i,j in ([1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]):
        next_h=h+i
        next_w=w+j
        if next_h<0 or next_h>=H:
            continue
        elif next_w<0 or next_w>=W:
            continue

        if S[next_h][next_w]=="#":
            ret+=1
    
    return str(ret)

ans=[""]*H
for i in range(H):
    for j in range(W):
        cnt=count_bomb(i,j,S)
        ans[i]+=cnt

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

Submission status: Accepted

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

mine = [list(input()) for _ in range(h)]


def update(masu):
    if masu == '.':
        return 1
    elif masu == '#':
        return masu
    else:
        return masu + 1


for i in range(h):
    for j in range(w):
        now = mine[i][j]
        ul = mine[max(i - 1, 0)][max(j - 1, 0)]
        u = mine[max(i - 1, 0)][j]
        ur = mine[max(i - 1, 0)][min(j + 1, w - 1)]
        dl = mine[min(i + 1, h - 1)][max(j - 1, 0)]
        d = mine[min(i + 1, h - 1)][j]
        dr = mine[min(i + 1, h - 1)][min(j + 1, w - 1)]
        r = mine[i][min(j + 1, w - 1)]
        l = mine[i][max(j - 1, 0)]

        if now == '#':
            mine[max(i - 1, 0)][max(j - 1, 0)] = update(ul)
            mine[max(i - 1, 0)][j] = update(u)
            mine[max(i - 1, 0)][min(j + 1, w - 1)] = update(ur)
            mine[min(i + 1, h - 1)][max(j - 1, 0)] = update(dl)
            mine[min(i + 1, h - 1)][j] = update(d)
            mine[min(i + 1, h - 1)][min(j + 1, w - 1)] = update(dr)
            mine[i][min(j + 1, w - 1)] = update(r)
            mine[i][max(j - 1, 0)] = update(l)

for ans in mine:
    print(''.join(list(map(str, ans))).replace('.', '0'))

Submission status: Accepted

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

result = []
for h in range(H):
    result.append([])
    for w in range(W):
        count = 0
        if l[h][w] == '#':
            result[h].append('#')
            continue

        # 対象の右
        if W - 1 != w and l[h][w + 1] == '#':
            count += 1

        # 対象の右下
        if W - 1 != w and H - 1 != h and l[h + 1][w + 1] == '#':
            count += 1

        # 対象の下
        if H - 1 != h and l[h + 1][w] == '#':
            count += 1

        # 対象の左下
        if 0 < w and H - 1 != h and l[h + 1][w - 1] == '#':
            count += 1

        # 対象の左
        if 0 < w and l[h][w - 1] == '#':
            count += 1

        # 対象の左上
        if 0 < w and 0 < h and l[h - 1][w - 1] == '#':
            count += 1

        # 対象の上
        if 0 < h and l[h - 1][w] == '#':
            count += 1

        # 対象の右上
        if W - 1 != w and 0 < h and l[h - 1][w + 1] == '#':
            count += 1

        result[h].append(str(count))
    print(''.join(result[h]))

Submission status: Accepted

Solution code:
def scanner(S,h,w,H,W):
    count=0
    if S[h][w]=='#':
        return('#')
    if h==0:
        h1=0
    else:
        h1=1
    if h==H-1:
        h2=0
    else:
        h2=1
    if w==0:
        w1=0
    else:
        w1=1
    if w==W-1:
        w2=0
    else:
        w2=1
    for height in range(h-h1,h+h2+1):
        for width in range(w-w1,w+w2+1):
            if S[height][width]=='#':
                count+=1
    return(count)

H,W=map(int,input().split())
S=[list(input())for i in range(H)]
for h in range(H):
    ans=''
    for w in range(W):
        ans+=str(scanner(S,h,w,H,W))
    print(ans)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
table = [[0]*(w+2) for i in range(h+2)]
for i in range(h):
    s_list = list(input())
    for k in range(w):
        if s_list[k] == ".":
            s_list[k] = 0
    for j in range(w):
        table[i+1][j+1] = s_list[j]

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

for i in range(1,h+1):
    dum = table[i][1:w+1]
    print("".join(map(str,dum)))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

import itertools
directions = list(itertools.product([-1,0,1], repeat=2))

new_masu = []
for h in range(H):
    tmp = []
    for w in range(W):
        if masu[h][w] == "#":
            tmp.append("#")
            continue

        bombs = 0
        for dx, dy in directions:
            x = w + dx
            y = h + dy
            if x < 0 or x >= W:
                continue
            if y < 0 or y >= H:
                continue
            if masu[y][x] == "#":
                bombs += 1
        tmp.append(bombs)
    new_masu.append(tmp)

for line in new_masu:
    # この書き方重要
    print(*line, sep="")

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

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

new_masu = []
for h in range(H):
    tmp = []
    for w in range(W):
        if masu[h][w] == "#":
            tmp.append("#")
            continue

        bombs = 0
        # is_debug = (h == 3 and w == 0)
        for direction in directions:
            x = w + direction[0]
            y = h + direction[1]
            if 0 <= x < W and 0 <= y < H:
                try:
                    if masu[y][x] == "#":
                        bombs += 1
                except Exception:
                    pass
        tmp.append(bombs)
    new_masu.append(tmp)

for line in new_masu:
    # この書き方重要
    print(*line, sep="")

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def count(S, i, j):
    res = 0
    for x, y in [(0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)]:
        if S[i + y][j + x] == "#":
            res += 1
    return res


def main():
    H, W = map(int, input().split())
    S = [None] * (H + 2)
    S[0] = S[-1] = "." * (W + 2)
    for i in range(1, H + 1):
        S[i] = "".join([".", input().rstrip(), "."])

    ans = [[None] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i + 1][j + 1] == "#":
                ans[i][j] = "#"
            else:
                ans[i][j] = count(S, i + 1, j + 1)

    for i in range(H):
        print("".join(map(str, 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)]

for i in range(H):
  for j in range(W):
    ans  = 0
    if S[i][j] == "#":
      continue
    else:
      if j != 0 and S[i][j-1] =="#":
        ans += 1
      if j != 0 and i != 0 and S[i-1][j-1] == "#":
          ans += 1
      if j != 0 and i != H-1 and S[i+1][j-1] == "#":
          ans += 1
      if j != W-1 and S[i][j+1] =="#":
        ans += 1
      if j != W-1 and i != 0 and S[i-1][j+1] == "#":
          ans += 1
      if j != W-1 and i != H-1 and S[i+1][j+1] == "#":
          ans += 1
      if i != 0 and S[i-1][j] == "#":
        ans += 1
      if i != H-1 and S[i+1][j] == "#":
        ans += 1
      S[i][j] = str(ans)
      ans = 0 
for i in range(H):
	print("".join(S[i]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
h, w  = map(int, input().split())
#  = list(map(int, input().split()))
masu = [list(input()) for i in range(h)]
#
# c = collections.Counter()


dumy = ['.'] * w
masu.append(dumy)
masu.insert(0, dumy)


for i in range(h+2):
    masu[i] = ['.'] + masu[i] + ['.']

nbomb = [[0] * (w + 2) for _ in range(h+2)]

for i, line in enumerate(masu[1:-1]):
    for j, m in enumerate(line[1:-1]):
        if m == '#':
            i_ = i + 1
            j_ = j + 1
            nbomb[i_-1][j_-1] += 1
            nbomb[i_-1][j_-0] += 1
            nbomb[i_-1][j_+1] += 1
            nbomb[i_-0][j_-1] += 1
            nbomb[i_-0][j_+1] += 1
            nbomb[i_+1][j_-1] += 1
            nbomb[i_+1][j_-0] += 1
            nbomb[i_+1][j_+1] += 1

for ml, bl in zip(masu[1:-1], nbomb[1:-1]):
    ml = ml[1:-1]
    bl = bl[1:-1]

    for m_, b_ in zip(ml, bl):
        if m_ == '.':
            print(b_, end='')
        else:
            print('#', end='')
    print()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

H, W = map(int, input().split())
S = [list('.'*(W+2))]

for i in range(H):
    S.append(list('.' + input() + '.'))

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

A = []


for i in range(1, H+1):
    AA = []
    for j in range(1, W+1):
        cnt = 0
        if S[i][j] == '#':
            AA.append('#')
            continue
        if S[i-1][j-1] == '#': cnt += 1
        if S[i-1][j] == '#': cnt += 1
        if S[i-1][j+1] == '#': cnt += 1
        if S[i][j-1] == '#': cnt += 1
        if S[i][j+1] == '#': cnt += 1
        if S[i+1][j-1] == '#': cnt += 1
        if S[i+1][j] == '#': cnt += 1
        if S[i+1][j+1] == '#': cnt += 1
        AA.append(cnt)
    A.append(AA)

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
B = []
B.append('.' * (W + 2))
for i in range(H):
    B.append('.' + S[i] + '.')
B.append('.' * (W + 2))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if B[i][j] == '#':
            print('#', end='')
            continue
        c = 0
        if B[i - 1][j - 1] == '#':
            c += 1
        if B[i - 1][j + 1] == '#':
            c += 1
        if B[i + 1][j - 1] == '#':
            c += 1
        if B[i + 1][j + 1] == '#':
            c += 1
        if B[i][j - 1] == '#':
            c += 1
        if B[i][j + 1] == '#':
            c += 1
        if B[i + 1][j] == '#':
            c += 1
        if B[i - 1][j] == '#':
            c += 1
        print(c, end='')
    print('')

Submission status: Accepted

Solution code:
import numpy as np

# 読み込み
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(map(str, input().rstrip())))

# 外枠を「.」で囲う準備
li = []
for i in range(H+2):
    li.append(["."]*(W+2))

# 外枠付きSの生成
for i in range(1, H+1):
    li[i][1:-1] = S[i-1]

# カウント用配列の準備
ans = []
for i in range(H+2):
    ans.append([0]*(W+2))
ans = np.array(ans)

# カウント処理
for h in range(1, H+1):
    for w in range(1, W+1):
        if li[h][w]=="#":
            ans[h-1:h+2, w-1:w+2] += 1

# カウント結果から必要な場所だけ切り出す
ans = ans.astype("str")
ans = ans[1:-1, 1:-1]

# 「#」で上書きしながら出力
for h in range(H):
    for w in range(W):
        if S[h][w]=="#":
            ans[h, w] = "#"
    print("".join(ans[h]))

Submission status: Accepted

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


str_list.append(["."]*w)
str_list.insert(0,["."]*w)
for i in range(h+2):
    str_list[i].append(".")
    str_list[i].insert(0,".")

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

baku = 0

for i in range(h):
    for j in range(w):
        if str_list[i+1][j+1] == "#":
            ans[i][j] = "#"
            continue
        
        baku = 0
        for a in range(3):
            for b in range(3):

                if a == 1 and b==1:
                    continue
                if str_list[i+a][j+b]=="#":
                    baku += 1
        
        ans[i][j] = baku
            

for i in range(h):
    a = ""
    a = str(a)
    for j in ans[i]:
        a += str(j)

    print(a)

Submission status: Accepted

Solution code:
H, W= [int(x) for x in input().split()]
s_list = []
for h in range(H):
    s_list.append(input())
s_list.insert(0, "."*W)
s_list.append("."*W)

ans = []

for h in range(1, H+1):
    for w in range(W):
        count = 0
        if s_list[h][w]=="#":
            count = "#"
        else:   
            if w!=W-1:
                if s_list[h-1][w+1]=="#":
                    count += 1
                if s_list[h][w+1]=="#":
                    count += 1
                if s_list[h+1][w+1]=="#":
                    count += 1
            if w!=0:
                if s_list[h-1][w-1]=="#":
                    count += 1
                if s_list[h][w-1]=="#":
                    count += 1
                if s_list[h+1][w-1]=="#":
                    count += 1
            # ここからは上下 
            if s_list[h+1][w]=="#":
                    count += 1
            if s_list[h-1][w]=="#":
                count += 1
        
        ans.append(count)

for n in range(H):
    for a in ans[n*W:n*W+W]:
        print(a, end='')
    print()

Submission status: Accepted

Solution code:
def main():
    for x in range(H):
        for y in range(W):
            if S[x][y] == ".":
                tmp = 0
                if x > 0 and S[x - 1][y] == "#":
                    tmp = tmp + 1
                if x < H - 1 and S[x + 1][y] == "#":
                    tmp = tmp + 1
                if y > 0 and S[x][y - 1] == "#":
                    tmp = tmp + 1
                if y < W - 1 and S[x][y + 1] == "#":
                    tmp = tmp + 1
                if x > 0 and y > 0 and S[x - 1][y - 1] == "#":
                    tmp = tmp + 1
                if x > 0 and y < W - 1 and S[x - 1][y + 1] == "#":
                    tmp = tmp + 1
                if x < H - 1 and y > 0 and S[x + 1][y - 1] == "#":
                    tmp = tmp + 1
                if x < H - 1 and y < W - 1 and S[x + 1][y + 1] == "#":
                    tmp = tmp + 1
                S[x][y] = str(tmp)

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


if __name__ == "__main__":
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    main()

Submission status: Accepted

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

for x in range(H):
    for y in range(W):
        if S[x][y] == ".":
            tmp = 0
            if x > 0 and S[x - 1][y] == "#":
                tmp = tmp + 1
            if x < H - 1 and S[x + 1][y] == "#":
                tmp = tmp + 1
            if y > 0 and S[x][y - 1] == "#":
                tmp = tmp + 1
            if y < W - 1 and S[x][y + 1] == "#":
                tmp = tmp + 1
            if x > 0 and y > 0 and S[x - 1][y - 1] == "#":
                tmp = tmp + 1
            if x > 0 and y < W - 1 and S[x - 1][y + 1] == "#":
                tmp = tmp + 1
            if x < H - 1 and y > 0 and S[x + 1][y - 1] == "#":
                tmp = tmp + 1
            if x < H - 1 and y < W - 1 and S[x + 1][y + 1] == "#":
                tmp = tmp + 1
            S[x][y] = str(tmp)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    H,W=map(int,input().split())
    S=[[] for _ in range(H+2)]
    S[0]=S[H+1]=['.']*(W+2)
    for i in range(1,H+1):
        S[i]=['.']+list(input())+['.']
    ans=[['']*W for _ in range(H)]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if S[i][j]=='#':
                ans[i-1][j-1]='#'
            else:
                c=0
                for dy in range(-1,2):
                    for dx in range(-1,2):
                        if dy==dx==0:
                            continue
                        c+=(S[i+dy][j+dx]=='#')
                ans[i-1][j-1]=str(c)
    for row in ans:
        print(''.join(row))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            l = max(0, j - 1)
            r = min(j + 2, w)
            if i == 0:
                if h == 1:
                    s[i][j] = s[i][l:r].count('#')
                else:
                    s[i][j] = s[i][l:r].count('#') + s[i + 1][l:r].count('#')
            elif i == h - 1:
                s[i][j] = s[i - 1][l:r].count('#') + s[i][l:r].count('#')
            else:
                s[i][j] = s[i - 1][l:r].count('#') + s[i][l:r].count('#') + s[i + 1][l:r].count('#')
    print(*s[i], sep='')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = [input() for _ in range(h)]
B = [['0']*w for _ in range(h)]

def check(i,j):
    cnt = 0
    for i2 in [-1,0,1]:
        for j2 in [-1,0,1]:
            if (i2!=0 or j2!=0) and 0<=i+i2<h and 0<=j+j2<w:
                if S[i+i2][j+j2]=='#':
                    cnt+=1
    return cnt

for i in range(h):
    for j in range(w):
        if S[i][j]=='.':
            B[i][j]=str(check(i,j))
        else:
            B[i][j]='#'
[print(''.join(x)) for x in B]

Submission status: Accepted

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

a = []  # Map

for i in range(H):
    a.append(input())

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

mx = [-1, -1, -1, 1, 1, 1, 0, 0]  # x direction
my = [-1, 0, 1, -1, 0, 1, -1, 1]  # y direction

for h in range(H):
    for w in range(W):
        cnt = 0  # bomb cnt
        if a[h][w] == '#':
            res[h][w] = '#'
        else:
            for i in range(len(mx)):  # move x
                    x = h + mx[i]
                    y = w + my[i]
                    if (0 <= x <= H-1) and (0 <= y <= W-1):
                        if a[x][y] == '#':
                            cnt += 1
            res[h][w] = cnt


for i in range(H):
    for j in range(W):
        print("{}".format(res[i][j]),end="")
    print()

Submission status: Accepted

Solution code:



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

# 外堀は作らない
# 代わりに、8方向の差分を計算する配列を用意




for i in range(h):
    array = list(input())   
        # マス目はスペースで区切られてないから、input split ではだめ
        # list関数を使わないといけない

    for j in range(w):
        s[i][j] = array[j]
        

dxdy = [[1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1]]


for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == '.':
            for k in range(8):
                dx = dxdy[k][0]
                dy = dxdy[k][1]
                xx = i + dx
                yy = j + dy

                if xx <0 :
                    continue
                if xx > h-1:
                    continue
                if yy <0 :
                    continue
                if yy > w-1:
                    continue

                if s[xx][yy] == '#':
                    count += 1

            s[i][j] = count

            

for i in range(h):
    text = ''
    for j in range(w):
        text += str(s[i][j])
    print(text)

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
Sq = []
Haround = []
Slast = []
for i in range(W+2) :
    Haround.append('0')

Sq.append(Haround)

for i in range(H) : 
    l = list(str(input()))
    S = ['0']
    for j in range(W) :
            S.append(l[j])
    S.append('0')
    Sq.append(S)
Sq.append(Haround)

around = [[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1]]
for i in range(H+2) : 
    for j in range(W+2) : 
        c = 0
        if Sq[i][j] == '.' :
            for k in range(8) :
                K = around[k]
                if Sq[i + K[0]][j + K[1]] == '#' :
                    c += 1
            Sq[i][j] = str(c)

for i in range(1,H+1) :
    Sq[i].pop(0)
    Sq[i].pop(W)
    ans = ''.join(Sq[i])
    print(ans)

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            continue

        count=0
        for k in range(8):
            di=i+dx[k]
            dj=j+dy[k]

            if (di<0)|(di>h-1):
                continue
            if (dj<0)|(dj>w-1):
                continue

            if s[di][dj]=="#":
                count+=1

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

for _ in range(h):
    print("".join(s[_]))

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if ans[i][j] !="#": #その升が爆弾じゃなかったら
            count = 0
            for p in range(max(0,i-1), min(i+2, h)):
                for q in range(max(0,j-1), min(j+2, w)):
                    if ans[p][q] == "#":
                        count += 1
            ans[i][j] = str(count)

for l in ans:
    for c in l:
        print(c, end="")
    print()

Submission status: Accepted

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

field = [[0 if x == '.' else 1 for x in xx] for xx in s]
result = [[''] * w for _ in range(h)]

for i in range(h):
    for j  in range(w):
        if field[i][j] == 1:
            result[i][j] = '#'
            continue
        
        total = 0
        for ii in range(i-1, i+2):
            for jj in range(j-1, j+2):
                if ii < 0 or ii >= h:
                    continue
                if jj < 0 or jj >= w:
                    continue
                total += field[ii][jj]
        result[i][j] = str(total)

for r_line in result:
    print("".join(r_line))

Submission status: Accepted

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

search = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]

for i in range(h):
    for j in range(w):
        if l[i][j] == '.':
            tmp = 0
            for k in range(len(search)):
                if 0 <= i + search[k][0] <= h-1 and 0 <= j + search[k][1] <= w-1:
                    if l[i + search[k][0]][j + search[k][1]] == '#':
                        tmp += 1
            l[i][j] = str(tmp)

for k in range(h):
    print(''.join(l[k]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s_list = [list(input()) for nesya in range(h)]
k = 0
for s in s_list:
  m = 0
  for a in s:
    if a == '#':
      pass
    else:
      c = []
      if m != 0:
        c.append(s_list[k][m-1])
        if k != 0:
          c.append(s_list[k-1][m-1])
        if k != h-1:
          c.append(s_list[k+1][m-1])
      if m != w-1:
        c.append(s_list[k][m+1])
        if k != 0:
          c.append(s_list[k-1][m+1])
        if k != h-1:
          c.append(s_list[k+1][m+1])
      if k != 0:
        c.append(s_list[k-1][m])
      if k != h-1:
        c.append(s_list[k+1][m])
      hoge = c.count('#')
      s_list[k][m] = str(hoge)
    m += 1
  k += 1
for ans in s_list:
  print(''.join(ans))

Submission status: Accepted

Solution code:
import collections
import math

H, W = map(int, input().split())
bomb_dict = collections.defaultdict(bool)
count_dict = collections.defaultdict(int)
for i in range(H):
    S = input()
    for j in range(len(S)):
        if S[j] == '#':
            bomb_dict[i, j] = True
            count_dict[i - 1, j - 1] += 1
            count_dict[i - 1, j] += 1
            count_dict[i - 1, j + 1] += 1

            count_dict[i, j - 1] += 1
            count_dict[i, j + 1] += 1

            count_dict[i + 1, j - 1] += 1
            count_dict[i + 1, j] += 1
            count_dict[i + 1, j + 1] += 1

for i in range(H):
    for j in range(W):
        if bomb_dict[i, j]:
            print('#', end='')
        else:
            print(count_dict[i, j], end='')

    print()

Submission status: Accepted

Solution code:
#下，右，上，左，右下，右上，左下，左上
dy = [1, 0, -1, 0, 1, -1, 1, -1]
dx = [0, 1, 0, -1, 1, 1, -1, -1]

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

for y in range(H):
    for x in range(W):

        if S[y][x] == "#":
            continue

        cnt = 0

        for k in range(8):
            ny = y + dy[k]
            nx = x + dx[k]

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

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

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

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [input() for _ in range(H)]
answer = [[0 if cell == '.' else '#' for cell in row] for row in S]
 
import itertools
for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            continue
        for dx,dy in itertools.product([-1,0,1],repeat=2):
            x = i+dx
            y = j+dy
            if x < 0 or x >= H:
                continue
            if y < 0 or y >= W:
                continue
            if S[x][y] == '#':
                continue
            answer[x][y] += 1
            
for row in answer:
    print(*row,sep='')

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[66]:


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


# In[67]:


import copy


# In[68]:


ans = copy.deepcopy(S)
# print(ans)
for h in range(len(S)):
#     print(h)
#     print(S[h])
    for w in range(len(S[h])):
#         print(h, w, S[h][w])
        if S[h][w] == ".":
            s = S[h-1][w-1:w+2] + S[h][w-1:w+2] + S[h+1][w-1:w+2]
#             print("\t", s, s.count("#"))
            ans[h][w] = str(s.count("#"))
# print(ans)
for h in range(H):
    row = "".join(ans[h+1])
    print(row[1:-1])


# In[ ]:

Submission status: Accepted

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

Submission status: Accepted

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

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

for h in range(H):
  for w in range(W):
    if S[h][w] == "#":
      continue
    cnt = 0
    for i in range(8):
      y = h + dy[i]
      x = w + dx[i]
      if 0 <= y < H and 0 <= x < W:
        if S[y][x] == "#":
          cnt += 1
    S[h][w] = str(cnt)
for s in S:
  print("".join(s))

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            for k in range(3):
                for l in range(3):
                    A[i + k][j + l] += 1

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            A[i + 1][j + 1] = '#'

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