Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

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

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

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

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

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

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

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

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

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

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

Sample Input 1

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

Sample Output 1

11211
1#2#1
11211

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

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

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

Sample Input 2

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

Sample Output 2

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

It is possible that there is no empty square.  

Sample Input 3

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

Sample Output 3

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



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


Sample output:
11211
1#2#1
11211

Submission status: Accepted

Solution code:
def count(i,j):
  count=0
  if list[i-1][j-1]=='#':
    count += 1
  if list[i-1][j]=='#':
    count += 1
  if list[i-1][j+1]=='#':
    count += 1
  if list[i][j-1]=='#':
    count += 1
  if list[i][j+1]=='#':
    count += 1
  if list[i+1][j-1]=='#':
    count += 1
  if list[i+1][j]=='#':
    count += 1
  if list[i+1][j+1]=='#':
    count += 1
  return count

h,w=map(int, input().split())
s_init='0'*(w+2)
list=[]
list.append(s_init)
for i in range(h):
  s=str(input())
  list.append('0'+s+'0')
list.append(s_init)
for i in range(1,h+1):
  for j in range(1,w+1):
    if list[i][j]=='.':
      list[i]=list[i][:j]+str(count(i,j))+list[i][j+1:]
for i in range(h):
  print(list[i+1][1:-1])

Submission status: Accepted

Solution code:
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def judge(n,m):
    if (0 <= n < H) and (0 <= m < W) and s[n][m]=="#":
        return True

def cnt(a,b):
    l = [-1,0,1]
    num = 0
    for i in l:
        for j in l:
            if i == j == 0:
                continue
            else:
                if judge(a+i,b+j):
                    num += 1
    return num

H,W = IL()
s = [[i for i in S()] for i in range(H)]
ans = [["#" for i in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        if s[i][j] == ".":
            ans[i][j] = cnt(i,j)

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())

hoge=[]
hoge.append(["."]*(m+2))
for i in range(n):
  hoge.append(["."]+list(input())+["."])
hoge.append(["."]*(m+2))

for i in range(n):
  for j in range(m):
    a=i+1
    b=j+1
    if(hoge[a][b]=="#"):
      print("#",end="")
    else:
      ans=0
      if(hoge[a-1][b-1]=="#"):
        ans+=1
      if(hoge[a-1][b]=="#"):
        ans+=1
      if(hoge[a-1][b+1]=="#"):
        ans+=1
      if(hoge[a][b-1]=="#"):
        ans+=1
      if(hoge[a][b+1]=="#"):
        ans+=1
      if(hoge[a+1][b-1]=="#"):
        ans+=1
      if(hoge[a+1][b]=="#"):
        ans+=1
      if(hoge[a+1][b+1]=="#"):
        ans+=1
      print(ans,end="")
  print("")

Submission status: Accepted

Solution code:
h, w = [int(item) for item in input().split()]
field = ["%" * (w+2)] + ["%" + input() + "%" for _ in range(h)] + ["%" * (w+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
        count = 0
        if field[i][j] == ".":
            for k in [-1,0,1]:
                for l in [-1,0,1]:
                    if field[i+k][j+l] == "#":
                        count += 1
            field[i] = field[i][:j] + str(count) + field[i][j+1:]

for line in field[1:h+1]:
    for c in line:
        if c !="%":
            print(c, end="")
    print("")

Submission status: Accepted

Solution code:
from sys import exit
H,W = [int(n) for n in input().split()]
S = [0]*H
dxy = [[1,0],[0,1],[0,-1],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]]
for i in range(H):
    S[i] = list(str(input()))

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for d in dxy:
                if(0<= i+d[0] < H and 0<= j+d[1] < W and S[i+d[0]][j+d[1]] == "#"):
                    cnt+=1
            S[i][j] = cnt

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())
s = [input() for i in range(h)]

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

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

Submission status: Accepted

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

T = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    for j, sj in enumerate(S[i]):
        if sj != '.':
            T[i][j] = sj
            continue

        count = 0
        if 0 <= i-1 and 0 <= j-1 and S[i-1][j-1] == '#':
            count += 1
        if 0 <= i-1 and S[i-1][j] == '#':
            count += 1
        if 0 <= i-1 and j+1 < W and S[i-1][j+1] == '#':
            count += 1
        if 0 <= j-1 and S[i][j-1] == '#':
            count += 1
        if j+1 < W and S[i][j+1] == '#':
            count += 1
        if i+1 < H and 0 <= j-1 and S[i+1][j-1] == '#':
            count += 1
        if i+1 < H and S[i+1][j] == '#':
            count += 1
        if i+1 < H and j+1 < W and S[i+1][j+1] == '#':
            count += 1
            
        T[i][j] = count

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

Submission status: Accepted

Solution code:
h, w = [int(n) for n in input().split()]

B = [['.'] * (w + 2)]
for i in range(h):
    s = ['.'] + list(input()) + ['.']
    B.append(s)
B.append(['.'] * (w + 2))

for i in range(1, h+1):
    for j in range(1, w+1):

        if B[i][j] == '#':
            continue
        else:
            
            B[i][j] = str((B[i-1][j-1:j+2] + [B[i][j-1]] + [B[i][j+1]] + B[i+1][j-1:j+2]) .count('#'))

#print(B)

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
Ss = [["."]+[j for j in input()]+["."] for i in range(H)]
Ss.insert(0,["."]*len(Ss[0]))
Ss.append(["."]*len(Ss[0]))
ans = []
for i in range(1,len(Ss)-1):
    nums = []
    for j in range(1,len(Ss[i])-1):
        num = 0
        if Ss[i][j]==".":
            if Ss[i+1][j]=="#":
                num+=1
            if Ss[i+1][j+1]=="#":
                num+=1
            if Ss[i+1][j-1]=="#":
                num+=1
            if Ss[i-1][j]=="#":
                num+=1
            if Ss[i-1][j+1]=="#":
                num+=1
            if Ss[i-1][j-1]=="#":
                num+=1
            if Ss[i][j+1]=="#":
                num+=1
            if Ss[i][j-1]=="#":
                num+=1
            nums.append(str(num))
        else:
            nums.append("#")
    print("".join(nums))

Submission status: Accepted

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

def count(x,y):
  p = 0
  if x >= 1 and y >= 1 and S[x-1][y-1] == "#":
    p += 1
  if x >= 1 and S[x-1][y] == "#":
    p += 1
  if x >= 1 and y <= w - 2 and S[x-1][y+1] == "#":
    p += 1
  if y >= 1 and S[x][y-1] == "#":
    p += 1
  if y <= w - 2 and S[x][y+1] == "#":
    p += 1
  if x <= h - 2 and y >= 1 and S[x+1][y-1] == "#":
    p += 1
  if x <= h - 2 and S[x+1][y] == "#":
    p += 1
  if x <= h - 2 and y <= w - 2 and S[x+1][y+1] == "#":
    p += 1
  p = str(p)
  return(p)

for i in range(h):
  ans = ""
  for j in range(w):
    if S[i][j] == "#":
      ans += "#"
    else:
      ans += count(i,j)
  print(ans)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
#get hight and weidth 
glid = [0] * ( h+ 2)

h_index = [-1, -1, -1, 0, 0, 1, 1, 1]
w_index = [-1, 0, 1, -1, 1, -1, 0, 1]

glid[0]=[0] * ( w + 2 )
glid[h + 1 ] = [0] * ( w + 2 )

for i in range(h):
	temp1 = [0]
	temp2 = list(input())
	temp1.extend(temp2)
	temp1.append(0)
	glid[ i + 1 ] = temp1

# get glids 

for j in range(h):
	for k in range(w):
		if glid[ j + 1 ][ k + 1 ] == "#":
			continue
		else:
			cnt = 0
			for n in range(8):
				if glid[ j  + 1 + h_index[ n ] ][ k + 1 + w_index[ n ]] == "#" :
					cnt += 1
			glid[ j + 1 ][ k +1 ] = str(cnt)

for h_print in range ( h ):
	for w_print in range ( w ):
		print(glid[h_print + 1][w_print + 1], end="")
	print()

Submission status: Accepted

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

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], S[i - 1][j], S[i - 1][j + 1], S[i][j - 1], S[i][j + 1], S[i + 1][j - 1], S[i + 1][j], S[i + 1][j + 1]].count('#'))

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

Submission status: Accepted

Solution code:
import sys; sys.setrecursionlimit(50000)
input = sys.stdin.readline

H, W = map(int, input().split())
grid = [list(input().replace("\n","")) for _ in range(H)]
dir4 = ((0, 1), (0, -1), (1, 0), (-1, 0))
dir8 = ((0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (1, -1), (-1, 1), (1, 1))

for i in range(H):
    for j in range(W):
        if grid[i][j] != ".": continue
        cnt = 0
        for d in dir8:
            nx = i + d[0]
            ny = j + d[1]
            if not (0 <= nx < H): continue
            if not (0 <= ny < W): continue
            if grid[nx][ny] == "#": cnt += 1
        grid[i][j] = str(cnt)
    print(*grid[i], sep="")

Submission status: Accepted

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

for i in range(h):
  l = ""
  for j in range(w):
    if s[i][j] == "#":
      l += "#"
    else:
      x = 0
      for dh in [-1, 0, 1]:
        for dw in [-1, 0, 1]:
          if 0 <= i + dh < h and 0 <= j + dw < w and s[i + dh][j + dw] == "#":
            x += 1
      l += str(x)
  print(l)

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

Submission status: Accepted

Solution code:
import numpy as np

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

ans = np.array([['0']*W for i in range(H)])
for i in range(H):
    for j in range(W):
        if S[i, j] == '#':
            ans[i, j] = '#'
        else:
            ans[i, j] = np.sum(S[max(0, i-1):min(i+2, H), max(0, j-1):min(j+2, W)] == '#')

for x in ans:
    print(''.join(list(x)))

Submission status: Accepted

Solution code:
import sys; sys.setrecursionlimit(50000)
input = sys.stdin.readline

H, W = map(int, input().split())
grid = [list(input()) for _ in range(H)]
dir4 = ((0, 1), (0, -1), (1, 0), (-1, 0))
dir8 = ((0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (1, 1), (-1, 1), (1, -1))

for i in range(H):
    for j in range(W):
        if grid[i][j] != "#": continue
        for d in dir8:
            nx = i + d[0]
            ny = j + d[1]
            if not (0 <= nx < H): continue
            if not (0 <= ny < W): continue
            if grid[nx][ny] == ".":
                grid[nx][ny] = "1"
            elif grid[nx][ny] == "#" or grid[nx][ny] == "\n":
                pass
            else:
                grid[nx][ny] = str(int(grid[nx][ny]) + 1)

for s in grid:
    print("".join(s).replace(".", "0").replace("\n",""))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[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 i in range(h):
  for j in range(w):
    if l[i][j]=='.':
      count=0
      for k in range(8):
        nx=i+dx[k]
        ny=j+dy[k]
        if nx<0 or ny<0 or nx>=h or ny>=w:
          continue
        else:
          if l[nx][ny]=='#':
            count+=1
      l[i][j]=str(count) 
for i in l:
  print(''.join(i))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[[0]*(w+2)]
for i in range(h):
  a=list(input())
  a.insert(0,0)
  a.append(0)
  l.append(a)
l.append([0]*(w+2))
pointx=[-1,0,1]
pointy=[-1,0,1]
for i in range(1,h+1):
  for j in range(1,w+1):
    if l[i][j]=='.':
      count=0
      for k in pointx:
        for m in pointy:
          if l[i+k][j+m]=='#':
            count+=1
      l[i][j]=str(count)

del l[0]
del l[h]
for i in range(h):
  l[i]=l[i][1:w+1]
for i in l:
  print(''.join(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] != "#":
            cnt = 0
            if i != 0:
                if j != 0:
                    if S[i-1][j-1] == "#":
                        cnt += 1
                if S[i-1][j] == "#":
                    cnt += 1
                if j != W-1:
                    if S[i-1][j+1] == "#":
                        cnt += 1
            if j != 0:
                if S[i][j-1] == "#":
                    cnt += 1
            if j != W-1:
                if S[i][j+1] == "#":
                    cnt += 1
            if i != H-1:
                if j != 0:
                    if S[i+1][j-1] == "#":
                        cnt += 1
                if S[i+1][j] == "#":
                    cnt += 1
                if j != W-1:
                    if S[i+1][j+1] == "#":
                        cnt += 1
            S[i][j] = str(cnt)
    S_str = "".join(S[i])
    print(S_str)
    S_str = ""

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
s = [input() for i in range(H)]
ans = [[0]*W for i in range(H)]
for i in range(H):
  for j in range(W):
    count = 0
    if s[i][j] != '#':
      if i+1 < H and s[i+1][j] == '#':
        count+=1
      if j+1 < W and s[i][j+1] == '#':
        count+=1
      if i-1 >= 0 and s[i-1][j] == '#':
        count+=1
      if j-1 >= 0 and s[i][j-1] == '#':
        count+=1
      if i+1 < H and j+1 < W and s[i+1][j+1] == '#':
        count+=1
      if i+1 < H and j-1 >= 0 and s[i+1][j-1] == '#':
        count+=1
      if i-1 >= 0 and j+1 < W and s[i-1][j+1] == '#':
        count+=1
      if i-1 >= 0 and j-1 >= 0 and s[i-1][j-1] == '#':
        count+=1
      ans[i][j]=count
    else:
      ans[i][j] = '#'
for i in range(H):
  tmp=''
  for j in range(W):
    tmp+=str(ans[i][j])
  print(tmp)

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
ats = []
for i in range(W+2):
  ats.append('@')
grid = [ats]
for i in range(H):
  S_i = ['@']
  S_i_raw = input()
  for j in range(W):
    S_i.append(S_i_raw[j])
  S_i.append('@')
  grid.append(S_i)
grid.append(ats)

for h in range(H):
  for w in range(W):
    counter = 0
    if grid[h+1][w+1] == '.':
      if grid[h][w] == '#':
        counter += 1
      if grid[h][w+1] == '#':
        counter += 1
      if grid[h][w+2] == '#':
        counter += 1
      if grid[h+1][w] == '#':
        counter += 1
      if grid[h+1][w+2] == '#':
        counter += 1
      if grid[h+2][w] == '#':
        counter += 1
      if grid[h+2][w+1] == '#':
        counter += 1
      if grid[h+2][w+2] == '#':
        counter += 1
      grid[h+1][w+1] = str(counter)
      
for i in range(H):
  ans=''
  for j in range(W):
    ans = ans + grid[i+1][j+1]
  print(ans)

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) :
        cnt = 0
        if S[i][j] == "." :
            for a in range(-1, 2) :
                for b in range(-1, 2) :
                    if i + a >= 0 and i + a < H and j + b >= 0 and j + b < W :
                        if S[i + a][j + b] == "#" :
                            cnt += 1
            S[i][j] = str(cnt)

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())
m=[[0for _ in range(w+2)]for _ in range(h+2)]
for y in range(h):
  s=input()
  for x in range(w):
    if s[x]=="#":
      for i in [0,1,2]:
        for j in [0,1,2]:
          m[y+j][x+i]+=1
      m[y+1][x+1]=9
for y in range(1,h+1):
  ans=""
  for x in range(1,w+1):
    if m[y][x]>8:
      ans+="#"
    else:
      ans+=str(m[y][x])
  print(ans)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える

h,w = map(int,input().split())
s = ["."*(w+2)]
for i in range(h):
    s.append("."+str(input())+".")
s.append("."*(w+2))

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
#pading
S = [['P' for _ in range(W+2)] for _ in range(H+2)]
for i in range(H):
    s = list(input()) 
    for j in range(W):
        S[i+1][j+1] = s[j]
for i in range(H):
    for j in range(W):
        if S[i+1][j+1]=='.':
            counter = 0
            for k in [i, i+1, i+2]:
                for l in [j, j+1, j+2]:
                    if S[k][l] == '#':
                        counter += 1
            S[i+1][j+1] = str(counter)
for i in range(1,H+1):
    print(''.join(S[i][1:W+1]))

Submission status: Accepted

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

for i in range(1, h+1):
    for j in range(1, w+1):
        if m[i][j] == '.':
            m[i][j] = 0    
            for di in range(-1,2):
                for dj in range(-1,2):
                    if m[i+di][j+dj] == '#':
                        m[i][j] += 1
    print(''.join(map(str, m[i][1:w+1])))

Submission status: Accepted

Solution code:
from sys import stdin
h, w = [int(_) for _ in stdin.readline().rstrip().split()]
data = [list(stdin.readline().rstrip()) for _ in range(h)]

for i in range(h):
    for j in range(w):
        if data[i][j] == '.':
            count = 0
            for ii in range(i-1, i+2):
                for jj in range(j-1, j+2):
                    if 0 <= ii < h and 0 <= jj < w:
                        if data[ii][jj] == '#':
                           count += 1
        else:
            count = '#'
        print(count, end='')
    print()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
mas = [input() for i in range(H)]
dx = [-1, 0, 1, -1, 0, 1, -1, 0, 1]
dy = [-1, -1, -1, 0, 0, 0, 1, 1, 1]

ans = []
for i in range(H) :
    ans.append(['#'] * W)

for h in range(H) :
    for w in range(W) :
        c = 0
        if mas[h][w] == '#':
            continue
        for i in range(9) :
            if w + dx[i] < 0 or h + dy[i] < 0 or w + dx[i] > W - 1 or h + dy[i] > H - 1:
                continue
            if mas[h+dy[i]][w+dx[i]] == '#' :
                c += 1
        ans[h][w] = c

for a in ans :
    b = map(str, a)
    print(''.join(b))

Submission status: Accepted

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

def grid(x,y):
  field = 0
  for dy in range(-1,2):
    for dx in range(-1,2):
      nx = x + dx
      ny = y + dy
      if 0 <= nx and nx < W and 0 <= ny and ny < H and L[ny][nx] == '#':
        field += 1
  return str(field)
    

for y in range(H):
  ans = ''
  for x in range(W):
    if L[y][x] != '#':
      ans += grid(x,y)
    else:
      ans += '#'
  print(ans)

Submission status: Accepted

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

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

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

del s[0]
del s[-1]

for i in range(h):
    s[i] =  s[i][1:-1]
    s[i] = map(str,s[i])
    s[i] = "".join(s[i])
    print(s[i])

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    a,b,c = LI()
    if a not in [b,c]:
        print(a)
    elif b not in [a,c]:
        print(b)
    else:
        print(c)
    return

#B
def B():
    h,w = LI()
    s = SR(h)
    ans = [[0 for i in range(w)] for j in range(h)]
    d = [[0,1],[0,-1],[1,0],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]]
    for y in range(h):
        for x in range(w):
            if s[y][x] == "#":
                ans[y][x] += 10
                for dy,dx in d:
                    y2 = y+dy
                    x2 = x+dx
                    if 0 <= y2 < h and 0 <= x2 < w:
                        ans[y2][x2] += 1
    for y in range(h):
        for x in range(w):
            if ans[y][x] >= 10:
                print("#",end = "")
            else:
                print(ans[y][x],end = "")
        print()
    return

#C
def C():
    def bfs(a,b):
        bfs_map = [1 for i in range(n)]
        bfs_map[0] = 0
        q = deque()
        q.append(0)
        s = 1
        while q:
            x = q.pop()
            for y in v[x]:
                if [x,y] != [a,b] and [x,y] != [b,a]:
                    if bfs_map[y]:
                        bfs_map[y] = 0
                        q.append(y)
                        s += 1
        return s
    n,m = LI()
    v = [deque() for i in range(n)]
    l = deque()
    for i in range(m):
        a,b = LI()
        a -= 1
        b -= 1
        v[a].append(b)
        v[b].append(a)
        l.append([a,b])
    ans = 0
    for a,b in l:
        s = bfs(a,b)
        if s < n:
            ans += 1
    print(ans)
#D
def D():
    n,k = LI()
    v = [None for i in range(n)]
    for i in range(n):
        v[i] = LI()
        v[i] = [[None,None]]+[v[i]]
    v.sort(key = lambda x:x[1][1])
    for i in range(n):
        v[i][0][1] = i
    v.sort(key = lambda x:x[1][0])
    for i in range(n):
        v[i][0][0] = i
    po = [[None for i in range(n)] for j in range(n)]
    h = [None for i in range(n)]
    w = [None for i in range(n)]
    for i in range(n):
        po[v[i][0][0]][v[i][0][1]] = v[i][1]
        h[v[i][0][0]] = v[i][1][0]
        w[v[i][0][1]] = v[i][1][1]
    re = [[[[0 for x in range(n)] for y in range(n)] for t in range(n)] for s in range(n)]
    ans = float("inf")
    for s in range(n):
        for t in range(n):
            for y in range(s,n):
                for x in range(t,n):
                    if x == t:
                        re[s][t][y][x] = re[s][t][y-1][x]
                    else:
                        re[s][t][y][x] = max(0,re[s][t][y][x-1]+re[s][t][y-1][x]-re[s][t][y-1][x-1])
                    if po[y][x] != None:
                        re[s][t][y][x] += 1
                    if re[s][t][y][x] >= k:
                        ans = min(ans,(h[y]-h[s])*(w[x]-w[t]))
    print(ans)
#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    B()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
e=["*" for i in range(w+2)]
s=[e]
for i in range(h):
  s.append(["*"]+list(input())+["*"])
s.append(e)

for i in range(1,h+1):
  for j in range(1,w+1):
    if s[i][j]==".":
      s[i][j]=str(sum(s[k][j-1:j+2].count("#") for k in range(i-1,i+2)))
  print("".join(s[i][1:w+1]))

Submission status: Accepted

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

for h in range(0, H):
    h_strs = []
    for w in range(0, W):
        if Sn[h][w] == '#':
            h_strs.append('#')
            continue
        n = 0
        for sh in range(h-1, h+2):
            for sw in range(w-1, w+2):
                if 0 <= sh and sh < H and 0 <= sw and sw < W:
                    if Sn[sh][sw] == '#':
                        n += 1
        h_strs.append(str(n))
    Sn[h] = "".join(h_strs)

for S in Sn:
    print(S)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
Map=['.'*(W+2)]
for i in range(H):
    line='.'+input()+'.'
    x=[]
    for j in range(W+2):
        x.append(line[j])
    Map.append(x)
Map.append('.'*(W+2))
for h in range(1,1+H):
    for w in range(1,1+W):
        if Map[h][w]=='.':
            cnt=0
            for i,j in [(-1,-1),(-1,0),(0,-1),(1,-1),(-1,1),(1,0),(0,1),(1,1)]:
                if Map[h+i][w+j]=='#':cnt+=1
            Map[h][w]=str(cnt)
for h in range(1,1+H):
    print(''.join(Map[h][1:-1]))

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if masu[i][j] == ".":
            count = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    xx = j + dx
                    yy = i + dy
                    if 0 <= xx and xx < w and 0 <= yy and yy < h:
                        if masu[yy][xx] == "#":
                            count += 1
            masu[i][j] = count
for i in range(h):
    print("".join(map(str, masu[i])))

Submission status: Accepted

Solution code:
def mine(i, j):
    if masu[i][j] == ".":
        masu[i][j] = 0
    if masu[i][j] != "#":
        masu[i][j] += 1
    return masu[i][j]


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

for i in range(1, h+1):
    masu[i] = [0]+list(input())+[0]
for i in range(h+2):
    for j in range(w+2):
        if masu[i][j] == ".":
            masu[i][j] = 0
        if masu[i][j] == "#":
            masu[i-1][j-1] = mine(i-1, j-1)
            masu[i-1][j] = mine(i-1, j)
            masu[i-1][j+1] = mine(i-1, j+1)
            masu[i][j-1] = mine(i, j-1)
            masu[i][j+1] = mine(i, j+1)
            masu[i+1][j-1] = mine(i+1, j-1)
            masu[i+1][j] = mine(i+1, j)
            masu[i+1][j+1] = mine(i+1, j+1)

for i in range(1, h+1):
    print("".join(map(str, masu[i][1:-1])))

Submission status: Accepted

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

def countBom(S,i,j):
    con_bom =0
    for h in range(-1,2):
        for w in range(-1,2):
            if S[i+h][j+w] == "#":
                con_bom += 1
    return con_bom




for i in range(H):
    for j in range(W):
        if S[i+1][j+1] ==".":
            S[i+1][j+1] = countBom(S,i+1,j+1)
    print("".join(list(map(str,(S[i+1])))))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = []
def calc(i,j,k,l):
    if k==0 and l==0:
        return 0
    elif i+k==-1 or j+l==-1 or i+k==h or j+l==w:
        return 0
    elif s[i+k][j+l] == '#':
        count[i][j] = count[i][j] + 1
for i in range(h):
    s += [input()]
count = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        for k in range(3):
            for l in range(3):
                calc(i,j,k-1,l-1)
for i in range(h):
    s_tmp=''
    for j in range(w):
        if s[i][j]=='#':
            s_tmp += '#'
        else:
            s_tmp += str(count[i][j])
    s[i]=s_tmp
for i in range(h):
    print(s[i])

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            count = 0
            for y in range(-1, 2):
                for x in range(-1, 2):
                     if 0 <= h+y and h+y < H and 0 <= w+x and w+x < W:
                         if S[h+y][w+x] == '#':
                             count += 1
        else:
            count = '#'
        print(count, end='')
    print()

Submission status: Accepted

Solution code:

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

def aaa(i, j):
    cnt = 0
    for x in [-1, 0, 1]:
        for y in [-1, 0, 1]:
            if (not 0 <= i + y < H) or (not 0 <= j + x < W):
                continue
            if S[i + y][j + x] == '#':
                cnt += 1    
    return str(cnt)

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        S[i][j] = aaa(i, j)
for s in S:
    print("".join(s))