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 = [int(i) for i in input().split()]
S = [list(input()) for _ in range(H)]

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

for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        count = 0
        for nh, nw in ((h + dh, w + dw) for dh, dw in move):
            if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == "#":
                count += 1
        S[h][w] = str(count)

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

Submission status: Accepted

Solution code:
from sys import stdin
H,W = [int(x) for x in stdin.readline().rstrip().split()]

data = [[""] * (W + 2) ]
for _ in range(H):
    data.append([""] + list(input()) + [""])
data.append([""] * (W + 2))

for i in range(1,H+1):
    for j in range(1,W+1):
        if data[i][j] == ".":
            mine = 0
            if data[i-1][j-1] == "#":
                mine += 1
            if data[i-1][j]   == "#":
                mine += 1
            if data[i-1][j+1] == "#":
                mine += 1
            if data[i][j-1]   == "#":
                mine += 1
            if data[i][j+1]   == "#":
                mine += 1
            if data[i+1][j-1] == "#":
                mine += 1
            if data[i+1][j]   == "#":
                mine += 1
            if data[i+1][j+1] == "#":
                mine += 1
            data[i][j] = str(mine)
        mine = 0

for k in data:
    print("".join(k))

Submission status: Accepted

Solution code:

def main(s_list):
    res_list = []
    for i_row, s in enumerate(s_list):
        res_char_list = []
        for j_col, char in enumerate(s):
            if char == '#':
                res_char_list.append('#')
            else:
                res_char_list.append(str(''.join(
                    [rows[max(j_col-1, 0): j_col+2] for rows in s_list[max(i_row-1, 0):i_row+2]]).count('#')))
        res_list.append(''.join(res_char_list))

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


if __name__ == "__main__":
    H, W = [int(x) for x in input().split()]
    s_list = []
    for i in range(H):
        s_list.append(input())

    main(s_list)

def test1(capsys):
    inputs = ['.....',
        '.#.#.',
        '.....']
    out = ['11211',
        '1#2#1',
        '11211']
    main(inputs)
    captured = capsys.readouterr()    
    assert captured.out == '\n'.join(out)+'\n'

def test2(capsys):
    inputs = ['#####',
                '#####',
                '#####']
    out = ['#####',
                '#####',
                '#####']
    main(inputs)
    captured = capsys.readouterr()    
    assert captured.out == '\n'.join(out)+'\n'

def test3(capsys):
    inputs = ['#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    out = ['#####3', '#8#7##', '####5#', '4#65#2', '#5##21', '#4#310']
    main(inputs)
    captured = capsys.readouterr()    
    assert captured.out == '\n'.join(out)+'\n'

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):
        if S[i][j] == '#':
            continue
        cnt = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if 0 <= i + dy < H and 0 <= j + dx < W:
                    if S[i+dy][j+dx] == '#':
                        cnt += 1
                else:
                    continue
        S[i][j] = str(cnt)

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

Submission status: Accepted

Solution code:
from itertools import product


H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
m = list(product([0, 1, -1], repeat=2))
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            S[i][j] = 0
            for ii, jj in m:
                iii = i + ii
                jjj = j + jj
                if 0 <= iii < H and 0 <= jjj < W:
                    if S[iii][jjj] == '#':
                        S[i][j] += 1
            S[i][j] = str(S[i][j])
for s in S:
    print(('').join(s))

Submission status: Accepted

Solution code:
from itertools import product


H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        m = product([0, 1, -1], repeat=2)
        if S[i][j] == '.':
            S[i][j] = 0
            for ii, jj in m:
                iii = i + ii
                jjj = j + jj
                if 0 <= iii < H and 0 <= jjj < W:
                    if S[iii][jjj] == '#':
                        S[i][j] += 1
            S[i][j] = str(S[i][j])
for s in S:
    print(('').join(s))

Submission status: Accepted

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

h, w = map(int, input().split())

s = [list(input()) for i in range(h)]

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

for i in range(h):
    for j in range(w):
        cnt = 0

        if 1 <= i and 1 <= j:
            if s[i-1][j-1] == '#':
                cnt += 1
        if 1 <= i:
            if s[i-1][j] == '#':
                cnt += 1
        if 1 <= i and j <= w-2:
            if s[i-1][j+1] == '#':
                cnt += 1
        if 1 <= j:
            if s[i][j-1] == '#':
                cnt += 1
        if j <= w-2:
            if s[i][j+1] == '#':
                cnt += 1
        if i <= h-2 and 1 <= j:
            if s[i+1][j-1] == '#':
                cnt += 1
        if i <= h-2:
            if s[i+1][j] == '#':
                cnt += 1
        if i <= h-2 and j <= w-2:
            if s[i+1][j+1] == '#':
                cnt += 1

        if s[i][j] == '#':
            s2[i][j] = '#'
        else:
            s2[i][j] = str(cnt)


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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
S = [input() for i 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())
S=[]
for _ in range(H):
    a=["."]
    a.extend(list(map(str,input())))
    a.append(".")
    S.append(a)

S.insert(0,["."]*(W+2))
S.insert(H+1,["."]*(W+2))

# print(S)

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

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

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
a = [input() for i in range(h)]
x_ = [-1, -1, -1, 0, 0, 1, 1, 1]
y_ = [-1, 0, 1, -1, 1, -1, 0, 1]
result = [['0' for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    count = 0
    if a[i][j] == '#':
      result[i][j] = '#'
      continue
    for k in range(8):
      x = i+x_[k]
      y = j+y_[k]
      if x<0 or y<0 or x>=h or y>=w:
        continue
      elif a[x][y] == '#':
        count += 1
    result[i][j] = str(count)
for i in result:
  print(''.join(i))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[list(input()) for i in range(h)]
for item in l:
  for i in range(w):
    if item[i]==".":
      item[i]=0

for i in range(h):
  for j in range(w):
    if l[i][j]=="#":
      if i>0:
        if l[i-1][j]!="#":
          l[i-1][j]+=1
        if j>0 and l[i-1][j-1]!="#":
          l[i-1][j-1]+=1
        if j<w-1 and l[i-1][j+1]!="#":
          l[i-1][j+1]+=1
      if i<h-1:
        if l[i+1][j]!="#":
          l[i+1][j]+=1
        if j>0 and l[i+1][j-1]!="#":
          l[i+1][j-1]+=1
        if j<w-1 and l[i+1][j+1]!="#":
          l[i+1][j+1]+=1
      if j>0 and l[i][j-1]!="#":
        l[i][j-1]+=1
      if j<w-1 and l[i][j+1]!="#":
        l[i][j+1]+=1
for item in l:
  print("".join(map(str,item)))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = eval('list(input()),' *h)
for i in range(h):
    for j in range(w):
        c = 0
        for a in range(i-1,i+2):
            for b in range(j-1,j+2):
                c += all((min(a,b)>=0, a<h, b<w)) and s[a][b] == '#'
                
        if s[i][j] == '.':
            s[i][j] = c
for c in s:
    print(*c,sep='')

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
S=[["." for _ in range(w+2)]]
S+=[["."]+[i for i in input()]+["."]for _ in range(h)]
S+=[["." for _ in range(w+2)]]
for i in range(1, h+1):
    l=""
    for j in range(1, w+1):
        if S[i][j]=="#":
            l+="#"
        else:
            l+=str(sum(S[i+n][j+m]=="#" for n in range(-1, 2, 1) for m in range(-1, 2, 1)))
    print(l)

Submission status: Accepted

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

S = []

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

for _ in range(H):
    S.append(list(input()))

for i in range(H):
    for j in range(W):
        if(S[i][j] == '#'): continue
        
        num = 0
        for dxy in dxy_list:
            ni = i + dxy[0]
            nj = j + dxy[1]

            if (ni < 0 or H <= ni): continue
            if (nj < 0 or W <= nj): continue
            
            if(S[ni][nj] == '#'): num += 1

        S[i][j] = str(num)

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=eval('list(input()),'*h)
r=range
for i in r(h):
  for j in r(w):
    c=0
    for a in r(i-1,i+2):
      for b in r(j-1,j+2):
        c+=all((min(a,b)>=0,a<h,b<w))and s[a][b]=='#'
    if s[i][j]=='.':s[i][j]=c
for a in s:print(*a,sep='')

Submission status: Accepted

Solution code:
h, w = [ int(v) for v in input().split() ]
field = [ list(input()) for i in range(h) ]
ansfield = [ [ "#" for j in range(w)] for i in range(h)]
for i in range(h):
	for j in range(w):
		if field[i][j] == ".":
			ansfield[i][j] =  str(sum([ [ field[i][j] for j in range(max(j-1,0),min(w,j+2)) ].count("#") for i in range(max(i-1,0),min(h,i+2)) ]))

for i in range(h):
	print("".join(ansfield[i]))

Submission status: Accepted

Solution code:
h,w=map(int, input().split())
S=["."*(w+2)]
S+=["."+input()+"." for _ in range(h)]
S+=["."*(w+2)]
# print(S)
for i in range(1, h+1):
    l=""
    for j in range(1, w+1):
        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)]))
            l+=str(sum([S[i+n][j+m]=="#" for n in range(-1, 2) for m in range(-1, 2)]))
    print(l)

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
S = [list(input()) for _ in range(H)]
answer = []
for i in range(H):
    row = []
    for j in range(W):
        if S[i][j] == "#":
            row.append("#")
            continue
        count = 0
        if i-1 != -1:
            for k in range(3):
                if j+k-1 != -1 and j+k-1 != W:
                    count += 1 if S[i-1][j+k-1] == "#" else 0
        for k in range(0,3,2):
            if j+k-1 != -1 and j+k-1 != W:
                count += 1 if S[i][j+k-1] == "#" else 0
        if i+1 != H:
            for k in range(3):
                if j+k-1 != -1 and j+k-1 != W:
                    count += 1 if S[i+1][j+k-1] == "#" else 0
        row.append(count)
    answer.append(row)
for i in answer:
    print(*i, sep="")

Submission status: Accepted

Solution code:
global h, w, dat
h, w = map(int, input().split())
dat = []
for i in range(h):
    dat.append(input())
for y in range(h):
    for x in range(w):
        c = 0
        if dat[y][x] == "#":
            print("#", end="")
            continue

        if 0 < y:
            if dat[y - 1][x] == "#":  # 2
                c += 1
            if 0 < x:
                if dat[y - 1][x - 1] == "#":  # 1
                    c += 1
            if w - 1 > x:
                if dat[y - 1][x + 1] == "#":  # 3
                    c += 1
        if 0 < x:
            if dat[y][x - 1] == "#":  # 4
                c += 1
            if y < h - 1:
                if dat[y + 1][x - 1] == "#":  # 7
                    c += 1

        if x < w - 1:
            if dat[y][x + 1] == "#":  # 6
                c += 1
            if y < h - 1:
                if dat[y + 1][x + 1] == "#":  # 9
                    c += 1

        if y < h - 1:
            if dat[y + 1][x] == "#":  # 8
                c += 1
        print(c, end="")
    print("")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
#H×Wのマス目入力の周りに"."を付け加える関数
def pixel_input(H,W,border="."):
    pixel = [[border]*(W+2)] + \
    [[border] + list(input()) + [border] for _ in range(H)] + \
    [[border]*(W+2)]
    return pixel
#マス目型2次元配列を出力する関数
def pixel_print(pixel):
    for i in range(len(pixel)):
        print("".join(pixel[i]))
S=pixel_input(H,W)
T=[["?" for _ in range(W)] for _ in range(H)]

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=="#":
            T[i-1][j-1]="#"
        else:
            T[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("#"))
pixel_print(T)

Submission status: Accepted

Solution code:
import copy
H, W = map(int, input().split())
s = []
for _ in range(H):
    s.append(list(input()))
ans = copy.deepcopy(s)
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            continue
        c = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if 0 <= dy + i < H and 0 <= dx + j < W:
                    if s[i+dy][j+dx] == '#':
                        c += 1
                else:
                    continue
        ans[i][j] = str(c)
for a in ans:
    print("".join(a))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = [list(input()) for i in range(h)]
dy = [1,1,1,0,-1,-1,-1,0]
dx = [1,0,-1,-1,-1,0,1,1]
for y in range(h):
    A = []
    for x in range(w):
        if S[y][x] == '#':
            A.append('#')
        else:
            cnt = 0
            for i in range(8):
                ny = y+dy[i]
                nx = x+dx[i]
                if 0<=ny<h and 0<=nx<w and S[ny][nx]=='#':
                    cnt += 1
            A.append(str(cnt))
    print("".join(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding: utf-8

H, W = map(int, input().split(' '))

H += 2
W += 2

S = ['0'*W] * H

for i in range(1, H-1):
    S[i] = str(input())

for n in range(1, H-1):
    S[n] = '0' + S[n] + '0'

for n in range(1, H-1):
    for m in range(1, W-1):
        if(S[n][m] == '.'):
            counter = 0
            for s in range(n-1, n+2):
                for t in range(m-1, m+2):
                    if(S[s][t] == '#'):
                        counter += 1
            print(counter,end='')
        if(S[n][m] == '#'):
            print('#',end='')
    print()

Submission status: Accepted

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

for y in range(1, H+1):
    for x in range(1, W+1):
        if S[y][x] == '.':
            S[y][x] = str(len([1 for i in range(y-1, y+2) for j in range(x-1, x+2) if S[i][j] == '#']))

S = [s[1:-1] for s in S[1:-1]]

[print(''.join(s)) for s in S]

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
dx = [1,1,1,0,0,-1,-1,-1]
dy = [1,0,-1,1,-1,1,0,-1]
for j in range(H):
    for i in range(W):
        cnt = 0
        for k in range(8):
            if 0<=i+dx[k]<W and 0<=j+dy[k]<H:
                if S[j+dy[k]][i+dx[k]]=="#":
                    cnt += 1
        if S[j][i] == "#":
            continue
        else:
            S[j][i] = cnt


for i in range(H):
    for j in range(W):
        S[i][j] = str(S[i][j])

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

Submission status: Accepted

Solution code:
def putnum(arr, i, j):
    i_l = i-1 if i-1 >= 0 else 50
    j_l = j-1 if j-1 >= 0 else 50
    try: arr[i_l][j_l] += 1
    except: pass
    try: arr[i_l][j] += 1
    except: pass
    try: arr[i_l][j+1] += 1
    except: pass
    try: arr[i][j_l] += 1
    except: pass
    try: arr[i][j+1] += 1
    except: pass
    try: arr[i+1][j_l] += 1
    except: pass
    try: arr[i+1][j] += 1
    except: pass
    try: arr[i+1][j+1] += 1
    except: pass

h, w = map(int, input().split())
fie = [[0]*w for _ in range(h)]
for i in range(h):
    for j, c in enumerate(list(input())):
        if c=="#": fie[i][j]=9

for i in range(h):
    for j in range(w):
        if fie[i][j] >= 9: putnum(fie, i, j)

for i in fie:
    i = list(map(str, map(lambda x:min(9,x), i)))
    print("".join(i).replace("9", "#"))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))
ans = []
for i in range(H):
    ans_s = []
    for j in range(W):
        if S[i][j] == "#":
            ans_s.append("#")
        else:
            bom = 0
            for x in range(-1, 2):
                for y in range(-1, 2):
                    if 0 <= i - x and i - x <= H - 1 and 0 <= j - y and j - y <= W - 1:
                        if not (x == 0 and y == 0):
                            if S[i - x][j - y] == "#":
                                bom += 1
            ans_s.append(str(bom))
    ans.append(ans_s)

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

Submission status: Accepted

Solution code:
import itertools
import math
H,W = list(map(int,input().split(" ")))
mines = []
dirs = [0,1,-1]

for _ in range(H):
    l =input()
    mines.append(l)


for h in range(H):
    for w in range(W):

        if mines[h][w] == "#":
            print("#",end="")

        else:
            c = 0
            for d1 in dirs:
                for d2 in dirs:
                    x = w - d1
                    y = h - d2
                    if x >=0 and x < W and y >= 0 and y < H:
                        if mines[y][x] == "#":
                            c += 1
            print(c,end="")
    print()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
s = [input() for _ in range(H)]
def ch(ii,jj):
    if s[ii][jj] == '#':
        return 1
    else:
        return 0

news = ['' for _ in range(H)]
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            b = 0
            if i-1>=0:
                if j-1>=0:
                    b += ch(i-1,j-1)
                b += ch(i-1,j)
                if j+1 <= W-1:
                    b += ch(i-1,j+1) 
            if j-1>=0:
                b += ch(i,j-1)
            if j+1 <= W-1:
                b += ch(i,j+1)
            if i+1<=H-1:
                if j-1>=0:
                     b += ch(i+1,j-1)
                b += ch(i+1,j)
                if j+1 <= W-1:
                    b += ch(i+1,j+1)
            news[i] = news[i] + str(b)
        else:
            news[i] = news[i] + '#'
for l in news:
    print(l)

Submission status: Accepted

Solution code:
import numpy as np

def in_map(x,y,map_str):
    if 0<=x and x <map_str.shape[1] and 0<=y and y<map_str.shape[0]:
        result=True
    else:
        result=False
    return result

def bom_count(bom,map_str,bom_map):
    # print(bom)
    for h in range(-1,2):
        for w in range(-1,2):
            # print(h,w)
            y = bom[0]+h
            x = bom[1]+w
            if h==0 and w==0:
                bom_map[y,x]=9
            if not in_map(x,y,map_str):
                continue
            if not map_str[y,x]=="#":
                bom_map[y,x]+=1

    # print(bom_map)
    return bom_map

if __name__ =="__main__":
    H,W=input_list=[int(i) for i in input().split()]
    map_str=np.empty((H,W),dtype=np.str_)
    for h in range(H):
        map_str[h] = np.array([i for i in input()])
    boms=np.where(map_str=="#")
    # print(boms)
    bom_map=np.zeros((H,W),dtype=np.int16)
    for i in range(len(boms[0])):
        bom=[boms[0][i],boms[1][i]]
        # print(bom)
        bom_map=bom_count(bom,map_str,bom_map)
    for h in range(H):
        print_str=""
        for w in range(W):
            if bom_map[h,w]==9:
                print_str+="#"
            else:
                print_str+=str(bom_map[h,w])
        print(print_str)

Submission status: Accepted

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

s = [["." for _ in range(w+2)]] + [["."]+list(input())+["."] for _ in range(h)] + [["." for _ in range(w+2)]]
for r in range(1, h+1):
  for c in range(1, w+1):
    if s[r][c] == ".":
      s[r][c] = str([s[r+dr][c+dc] for dr in [-1, 0, 1] for dc in [-1, 0, 1]].count("#"))
for r in range(1, h+1):
  print("".join(s[r][1:w+1]))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,h+1):
    tmp=[]
    for j in range(1,w+1):
        cnt=0
        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
        
        if S[i][j]=='#':
            tmp.append('#')
        else: tmp.append(str(cnt))
    Key.append(tmp)


for i in range(h):
    tmp=''.join(Key[i])
    print(tmp)

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
d = [list(input()) for i in range(r)]
bom = [(x,y) for (x,i) in enumerate(d) for (y,j) in enumerate(i) if j == '#']
temp = []
for b in bom:
    temp += [(b[0]+x, b[1]+y) for y in [1,0,-1] for x in [1,0,-1]]
print('\n'.join([''.join([str(temp.count((i,j))) if d[i][j] != '#' else '#' for j in range(c)]) for i in range(r)]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(list(input()))
if h == 1 and w == 1:
    if S[0][0] == '.':
        print('0')
    else:
        print(S[0][0])
if h == 1 and w == 2:
    if S == [['.','#']]:
        print('1#')
    if S == [['#','#']]:
        print('##')
    if S == [['#','.']]:
        print('#1')
    if S == [['.','.']]:
        print('00')
if h == 1 and w >= 3:
    for g in range(w):
        if S[0][0] == '.':
            if S[0][1] == '#':
                S[0][0] = '1'
            else:
                S[0][0] = '0'
        if 1 <= g <= w-2:
            if S[0][g] == '.':
                S[0][g] = str(S[0][g-1:g+2].count('#'))
        if S[0][w-1] == '.':
            if S[0][w-2] == '#':
                S[0][w-1] = '1'
            else:
                S[0][w-1] = '0'
    print(''.join(S[0]))
if h >= 3 and w == 1:
    for y in range(h):
        if S[0][0] == '.':
            if S[1][0] == '#':
                S[0][0] = '1'
            else:
                S[0][0] = '0' 
        if 1 <= y <= h-2:
            if S[y][0] == '.':
                count = 0
                if S[y-1][0] == '#':
                    count += 1
                if S[y+1][0] == '#':
                    count += 1
                S[y][0] = int(count)
        if S[h-1][0] == '.':
            if S[h-2][0] == '#':
                S[h-1][0] = '1'
            else:
                S[h-1][0] = '0'
    for p in S:
        print(p[0])
if h == 2 and w == 1:
    if S == [['.'],['.']]:
        print('0')
        print('0')
    if S == [['#'],['.']]:
        print('#')
        print('1')
    if S == [['.'],['#']]:
        print('1')
        print('#')
    if S == [['#'],['#']]:
        print('#')
        print('#')
if h >= 2 and w >= 2:
    for k in range(h):
        for j in range(w):
            if S[k][j] == '.':
                if k == 0 and j == 0:
                    count = 0
                    if S[0][1] == '#':
                        count += 1 
                    count += S[1][0:2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and 1 <= j <= w-2:
                    count = 0
                    count += S[0][j-1:j+2].count('#')
                    count += S[1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and j == w-1:
                    count = 0
                    if S[0][j-1] == '#':
                        count += 1
                    count += S[1][j-1:].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    if S[k][1] == '#':
                        count += 1 
                    count += S[k+1][0:2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    count += S[k+1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    count += S[k+1][j-1:].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    count += S[k][1].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    S[k][j] = str(count)
    for p in S:
        print(''.join(p))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
  S.append(list(i for i in input()))
# 参考https://atcoder.jp/contests/abc075/submissions/5409794
for i in range(H):
  for j in range(W):
    if S[i][j] == ".":
      cnt = 0
      if i != 0:
        cnt += S[i-1][max(0, j-1) : min(j+1, W-1) + 1].count("#")
      cnt += S[i][max(0, j-1) : min(j+1, W-1) + 1].count("#")
      if i != H-1:
        cnt += S[i+1][max(0, j-1) : min(j+1, W-1) + 1].count("#")
      S[i][j] = str(cnt)
for i in range(H):
	print("".join(S[i]))

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if s_list[i][j] == "#":
            ans_list[i][j] = "#"
        else:
            if (i!=0) and (j!=0):
                if (s_list[i-1][j-1] == "#"):
                    ans_list[i][j] += 1
            if (i!=0):
                if (s_list[i-1][j] == "#"):
                    ans_list[i][j] += 1
            if (i!=0) and (j!=w-1):
                if (s_list[i-1][j+1] == "#"):
                    ans_list[i][j] += 1
            if (j!=0):
                if (s_list[i][j-1] == "#"):
                    ans_list[i][j] += 1
            if (j!=w-1):
                if (s_list[i][j+1] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=0):
                if (s_list[i+1][j-1] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1):
                if (s_list[i+1][j] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=w-1):
                if (s_list[i+1][j+1] == "#"):
                    ans_list[i][j] += 1

for i in ans_list:
    print(''.join(map(str, i)))