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())
sss = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if sss[i][j] == '#':
            continue
        else:
            cnt = 0
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    if 0 <= i + y < h and 0 <= j + x < w:
                        if sss[i + y][j + x] == '#':
                            cnt += 1

                    sss[i][j] = str(cnt)
for s in sss:
    print(''.join(s))

Submission status: Accepted

Solution code:
def main():
  from sys import stdin
  input = stdin.readline
  h,w = map(int,input().split())
  s = []
  sa = s.append
  sa(list("."*(w+2)))
  for _ in range(h):
    sa(["."]+list(input().strip())+["."])
  sa(list("."*(w+2)))
  for i in range(1,h+1):
    for j in range(1,w+1):
      if s[i][j] == ".":
        n = 0
        n += s[i-1][j-1:j+2].count("#")
        n += s[i][j-1:j+2].count("#")
        n += s[i+1][j-1:j+2].count("#")
        s[i][j] = str(n)
  for i in range(1,h+1):
    print("".join(s[i][1:w+1]))
main()

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
input = stdin.readline
h,w = map(int,input().split())
s = (np.array([list(input().strip()) for _ in range(h)])=="#")
ans = np.zeros((h+2,w+2),dtype=np.uint8)
for i,j in [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]:
  ans[1+i:h+1+i,1+j:w+1+j] += s
ans = ans[1:h+1,1:w+1].astype("str")
ans[s] = "#"
for i in ans:
  print("".join(i))

Submission status: Accepted

Solution code:

r,c=map(int,input().split())
grid=[[__ for __ in input()] for _ in range(r)]
grid2=[["#" for _ in range(c)] for _ in range(r)]
def pos(x,y):
    dx = [+0, +0, +1, -1, -1, +1, -1, +1]
    dy = [-1, +1, +0, +0, +1, +1, -1, -1]
    tot=0
    for a,b in zip(dx,dy):
        if 0<=a+x<r and 0<=b+y<c and grid[a+x][b+y]=="#":
            tot+=1
    return str(tot)
for i in range(r):
    for j in range(c):
        if grid[i][j]=='.':
            grid2[i][j]=pos(i,j)
for __ in grid2:
    print(''.join(__))

Submission status: Accepted

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

mp.insert(0, '.'*(W+2))
mp.append('.'*(W+2))

for i in range(1, H+1):
    for j in range(1, W+1):
        if mp[i][j] == '#':
            mp_num[i-1][j-1] += 1
            mp_num[i-1][j] += 1
            mp_num[i-1][j+1] += 1
            mp_num[i][j-1] += 1
            mp_num[i][j+1] += 1
            mp_num[i+1][j-1] += 1
            mp_num[i+1][j] += 1
            mp_num[i+1][j+1] += 1

for i in range(1, H+1):
    for j in range(1, W+1):
        if mp[i][j] == '#':
            mp_num[i][j] = '#'

for i in range(H):
    tmp = [str(a) for a in mp_num[i+1]]
    print(''.join(tmp[1:W+1]))

Submission status: Accepted

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

for h in range(H):
  for w in range(W):
    if A[h][w] == "#":
      print("#",end="")
    elif A[h][w] == ".":
      c = 0 
      for i in range(-1,2):
        for j in range(-1,2):
          if 0<=h+i<=H-1 and 0<=w+j<=W-1 and A[h+i][w+j]=="#":
            c+=1
      print(c,end="")
  print("")

Submission status: Accepted

Solution code:
import numpy as np
import scipy as sp
from scipy import signal

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

B = np.array(A)
B = np.where(B=="#",1,B)
B = np.where(B==".",0,B)
B = B.astype("int32")

s = np.ones(9).reshape(3,3)
s[1][1]=0

C = sp.signal.convolve2d(B,s,"same").astype("int32")

for h in range(H):
  for w in range(W):
    if A[h][w] == "#":
      print("#",end="")
    elif A[h][w] == ".":
      print(C[h][w],end="")
  print("")

Submission status: Accepted

Solution code:
h , w = map(int,input().split())
p = []
for i in range(h+2):
    kar = []
    if i == 0 or i == h+1:
        p.append(["!" for j in range(w+2)])
    else:
        kar = list(input())
        kar.insert(0,"!")
        kar.append("!")
        p.append(kar)
for i in range(1,h+1):
    for j in range(1,w+1):
        cou = 0
        if p[i][j] == "#":
            print("#",end="")
        elif p[i][j] ==".":
            for k in range(-1,2):
                for l in range(-1,2):
                    if p[i+k][j+l] == "#":
                        cou += 1
            print(cou,end="")
    print("")

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    board = [list(input()) for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if board[i][j] == ".":
                count = 0
                for k in range(-1, 2):
                    for l in range(-1, 2):
                        if 0 <= i + k < H and 0 <= j + l < W:
                            if board[i + k][j + l] == "#":
                                count += 1
                board[i][j] = str(count)
    for i in range(H):
        print("".join(map(str, board[i])))
main()

Submission status: Accepted

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

dxy=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
def bombcount(hi,wi):
  cnt=0
  for x,y in dxy:
    
      
    if 0<=hi+x<=h-1 and 0<=wi+y<=w-1:
      if s[hi+x][wi+y]=="#":
        
        cnt+=1
  return cnt
      
for i in range(h):
  for j in range(w):
    
    
    if s[i][j]==".":
      s[i][j]=str(bombcount(i,j))
      
for i in range(h):
  print("".join(s[i]))

Submission status: Accepted

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

# for h in range(H):
#     for w in range(W):
#         print(S[h][w], end='')
#     print()

ans = [['#' for _ in range(W)] for _ in range(H)]

# for h in range(H):
#     for w in range(W):
#         print(ans[h][w], end='')
#     print()

for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            continue
        dH = [1, 1, 0, -1, -1, -1, 0, 1]
        dW = [0, 1, 1, 1, 0, -1, -1, -1]
        cnt = 0
        for dh, dw in zip(dH, dW):
            nh = h + dh
            nw = w + dw
            if (nh < 0 or H <= nh or nw < 0 or W <= nw):
                continue
            if S[nh][nw] == '#':
                cnt += 1
        ans[h][w] = cnt

for h in range(H):
    for w in range(W):
        print(ans[h][w], end='')
    print()

Submission status: Accepted

Solution code:
# ABC 075: B – Minesweeper
h, w = map(int, input().split())
s = [input() for _ in range(h)]

mines = 0

for row in range(h):
    for column in range(w):
        if s[row][column] == '.':
            if row > 0 and column > 0: # top left
                mines = mines + 1 if s[row - 1][column -1] == '#' else mines
            if row > 0: # top
                mines = mines + 1 if s[row - 1][column] == '#' else mines
            if row > 0 and column < w - 1: # top right
                mines = mines + 1 if s[row - 1][column + 1] == '#' else mines
            if column > 0: # left
                mines = mines + 1 if s[row][column - 1] == '#' else mines
            if column < w - 1: # right
                mines = mines + 1 if s[row][column + 1] == '#' else mines
            if row < h - 1 and column > 0: # bottom left
                mines = mines + 1 if s[row + 1][column - 1] == '#' else mines
            if row < h -1 : # bottom
                mines = mines + 1 if s[row + 1][column] == '#' else mines
            if row < h - 1 and column < w - 1: # bottom right
                mines = mines + 1 if s[row + 1][column + 1] == '#' else mines
        
            #s[row][column].replace('.', str(mines))
            s[row] = s[row][:column] + str(mines) + s[row][column + 1:]
            mines = 0

for row in range(h):
    print(s[row])

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
field = [list(input())for i in range(h)]
for i in range(h):
    for j in range(w):
        res = 0
        if field[i][j] == '#':continue
        if i-1 >=0 and field[i-1][j] == '#':res+=1
        if j-1 >=0 and field[i][j-1] == '#':res+=1
        if i+1 <=h-1 and field[i+1][j] == '#':res+=1
        if j+1 <=w-1 and field[i][j+1] == '#':res+=1
        if i-1 >=0 and j-1 >= 0 and field[i-1][j-1] == '#':res+=1
        if i-1 >=0 and j+1 <= w-1 and field[i-1][j+1] == '#':res+=1
        if i+1 <=h-1 and j-1 >= 0 and field[i+1][j-1] == '#':res+=1
        if i+1 <=h-1 and j+1 <= w-1 and field[i+1][j+1] == '#':res+=1
        field[i][j] = str(res)
for i in range(h):
    print("".join(field[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
for _ in range(h):
  s.append(input())
ans=[[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j]=='#':
      ans[i][j]='#'
      continue
    i0=max(0,i-1)
    i1=min(i+1,h-1)
    j0=max(0,j-1)
    j1=min(j+1,w-1)
    p=0
    for _ in range(i0,i1+1):
      p += s[_][j0:j1+1].count('#')
    ans[i][j]=p
#print(s)
for ansi in ans:
  print(''.join(map(str,ansi)))

Submission status: Accepted

Solution code:
from itertools import product
import numpy as np
H, W = map(int, input().split())
S = [list('-' * (W + 2))] + [['-'] + list(input()) + ['-']
                             for _ in range(H)] + [list('-' * (W + 2))]
S = np.array(S, dtype='object')
# print('S', S)

d = [-1, 0, 1]
dd = list(product(d, repeat=2))
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h, w] == '.':
            cnt = 0
            for x, y in dd:
                nh = h + x
                nw = w + y
                if S[nh, nw] == '#':
                    cnt += 1
            S[h, w] = cnt

for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h, w] != '-':
            print(S[h, w], sep='', end='')
    print('')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
L = [[0] * (w) for _ in range(h)]
#print(L)
for i in range(h):
    tmp = list(input())
    #print(tmp)
    for j in range(w):
        #print(i,j)
        if tmp[j] == "#":
            #print(i,j)
            L[i][j] = "#"

#print(L)

for i in range(h):
    for j in range(w):
        if L[i][j] == "#":
            try:
                L[i+1][j] += 1
            except:
                pass
            try:
                if i-1>=0:
                    L[i-1][j] += 1
            except:
                pass
            try:
                L[i][j+1] += 1
            except:
                pass
            try:
                if j-1>=0:
                    L[i][j-1] += 1
            except:
                pass
            try:
                L[i+1][j+1] += 1
            except:
                pass
            try:
                if i-1>=0 and j-1>=0:
                    L[i-1][j-1] += 1
            except:
                pass
            try:
                if i-1>=0:
                    L[i-1][j+1] += 1
            except:
                pass
            try:
                if j-1>=0:
                    L[i+1][j-1] += 1
            except:
                pass

for i in L:
    i = list(map(str,i))
    print("".join(i))

Submission status: Accepted

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

def bomb_count(hi, wi):
    cnt = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            if (0 <= (hi + i) <= (H-1)) and (0 <= (wi + j) <= (W-1)):
                if S[hi+i][wi+j] == "#":
                    cnt += 1
    return cnt


for hi in range(H):
    for wi in range(W):
        masu = S[hi][wi]
        if masu == ".":
            S[hi][wi] = str(bomb_count(hi, wi))

for hi in range(H):
    row = S[hi]
    print("".join(row))

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        cnt = 0
        if s[i][j] == ".":
            for k in d:
                if 0 <= i + k[0] <= h-1 and 0 <= j + k[1] <= w-1:
                    if s[i+k[0]][j+k[1]] == "#":
                        cnt += 1
        if s[i][j] == "#":
            continue
        s[i][j] = str(cnt)

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a = []
a.append([u'はしっこ' for i in range(w+2)])
for i in range(h):
    a.append([u'はしっこ']+list(input())+[u'はしっこ'])
a.append([u'はしっこ' for i in range(w+2)])
#print(a)
for i in range(1,h+1):
    for j in range(1,w+1):
        if a[i][j]=='.':
            p=0
            if a[i-1][j-1]=='#':
                p+=1
            if a[i-1][j]=='#':
                p+=1
            if a[i-1][j+1]=='#':
                p+=1
            if a[i][j-1]=='#':
                p+=1
            if a[i][j+1]=='#':
                p+=1
            if a[i+1][j-1]=='#':
                p+=1
            if a[i+1][j]=='#':
                p+=1
            if a[i+1][j+1]=='#':
                p+=1
            a[i][j]=str(p)
for i in range(1,h+1):
    print(''.join(a[i][1:w+1]))

Submission status: Accepted

Solution code:
import copy
H,W = map(int,input().split())
S = [['.']*(W+2)]
for i in range(H):
    S.append(list('.'+input()+'.'))
S.append(['.']*(W+2))
T = copy.deepcopy(S)
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == '#':
            continue
        s = 0
        for k in [-1,0,1]:
            for l in [-1,0,1]:
                if S[i+k][j+l]=='#':
                    s += 1
        T[i][j] = s
for i in range(1,H+1):
    print(''.join([str(i) for i in T[i][1:W+1]]))

Submission status: Accepted

Solution code:

import numpy as np
H,W = list(map(int,input().split()))
data = [input().strip().split() for _ in range(H)]
data_ = np.array([list(str(data[i])) for i in range(H)])[:,2:-2]

d = [[0,1],[1,0],[0,-1],[-1,0],[1,1],[-1,-1],[1,-1],[-1,1]]

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

		if data_[y][x] == '#':
			continue

		count = 0
		for dy,dx in d:
			if 0 <= y+dy < H and 0 <= x+dx < W and data_[y+dy][x+dx] == '#':
				count += 1

		data_[y][x] = count

[print(''.join(i) ) for i in data_]

Submission status: Accepted

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

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


for i in range(H):
   for j in range(W):

       count = 0
       if s[i][j] == "#":
          pass
    
       else :

           if i > 0 and j > 0 and s[i-1][j-1] == "#":
               count += 1
           
           if i > 0 and s[i-1][j] == "#":
               count += 1

           if i > 0 and j < W-1 and s[i-1][j+1] == "#":
               count += 1
           
           if j > 0 and s[i][j-1] == "#":
               count += 1
           if j < W-1 and s[i][j+1] == "#":
               count += 1
           if i < H-1 and j > 0 and s[i+1][j-1] == "#":
               count += 1
           if i < H-1 and s[i+1][j] == "#":
               count += 1
           if i < H-1 and j < W-1 and s[i+1][j+1] == "#":
               count += 1
           
           s[i][j] = str(count)    

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [0 for _ in range(H)]
ans=[[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    S[i] = 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] == "#":
            ans[i][j] = "#"
            continue    # continueはループをスキップして次のループに進む　breakはループを終了
        counter = 0
        for d in range(8):    # S[i][j]に対して、隣接する8マスについて爆弾があるかを判定する
            di = i + dx[d]
            dj = j + dy[d]
            if di<0 or (H-1)<di or dj<0 or W-1<dj:
                continue
            if S[di][dj] == "#":
                counter += 1
        ans[i][j] = str(counter)
        
for i in range(H):
    ans[i] = "".join(ans[i])
    print(ans[i])

Submission status: Accepted

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

def p(i, j):
    if S[i][j] == '#':
        return 1
    return 0

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

        if j != 0:
            cnt += p(i,j-1)
        if j != W-1:
            cnt += p(i,j+1)
        
        if i != H-1:
            if j != 0:
                cnt += p(i+1,j-1)
            cnt += p(i+1,j)
            if j != W-1:
                cnt += p(i+1,j+1)
        
        S[i][j] = str(cnt)

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

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,boundary=1,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

h,w=LI()
h,w,g,_=GGI(h,w,boundary=0)
ans=['']*(h*w)
for i in range(h*w):
    if 0==i//w or i//w==h-1 or i%w==0 or i%w==w-1:
        continue
    if g[i]==1:
        ans[i]='#'
    else:
        ans[i]=str(sum([g[i+d]for d in (-1,1,w,-w,w-1,-w-1,w+1,-w+1)]))

for i in range(1,h-1):
    print(''.join(ans[i*w+1:(i+1)*w-1]))

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):
        count = 0
        if s[i][j] == '#':
            continue
        for n, m in ([-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,1],[1,0],[1,-1]):
            if i + n < 0 or i + n >= h or j + m < 0 or j + m >= w:
                continue
            if s[i+n][j+m] == '#':
                count += 1
        s[i][j] = str(count)

ans = ''
for i in range(h):
    c = ''.join(s[i])
    ans += c
    if i != h-1:
        ans += '\n'
print(ans)

Submission status: Accepted

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

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

            if i-1 >= 0 and ans[i-1][j] != "#":
                ans[i-1][j] += 1
            if i+1 <= H-1 and ans[i+1][j] != "#":
                ans[i+1][j] += 1
            
            if j+1 <= W-1 and ans[i][j+1] != "#":
                ans[i][j+1] += 1
            if j+1 <= W-1 and i-1 >= 0 and ans[i-1][j+1] != "#":
                ans[i-1][j+1] += 1
            if j+1 <= W-1 and i+1 <= H-1 and ans[i+1][j+1] != "#":
                ans[i+1][j+1] += 1                        

for i in range(H):
    print(''.join(map(str,ans[i])))

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if x[i][j] == '.':
            y[i+1][j+1] = 0
        else:
            y[i+1][j+1] = 1

for i in range(h):
    for j in range(w):
        if x[i][j] == '.':
            x[i][j] = str(sum(y[i][j:j+3])+sum(y[i+1][j:j+3])+sum(y[i+2][j:j+3]))
    print(''.join(x[i]))

Submission status: Accepted

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

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

ans = [''] * h

for y in range(h):
    for x in range(w):
        if S[y][x] == '#':
            ans[y] += '#'
        else:
            b = 0
            for dx,dy in dxy:
                nx, ny = x + dx , y + dy
                if not 0 <= nx < w: continue
                if not 0 <= ny < h: continue
                if S[ny][nx] == '#':
                    b += 1
            
            ans[y] += str(b)

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

Submission status: Accepted

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

for i in range(h):
  s = input()
  for j in range(w):
    if s[j] == "#":
      if i-1>=0 and j-1>=0:
        array[i-1][j-1] += 1
        
      if i-1>=0:
        array[i-1][j] += 1
        
      if i-1>=0 and j+1 <= w-1:
        array[i-1][j+1] += 1
        
      if j-1>=0:
        array[i][j-1] += 1
        
      array[i][j] = 9
      
      if j+1 <= w-1:
        array[i][j+1] += 1
        
      if i+1 <= h-1 and j-1>=0:
        array[i+1][j-1] += 1
        
      if i+1 <= h-1:
        array[i+1][j] += 1
      if i+1 <= h-1 and j+1 <=w-1:
        array[i+1][j+1] += 1

for i in range(h):
  for j in range(w):
    if array[i][j] >= 9:
      array[i][j] = "#"
    else:
      array[i][j] = str(array[i][j])
for i in range(h):
  print("".join(array[i]))

Submission status: Accepted

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

for i in range(1, h+1):
    ans = ''
    for j in range(1, w+1):
        if l[i][j] == '#':
            ans += '#'
        else:
            _tmp = l[i-1][j-1:j+2] + l[i][j-1:j+2] + l[i+1][j-1:j+2]
            ans += str(_tmp.count('#'))
    print(ans)

Submission status: Accepted

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

def check(S,H,W):

    for h in range(H):
        for w in range(W):
            if S[h][w] != "#":
                if h !=0 :
                    if S[h-1][w] == "#":
                        S[h][w] +=1
                if h !=H-1 :
                    if S[h+1][w] == "#":
                        S[h][w] +=1
                if w !=0 :
                    if S[h][w-1] == "#":
                        S[h][w] +=1
                if w !=W-1 :
                    if S[h][w+1] == "#":
                        S[h][w] +=1
#右斜め
                if h !=H-1  and w !=W-1:                    
                    if S[h+1][w+1] == "#":
                        S[h][w] +=1
                if h != 0  and w !=W-1:                    
                    if S[h-1][w+1] == "#":
                        S[h][w] +=1
#左斜め下　左斜め上
                if h !=H-1  and w !=0:                    
                    if S[h+1][w-1] == "#":
                        S[h][w] +=1

                if h != 0  and w !=0:                    
                    if S[h-1][w-1] == "#":
                        S[h][w] +=1

    for a in S:
       print(*a,sep='')
 

def convert(S,H,W):
    
    for h in range(H):
        for w in range(W):
            if S[h][w]==".":
                S[h][w] = 0
    return S

S,H,W = nyu()
S =convert(S,H,W)
check(S,H,W)

Submission status: Accepted

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

res = []

for i in range(H):
    r = []
    for j in range(W):
        if S[i][j] == '#':
            r.append('#')
        else:
            count = 0
            for d in [(-1, 1), (0, 1), (1, 1), (-1, 0), (0, 0), (1, 0), (-1, -1), (0, -1), (1, -1)]:
                x = i+d[0]
                y = j+d[1]

                if (0 <= x < H) and (0 <= y < W) and S[x][y] == '#':
                    count += 1
            r.append(count)
    res.append(r)

for i in range(H):
    print(''.join(map(str, res[i])))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [input() for i in range(h)]
grid = [[0 for i in range(w+2)] for j in range(h+2)]
grid2 = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            grid[i+1][j+1] = 1
for i in range(h):
    for j in range(w):
        if grid[i+1][j+1] == 0:
            grid2[i][j] += sum(grid[i][j:j+3]) +sum(grid[i+1][j:j+3]) + sum(grid[i+2][j:j+3])
            grid2[i][j] = str(grid2[i][j])
        else:
            grid2[i][j] = '#'
    print(''.join(grid2[i]))

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 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 = [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 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 = list(map(int, input().split()))
s = []
a = "."*(w+2)
s.append(a)
for i in range(h):
    p = input()
    x = "."+p+"."
    s.append(x)
s.append(a)


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
            print(count, end="")
        else:
            print("#", end="")
    print()

#print(s)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
map = a = [ list(input()) for _ in range(h) ]
d = [[1, 0], [0, 1], [1, 1], [-1, 0], [0, -1], [-1, -1], [1, -1], [-1, 1]]

for i in range(h):
    for j in range(w):
        cnt = 0
        if map[i][j] == ".":
            for dx, dy in d:
                nx = j + dx
                ny = i + dy
                if 0 <= nx < w and 0 <= ny < h and map[ny][nx] == "#":
                    cnt += 1
        if map[i][j] != "#":
            map[i][j] = cnt

for i in range(h):
    print(*map[i],sep="")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
d = [[-1,-1], [-1,0], [-1,1], [0,-1], [0,1], [1,-1], [1,0], [1,1]]

for i in range(h):
    a = []
    y = input()
    for j in y:
        a.append(j)
    s.append(a)

for y in range(h):
    for x in range(w):
        c = 0
        if s[y][x] == "#": continue

        for u in d:
            if x + u[0] < 0 or x + u[0] > w-1 or y + u[1] < 0 or y + u[1] > h -1: continue
            if s[y + u[1]][x + u[0]] == "#": c += 1

        s[y][x] = c

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

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            RES[i][j] = "#"
            for n in range(-1, 2):
                for m in range(-1, 2):
                    if not(n == 0 and m == 0) and 0 <= i + n and i + n < H and 0 <= j + m and j + m < W:
                        if RES[i + n][j + m] != "#":
                            RES[i + n][j + m] = str(int(RES[i + n][j + m]) + 1)

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