Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.  

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).  

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.  

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.  

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310



Sample input:
3 5
.....
.#.#.
.....


Sample output:
11211
1#2#1
11211

Submission status: Accepted

Solution code:



h, w = map(int,input().split())
s = [[0]*(w+2) for _ in range(h+2)]
## 外堀を作っておく


for i in range(1,h+1):   ## コロンが抜けてたｗ
    array = list(input())   
        # マス目はスペースで区切られてないから、input split ではだめ
        # list関数を使わないといけない
        #  

    for j in range(1,w+1):
        s[i][j] = array[j-1]    



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

            s[i][j] = count

            

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
grid = [['.']*(w+2)] + [['.'] + [s for s in input()] + ['.'] for i in range(h)] + [['.']*(w+2)]
#print(grid)
ans = [[0]*w for i in range(h)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if grid[i][j] == '.':
            for dx in range(-1,2):
                for dy in range(-1,2):
                    if grid[i+dx][j+dy] == '#':
                        ans[i-1][j-1] += 1
        else:
            ans[i-1][j-1] = '#'

for i in range(h):
    for j in range(w):
        ans[i][j] = str(ans[i][j])

for i in range(h):
    ans[i] = ''.join(ans[i])

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

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] == '.':
            num = 0
            if i != 0 and s[i-1][j] == '#':
                num += 1
            if j != 0 and s[i][j-1] == '#':
                num += 1
            if i != h-1 and s[i+1][j] == '#':
                num += 1
            if j != w-1 and s[i][j+1] == '#':
                num += 1
            if i != 0 and j != 0 and s[i-1][j-1] == '#':
                num += 1
            if i != h-1 and j != w-1 and s[i+1][j+1] == '#':
                num += 1
            if i != h-1 and j != 0 and s[i+1][j-1] == '#':
                num += 1
            if i != 0 and j != w-1 and s[i-1][j+1] == '#':
                num += 1

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# 外周に'.'の壁を作れば境界の場合分け不要になる。

# input
h, w = map(int, input().split())
s_input = [input() for _ in range(h)]
s = [['.' for _ in range(w+2)] for _ in range(h+2)]
for i in range(1, h+1):
    for j in range(1, w+1):
        s[i][j] = s_input[i-1][j-1]

def sweep(i, j, s): 
    global h; global w
    ans = 0 
    # inner 
    if 0 < i < h+1:
        if 0 < j < w+1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    return ans 

# calc
ans = [['' 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] == '#':
            ans[i-1][j-1] = '#' 
        if s[i][j] == '.':
            ans[i-1][j-1] = str(sweep(i, j, s)) 
# output
for i in range(h):
    print(''.join(ans[i]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# input
h, w = map(int, input().split())
s = [input() for _ in range(h)]

def sweep(i, j, s):
    global h; global w
    ans = 0
    # inner 
    if 0 < i < h-1:
        if 0 < j < w-1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    # boundary
    if w > 1:
        if i == 0:
            if j == 0:
                sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            else:
                sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j], s[i+1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
        elif i == h-1:
            if j == 0:
                sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            else:
                sweeps = [s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
        else:
            if j == 0:
                sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1], s[i+1][j+1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1], s[i+1][j-1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1

    # w == 1
    else:
        if i == 0:
            sweeps = [s[i+1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif i == h-1:
            sweeps = [s[i-1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        else:
            sweeps = [s[i-1][0], s[i+1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1

    return ans

def sweep1line(j, s):
    global w
    ans = 0
    if w > 1:
        if j == 0:
            if s[0][j+1] == '#':
                ans += 1
        elif j == w-1:
            if s[0][j-1] == '#':
                ans += 1
        else:
            sweeps = [s[0][j-1], s[0][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    return ans

# calc
ans = [['' for _ in range(w)] for _ in range(h)]

if h != 1:
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans[i][j] = '#'
            if s[i][j] == '.':
                ans[i][j] = str(sweep(i, j, s))
else:
    for j in range(w):
        if s[0][j] == '#':
            ans[0][j] = '#'
        if s[0][j] == '.':
            ans[0][j] = str(sweep1line(j, s))

# output
for i in range(h):
    print(''.join(ans[i]))

Submission status: Accepted

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

for i in range(0, H):
    s.append(input())

for y in range(0, H):
    for x in range(0, W):
        c=0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                xx = x + dx
                yy = y + dy
                if 0<=xx and xx<W and 0<=yy and yy<H:
                    if s[yy][xx]=="#":
                        c+=1

        if s[y][x]=="#":
            print("#", end='')
        else:
            print(c, end='')
    print("")

Submission status: Accepted

Solution code:
import sys
#from collections import defaultdict, deque, Counter
#from bisect import bisect_left
#import heapq
#import math
#from itertools import groupby as gb
#from itertools import permutations as perm
#from itertools import combinations as comb
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

#n = int(stdin.readline().rstrip())
#l = list(map(int, stdin.readline().rstrip().split()))
h,w = map(int, stdin.readline().rstrip().split())
s = [list(map(str, stdin.readline().rstrip().split())) for _ in range(h)]

dx = [-1,0,1,-1,1,-1,0,1]
dy = [-1,-1,-1,0,0,1,1,1]
ans = []
for i in range(h):
    an = []
    for j in range(w):
        if s[i][0][j] == ".":
            count = 0
            for d in range(8):
                if h > i + dy[d] >= 0 and w > j + dx[d] >= 0:
                    if s[i + dy[d]][0][j + dx[d]] == "#":
                        count += 1
            a = str(count)
        else:
            a = "#"
        an += a
    print("".join(an))

Submission status: Accepted

Solution code:
import sys
#from collections import defaultdict, deque, Counter
#from bisect import bisect_left
#import heapq
#import math
#from itertools import groupby as gb
#from itertools import permutations as perm
#from itertools import combinations as comb
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

#n = int(stdin.readline().rstrip())
#l = list(map(int, stdin.readline().rstrip().split()))
h,w = map(int, stdin.readline().rstrip().split())
s = [list(map(str, stdin.readline().rstrip().split())) for _ in range(h)]

dx = [-1,0,1,-1,1,-1,0,1]
dy = [-1,-1,-1,0,0,1,1,1]
ans = []
for i in range(h):
    an = []
    for j in range(w):
        if s[i][0][j] == ".":
            count = 0
            for d in range(8):
                if h > i + dy[d] >= 0 and w > j + dx[d] >= 0:
                    if s[i + dy[d]][0][j + dx[d]] == "#":
                        count += 1
                    else:
                        pass
                else:
                    pass
            a = str(count)
        else:
            a = "#"
        an += a
    ans.append(an)

for i in range(h):
    p = "".join(ans[i])
    print(str(p))

Submission status: Accepted

Solution code:
def count_bomb(y,x):
    ans=0
    for i in range(y-1,y+2):
        for j in range(x-1,x+2):
            if i>=0 and i<h and j>=0 and j<w:
                if s[i][j]=="#":
                    ans+=1
    return ans
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]==".":
            s[i][j]=count_bomb(i,j)
for i in range(h):
    print(*s[i],sep="")

Submission status: Accepted

Solution code:
def main():
	H,W = list(map(int, input().split()))
	grid = []

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

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

	for i in range(H):
		for j in range(W):
			if(grid[i][j] == "#"):
				continue
			
			count = 0
			for k in shift:
				ii = i + k[0]
				jj = j + k[1]

				if(-1 < ii < H and -1 < jj < W and grid[ii][jj] == "#"):
					count += 1
			
			grid[i][j] = count

	for row in grid:
		print("".join(map(str, row)))


if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from collections import deque
H,W=map(int,input().split())
b=deque()
ans=[]
for i in range(H):
    s=input()
    t=[]
    for j in range(W):
        if s[j]=='#':
            b.append((i,j))
            t.append('#')
        else:
            t.append(0)
    ans.append(t)
while b:
    y,x=b.popleft()
    for i in range(-1,2):
        for j in range(-1,2):
            if i==0 and j==0:
                continue
            if 0<=y+i<H and 0<=x+j<W and ans[y+i][x+j]!='#':
                ans[y+i][x+j]+=1
for s in ans:
    print(*s,sep='')

Submission status: Accepted

Solution code:
import numpy as np


def main():
    h, w = map(int, input().split())
    ss = np.array([list(input()) for _ in range(h)])
    ans = np.zeros((h, w))
    xs, ys = np.where(ss == '#')
    for x, y in zip(xs, ys):
        coord = np.array([[x, y]])
        surround = np.array([[-1, -1], [-1, 0], [-1, +1], [0, -1], [0, +1], [+1, -1], [+1, 0], [+1, +1]])
        mines = np.tile(coord, (8, 1)) - surround
        for mx, my in mines:
            if mx < 0 or my < 0 or mx >= h or my >= w:
                continue
            ans[mx, my] += 1
    ans = ans.astype(np.int16).tolist()
    for x, y in zip(xs, ys):
        ans[x][y] = '#'

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def main():
    H, W = map(int, readline().rstrip().split())
    mat = [list(readline().rstrip()) for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if mat[h][w] == '.':
                cnt = 0
                for dh in [-1, 0, 1]:
                    for dw in [-1, 0, 1]:
                        if 0 <= h+dh < H and 0 <= w+dw < W:
                            if mat[h+dh][w+dw] == '#':
                                cnt += 1
                mat[h][w] = cnt
    
    for l in mat:
        print(*l, sep='')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def count_bomb(y,x):
    ans=0
    for i in range(y-1,y+2):
        for j in range(x-1,x+2):
            if i>=0 and i<h and j>=0 and j<w:
                if s[i][j]=="#":
                    ans+=1
    return ans
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]==".":
            s[i][j]=count_bomb(i,j)
for i in range(h):
    print(*s[i],sep="")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
h, w = map(int, input().split())
sumgrid = [[0] * w for i in range(h)]   # 個数を集計していく表

for i in range(h):
    # 1行読み込んで1か0の配列に変換
    line = list(map(int, input().replace('#', '1 ').replace('.', '0 ').split()))

    for j in range(w):
        # 現在の位置を含む3マス幅分の爆弾の数
        bom_cnt = line[j-1] if 0 < j else 0
        bom_cnt += line[j]
        bom_cnt += line[j+1] if j < (w - 1) else 0

        # カレント行に足し込む
        if line[j] == 0:
            sumgrid[i][j] += bom_cnt
        else:
            sumgrid[i][j] = '#' # 爆弾マス

        # 上の行の集計に足し込む
        if 0 < i and sumgrid[i-1][j] != '#':
            sumgrid[i-1][j] += bom_cnt

        # 下の行の集計に足し込む
        if i + 1 < h:
            sumgrid[i+1][j] += bom_cnt

for i in range(h):
    print(''.join([str(n) for n in sumgrid[i]]))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
grid = []
ddxy = [[0,1],[1,0],[0,-1],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]]
ans=[[0]*w for _ in range(h)]

def ok(x, y):
    if x>-1 and y>-1 and x<w and y<h and ans[y][x]!=-1:
        return 1
    else:
        return 0
for i in range(h):
    s = list(input().strip())
    grid.append(s)
for i in range(h):
    for j in range(w):
        if grid[i][j]=="#":
            ans[i][j]=-1
            for dxdy in ddxy:
                if ok(j+dxdy[1],i+dxdy[0]):
                    ans[i+dxdy[0]][j+dxdy[1]]+=1
for i in range(h):
    for j in range(w):
        if(ans[i][j])==-1:
            print("#", end="")
        else:
            print(ans[i][j], end="")
    print()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

def check(i, j):
    global grid
    if grid[i][j] == '#':
        return '#'
    cnt = 0
    for k in range(-1, 2):
        for l in range(-1, 2):
            cnt += grid[i+k][j+l] == '#'
    return str(cnt)

def main():
    global grid
    H, W = map(int, readline().split())
    grid = []
    grid.append(['*']*(W+2))
    for _ in range(H):
        grid.append(['*'] + list(readline()[:-1]) + ['*'])
    grid.append(['*']*(W+2))

    for i in range(1, H+1):
        for j in range(1, W+1):
            grid[i][j] = check(i, j)
    
    for i in range(1, H+1):
        print(''.join(grid[i][1:W+1]))
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:


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

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


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 h <= ny or nx < 0 or w <= nx:
                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 = [str(input()) for _ in range(h)]
data = [[0] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            for x in range(-1, 2):
                for y in range(-1, 2):
                    if 0 <= i + x < h and 0 <= j + y < w:
                        if s[i+x][j+y] == '#':
                            data[i][j] += 1
        else:
            data[i][j] = '#'

for i in range(h):
    for j in range(w):
        if data[i][j] != '#':
            data[i][j] = str(data[i][j])

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

Submission status: Accepted

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

direc = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            for x in direc:
                for y in direc:
                    if 0 <= i + x < h and 0 <= j + y < w:
                        if s[i+x][j+y] == '#':
                            data[i][j] += 1
        else:
            data[i][j] = '#'

for i in range(h):
    for j in range(w):
        if data[i][j] != '#':
            data[i][j] = str(data[i][j])

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

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    H,W = list(map(int,_in[0].split(' ')))  # type:list(int)
    S_arr = [[None]*(W+2) for i in range(H+2)]
    for i in range(H):
        _ = list(_in[i+1])   # type:str
        S_arr[i+1][1:-1] = _
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    num_arr = [[None]*W for i in range(H)]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if S_arr[i][j] == '#':
                num_arr[i-1][j-1] = '#'
            else:
                cnt = 0
                cnt += 1 if S_arr[i-1][j-1]=='#' else 0
                cnt += 1 if S_arr[i-1][j]=='#' else 0
                cnt += 1 if S_arr[i-1][j+1]=='#' else 0
                cnt += 1 if S_arr[i][j-1]=='#' else 0
                cnt += 1 if S_arr[i][j+1]=='#' else 0
                cnt += 1 if S_arr[i+1][j-1]=='#' else 0
                cnt += 1 if S_arr[i+1][j]=='#' else 0
                cnt += 1 if S_arr[i+1][j+1]=='#' else 0
                num_arr[i-1][j-1] = str(cnt)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    for num in num_arr:
        print(''.join(num))

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    if ar[i][j] == '#':
      continue
    
    bomb = 0
    if i > 0:
      if j > 0 and ar[i-1][j-1] == '#':
        bomb += 1
      if ar[i-1][j] == '#':
        bomb += 1
      if j+1 < w and ar[i-1][j+1] == '#':
        bomb += 1
    if j > 0 and ar[i][j-1] == '#':
      bomb += 1
    if j+1 < w and ar[i][j+1] == '#':
      bomb += 1
    if i+1 < h:
      if j > 0 and ar[i+1][j-1] == '#':
        bomb += 1
      if ar[i+1][j] == '#':
        bomb += 1
      if j+1 < w and ar[i+1][j+1] == '#':
        bomb += 1
    ar[i][j] = str(bomb)

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

Submission status: Accepted

Solution code:
# region header
import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**7)
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]
INF = float('inf')
MOD = 10 ** 9 + 7
PI = math.pi
# endregion
H, W = ri_()
S = [list(rs()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j] == '#':
            continue
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if not 0 <= i + dy < H or not 0 <= j + dx < W:
                    continue
                if S[i + dy][j + dx] == '#':
                    cnt += 1
        S[i][j] = str(cnt)
for s in S:
    print(''.join(s))

Submission status: Accepted

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

def checker(s_input,point):
    tmp=0
    x,y = map(int,point)
    def next_check(y,x):
        if 0<=y<=(h-1) and 0<=x<=(w-1):
            if s_input[y][x]=='#':
                return 1
        return 0
    tmp+=next_check(y-1,x-1)
    tmp+=next_check(y-1,x)
    tmp+=next_check(y-1,x+1)
    tmp+=next_check(y,x-1)
    tmp+=next_check(y,x+1)
    tmp+=next_check(y+1,x-1)
    tmp+=next_check(y+1,x)
    tmp+=next_check(y+1,x+1)
    return tmp

for i in range(h):
    for j in range(w):
        if s_input[i][j]=='#':
            ans_map[i][j]='#'
        else:
            ans_map[i][j]=checker(s_input,[j,i])

for m in ans_map:
    print(''.join(map(str,m)))

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] == '.':
            count = 0
            for dx, dy in (
                (-1,1), (0,1), (1,1),
                (-1,0), (0,0), (1,0),
                (-1,-1),(0,-1),(1,-1)
            ):
                now_x, now_y = i+dx, j+dy
                if 0 <= now_x and 0 <= now_y and now_x <= H-1 and now_y <= W-1 and s[now_x][now_y] == "#":
                    count += 1
            s[i][j] = str(count)

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

Submission status: Accepted

Solution code:
import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
S = [S() for _ in range(H)]
ans = []
for h,w in itertools.product(range(H),range(W)):
    if S[h][w]=='#':
        ans.append('#')
        continue
    count = 0
    for dh,dw in itertools.product([-1,0,1],repeat=2):
        nh,nw = h+dh,w+dw
        if not(0<=nh<=H-1) or not (0<=nw<=W-1):
            continue
        if S[nh][nw]=='#':
            count += 1
    ans.append(count)
for i,x in enumerate(ans,1):
    print(x,sep='',end='')
    if i%W==0:
        print()

Submission status: Accepted

Solution code:
import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
S = [S() for _ in range(H)]
ans = []
dhdw = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for h,w in itertools.product(range(H),range(W)):
    if S[h][w]=='#':
        ans.append('#')
        continue
    count = 0
    for dh,dw in dhdw:
        nh,nw = h+dh,w+dw
        if not(0<=nh<=H-1) or not (0<=nw<=W-1):
            continue
        if S[nh][nw]=='#':
            count += 1
    ans.append(count)
for i,x in enumerate(ans,1):
    print(x,sep='',end='')
    if i%W==0:
        print()

Submission status: Accepted

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

ans = [[0 for _ in range(W)] for _ in range(H)]
dx = [-1, 0, 1]
dy = [-1, 0, 1]
for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            tmp = 0
            for i in range(3):
                for j in range(3):
                    if h == 0:
                        if dx[i] == -1:
                            continue
                    if h == H-1:
                        if dx[i] == 1:
                            continue
                    if w == 0:
                        if dy[j] == -1:
                            continue
                    if w == W-1:
                        if dy[j] == 1:
                            continue
                    new_x, new_y = h+dx[i], w+dy[j]
                    if S[new_x][new_y] == '#':
                        tmp += 1
            ans[h][w] = str(tmp)
        elif S[h][w] == '#':
            ans[h][w] = '#'

for h in range(H):
    print(''.join(ans[h]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S= [[j for j in input()] for i in range(h)]
for i in range(h):
  for j in range(w):
    a=0
    if S[i][j]==".":
      for p in range(-1,2):
        for k in range(-1,2):
          if 0<=i+p<h and 0<=j+k<w:
            if S[i+p][j+k]=="#":
              a+=1
      S[i][j]=str(a)

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
li=[input() for i in range(h)]
ans=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    if li[i][j]=='#':
      ans[i][j]='#'
      for k in range(-1,2):
        for l in range(-1,2):
          if 0<=i+k<h and 0<=j+l<w:
            if ans[i+k][j+l]!='#':
              ans[i+k][j+l]+=1
anss=[map(str,ans[i]) for i in range(h)]
for i in range(h):
  print(''.join(anss[i]))

Submission status: Accepted

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

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

d = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        if A[i][j] == "#":
            B[i][j] = "#"
            for k in d:
                for l in d:
                    if 0 <= i+k < h and 0 <= j+l < w:
                        if B[i+k][j+l] != "#":
                            B[i+k][j+l] += 1


B_str = [map(str, B[i]) for i in range(h)]
for i in range(h):
    print("".join(B_str[i]))

Submission status: Accepted

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

a = [0, 0, 1, -1, 1, -1, 1, -1]
b = [1, -1, 1, -1, -1, 1, 0, 0]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            sub = 0
            for k in range(8):
                if i + a[k] >= 0 and j + b[k] >= 0 and i + a[k] < h and j + b[k] < w:
                    if s[i + a[k]][j + b[k]] == '#':
                        sub += 1
            s[i][j] = sub

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
src = [input() for i in range(H)]
ans = []
for row in src:
    ans.append(list(row))
dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '#': continue
        c = 0
        for dx,dy in dxy:
            if x+dx < 0 or x+dx > W-1 or y+dy < 0 or y+dy > H-1: continue
            if src[y+dy][x+dx] == '#': c += 1
        ans[y][x] = c
for row in ans:
    print(''.join(list(map(str,row))))

Submission status: Accepted

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

src = [input() for i in range(h)]

ans = []
for row in src:
    ans.append(list(row))
    
for i in range(h):
    for j in range(w):
        if src[i][j] == '#': 
            continue
        c = 0
        if i > 0:
            if j > 0 and src[i-1][j-1] == '#': c += 1
            if src[i-1][j] == '#': c += 1
            if j < w-1 and src[i-1][j+1] == '#': c +=1
        if j > 0 and src[i][j-1] =='#' : c +=1
        if j < w-1 and src[i][j+1] == '#': c +=1
        if i < h-1:
            if j > 0 and src[i+1][j-1] == "#": c +=1
            if src[i+1][j] == '#': c += 1
            if j<w-1 and src[i+1][j+1] == '#' : c += 1
        ans[i][j] = c
        
for row in ans:
    print (''.join(list(map(str,row))))

Submission status: Accepted

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

src = [input() for i in range(h)]

ans = []
for row in src:
    ans.append(list(row))
    
for i in range(h):
    for j in range(w):
        if src[i][j] == '#': 
            continue
        c = 0
        if i > 0:
            if j > 0 and src[i-1][j-1] == '#': c += 1
            if src[i-1][j] == '#': c += 1
            if j < w-1 and src[i-1][j+1] == '#': c +=1
        if j > 0 and src[i][j-1] =='#' : c +=1
        if j < w-1 and src[i][j+1] == '#': c +=1
        if i < h-1:
            if j > 0 and src[i+1][j-1] == "#": c +=1
            if src[i+1][j] == '#': c += 1
            if j<w-1 and src[i+1][j+1] == '#' : c += 1
        ans[i][j] = c
        
for row in ans:
    print (''.join(list(map(str,row))))