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

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

result = []
for temp_h in range(h):
  temp_s = ""
  for temp_w in range(w):
    if list_s[temp_h][temp_w] == "#":
      temp_s += "#"
      continue
    temp_num = 0
    for one_pair in list_pair:
      check_h = temp_h + one_pair[0]
      check_w = temp_w + one_pair[1]
      if 0 <= check_h < h and 0 <= check_w < w:
        if list_s[check_h][check_w] == "#":
          temp_num += 1
    temp_s += str(temp_num)
  result.append(temp_s)

for one_line in result:
  print(one_line)

Submission status: Accepted

Solution code:
# coding: utf-8
H, W = map(int, input().split())
S = []
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
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] == "#":
            continue
        
        for k in range(len(dx)):
            if i+dx[k] < 0 or i+dx[k] >= H:
                continue
            if j+dy[k] < 0 or j+dy[k] >= W:
                continue
            if S[i+dx[k]][j+dy[k]] == "#":
                cnt += 1
        S[i][j] = str(cnt)
for i in range(H):
    print("".join(S[i]))

Submission status: Accepted

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

cnt=[[0 for i in range(w+2)] for j in range(h+2)]

for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            cnt[i][j]+=1
            cnt[i][j+1]+=1
            cnt[i][j+2]+=1
            cnt[i+1][j]+=1
            cnt[i+1][j+2]+=1
            cnt[i+2][j]+=1
            cnt[i+2][j+1]+=1
            cnt[i+2][j+2]+=1

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

for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            ans[i][j]='#'
        else:
            ans[i][j]=str(cnt[i+1][j+1])

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
bombs= []
for _h in range(h):
    _s = input()
    for _w in range(w):
        if _s[_w] == "#":
            bombs.append((_h, _w))
    s.append(_s)

a = [[0]*(w+1) for _ in range(h+1)]

for _h, _w in bombs:
        a[_h-1][_w-1] += 1
        a[_h-1][_w] += 1
        a[_h-1][_w+1] += 1

        a[_h][_w-1] += 1
        a[_h][_w+1] += 1

        a[_h+1][_w-1] += 1
        a[_h+1][_w] += 1
        a[_h+1][_w+1] += 1

for _h, _w in bombs:
    a[_h][_w] = "#"
a.pop(-1)
for _a in a:
    _a.pop(-1)
    print("".join(map(str, _a)))

Submission status: Accepted

Solution code:
def count(i, j):
  count = 0
  for k in range(-1, 2):
    for l in range(-1, 2):
      if (k, l) != (0, 0) \
        and 0 <= i+k < H \
        and 0 <= j+l < W \
        and S[i+k][j+l] == '#':
        count += 1
  return count

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] = str(count(i, j))

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split());s=[list(input()) for _ in range(h)]
for i in range(h):
    moji = ''
    for j in range(w):
        cnt = 0
        if s[i][j] == '#':
            moji += '#'
        else:
            for ih in [-1, 0, 1]:
                for jw in [-1, 0, 1]:
                    if 0 <= i + ih < h and 0 <= j + jw < w:
                        if ih == 0 and jw == 0:
                            pass
                        elif s[i + ih][j + jw] == '#':
                            cnt += 1
                        else:
                            pass
            moji += str(cnt)
    print(moji)

Submission status: Accepted

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

def check_bomb(i,j):
    if i<0 or j<0:
        return 0
    try:
        if H_lis[i][j]==".":
            return 0
        elif H_lis[i][j]=="#":
            return 1
        else:
            return 0
    except:
        return 0

for i in range(H):
    ans = ""
    for j in range(W):
        if check_bomb(i,j)==1:
            ans += "#"
        else:
            ans+=str(check_bomb(i-1,j-1)+check_bomb(i-1,j)+check_bomb(i-1,j+1)+check_bomb(i,j-1)+check_bomb(i,j+1)+check_bomb(i+1,j-1)+check_bomb(i+1,j)+check_bomb(i+1,j+1))
    print(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
s=[list(input()) for i in range(H)]
for i in range(H):
  ans=["0"]*W
  for j in range(W):
    if s[i][j]=="#":
      ans[j]="#"
      continue
    count=0
    if i > 0:
      if s[i-1][j]=="#":
        count+=1
      if j > 0:
        if s[i-1][j-1]=="#":
          count+=1
      if j < W-1:
        if s[i-1][j+1]=="#":
          count+=1
    if i<H-1:
      if s[i+1][j]=="#":
        count+=1
      if j >0:
        if s[i+1][j-1]=="#":
          count+=1
      if j<W-1:
        if s[i+1][j+1]=="#":
          count+=1
    if j>0:
      if s[i][j-1]=="#":
        count+=1
    if j<W-1:
      if s[i][j+1]=="#":
        count+=1
    ans[j]=str(count)
  print("".join(ans))

Submission status: Accepted

Solution code:
def is_in(min, i, max):
    if i < min:
        ret = False
    elif max <= i:
        ret = False
    else:
        ret = True
    return ret


def main():
    height, width = map(int, input().split())

    matrix = []
    for i in range(height):
        temp_s = input()
        matrix.append(list(temp_s))

    ans_matrix = []
    for i in range(height):
        temp_row = []
        for j in range(width):
            if matrix[i][j] == '#':
                string = '#'
            else:
                count = 0
                for x in range(-1, 2, 1):
                    for y in range(-1, 2, 1):
                        if x == 0 and y == 0:
                            continue

                        if is_in(0, i+x, height) and is_in(0, j+y, width):
                            temp = matrix[i+x][j+y]
                            if temp == '#':
                                count += 1
                string = str(count)
            temp_row.append(string)
        ans_matrix.append(temp_row[:])
    for i in range(height):
        print(''.join(ans_matrix[i]))
    print()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H = 0
W = 0
Data = []
Ans  = []

def check(h,w):
    if Data[h][w] == '#':
        return '#'

    num = 0
    dx = [-1,-1,-1,0 ,0, 1,1,1]
    dy = [-1, 0, 1,-1,1,-1,0,1]
    for i in range(8):
        if Data[h+dx[i]][w+dy[i]] == '#':
            num += 1
    return str(num)

H,W = map(int,input().split())
Data.append([0]*(W+2))
for _ in range(H):
    s = list(input())
    s.insert(0,0)
    s.append(0)
    Data.append(s)
Data.append([0]*(W+2))

for h in range(1,H+1):
    s = ''
    for w in range(1,W+1):
        s += check(h,w)
    Ans.append(s)

for a in Ans:
    print(a)

Submission status: Accepted

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

for i in range(H):
    A = ""
    for j in range(W):
        if S[i][j] == "#":
            A += "#"
        else:
            C = 0
            for SS in S[max(0,i-1):min(H,i+2)]:
                C += SS[max(0,j-1):min(W,j+2)].count("#")
            A += str(C)
    print(A)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
dx = [1,1,1,-1,-1,-1,0,0]
dy = [0,1,-1,0,1,-1,1,-1]
for i in range (h):
	z=[]
	for j in range (w):
		if(s[i][j] == "."):
			cnt = 0
			for k in range (8):
				x = i + dx[k]
				y = j + dy[k]
				if(x >= 0 and x < h and y >= 0 and y < w and s[x][y] == "#"):
					cnt += 1
			z.append(cnt)
		else:
			z.append("#")
	print (*z, sep="")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[[0 for i in range(W)] for j in range(H)]
for i in range(H):
  a=list(map(str,input()))
  for j in range(W):
    S[i][j]=a[j]
  a=[]
V=[[0 for i in range(W)] for j in range(H)]
for m in range(H):
  for n in range(W):
    if S[m][n]=="#":
      V[m][n]="#"
    if m>0 and n>0 and S[m-1][n-1]=="#":
      if S[m][n]=="#":
        V[m][n]="#"
      else:
        V[m][n] += 1
    if m>0 and S[m-1][n]=="#":
      if S[m][n]=="#":
        V[m][n]="#"
      else:
        V[m][n] += 1
    if W-1>n and m>0 and S[m-1][n+1]=="#":
      if S[m][n]=="#":
        V[m][n]="#"
      else:
        V[m][n] += 1
    if n>0 and S[m][n-1]=="#":
      if S[m][n]=="#":
        V[m][n]="#"
      else:
        V[m][n] += 1
    if W-1>n and S[m][n+1]=="#":
      if S[m][n]=="#":
        V[m][n]="#"
      else:
        V[m][n] += 1
    if H-1>m and n>0 and S[m+1][n-1]=="#":
      if S[m][n]=="#":
        V[m][n]="#"
      else:
        V[m][n] += 1
    if H-1>m and S[m+1][n]=="#":
      if S[m][n]=="#":
        V[m][n]="#"
      else:
        V[m][n] += 1
    if W-1>n and H-1>m and S[m+1][n+1]=="#":
      if S[m][n]=="#":
        V[m][n]="#"
      else:
        V[m][n] += 1
a=[]
for k in range(H):
  for l in range(W):
    a.append(V[k][l])
  print("".join(map(str,a)))
  a=[]

Submission status: Accepted

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

l = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]

for i in range(H):
    for j in range(W):
        c = 0
        if S[i][j] == "#":
            continue
        for k in range(8):
            x = j + l[k][0]
            y = i + l[k][1]
            if x < 0 or x >= W or y < 0 or y >= H:
                continue
            #print(x,y,S[y][x])
            if S[y][x] == '#':
                c += 1
        S[i][j] = str(c)
        
for i in range(H):
    print(''.join(S[i]))

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[[0 for i in range(W)] for j in range(H)]
for i in range(H):
  a=list(map(str,input()))
  for j in range(W):
    S[i][j]=a[j]
  a=[]
#print(S)
V=[[0 for i in range(W)] for j in range(H)]
for m in range(H):
  for n in range(W):
    if S[m][n]=="#":
      V[m][n]="#"
      continue
      
    if m > 0 and n > 0 and S[m-1][n-1]=="#":
      V[m][n] += 1
    if m > 0 and S[m-1][n]=="#":
      V[m][n] += 1
    if m > 0 and n < W - 1 and S[m-1][n+1]=="#":
      V[m][n] += 1
    if n > 0 and S[m][n-1]=="#":
      V[m][n] += 1
    if n < W - 1 and S[m][n+1]=="#":
      V[m][n] += 1
    if m < H - 1 and n > 0 and S[m+1][n-1]=="#":
      V[m][n] += 1
    if m < H - 1 and S[m+1][n]=="#":
      V[m][n] += 1
    if m < H - 1 and n < W - 1 and S[m+1][n+1]=="#":
      V[m][n] += 1

a=[]
#print(V)
for k in range(H):
  for l in range(W):
    a.append(V[k][l])
  print("".join(map(str,a)))
  a=[]

Submission status: Accepted

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

s = []

for _ in range(h):
    temp = []
    for i in input():
        if i == '#':
            temp.append(-100)
        else:
            temp.append(0)
    temp = [0] + temp + [0]
    s.append(temp)

s = [[0] * (w+2)] + s + [[0] * (w+2)]

for i in range(h+2):
    for j in range(w+2):
        if s[i][j] < 0:
            s[i-1][j-1] += 1
            s[i][j-1] += 1
            s[i+1][j-1] += 1
            s[i-1][j] += 1
            s[i+1][j] += 1
            s[i-1][j+1] += 1
            s[i][j+1] += 1
            s[i+1][j+1] += 1

s = s[1:-1]

ans = []

for i in s:
    temp = ''
    for j in i[1:-1]:
        if j < 0:
            temp += "#"
        else:
            temp += str(j)
    ans.append([temp])

for i in ans:
    print(*i)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[[0]*(w+2)]
for i in range(h):
  t=[0]
  u=input()
  for j in range(w):
    t.append(u[j])
  t.append(0)
  s.append(t)
s.append([0]*(w+2))

for i in range(1,h+1):
  v=[]
  for j in range(1,w+1):
    x=[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]]
    if s[i][j]=='#':
      v.append('#')
    else:
      v.append(x.count('#'))
  
  print(*v, sep='')

Submission status: Accepted

Solution code:
import sys
import os

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

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

            if 0 <= i-1 and i-1 <= H-1 and\
               0 <= j-1 and j-1 <= W-1 and S[i-1][j-1] == "#":
                    cnt +=1
            if 0 <= i+1 and i+1 <= H-1 and\
               0 <= j-1 and j-1 <= W-1 and S[i+1][j-1] == "#":
                    cnt +=1
            if 0 <= i-1 and i-1 <= H-1 and\
               0 <= j+1 and j+1 <= W-1 and S[i-1][j+1] == "#":
                    cnt +=1
            if 0 <= i+1 and i+1 <= H-1 and\
               0 <= j+1 and j+1 <= W-1 and S[i+1][j+1] == "#":
                    cnt +=1
            print(cnt,end="")
            cnt = 0
        else:
            print("#",end="")
        if j == W-1:
            print()

Submission status: Accepted

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

h,w=(int(x) for x in input().split())

sl=[]

for _ in range(h):
    s = input()
    sl.append(s[:-1])

for i in range(h):
    for j in range(w):
        mc=0
        #print("in j:",i,j,":sl:",sl[i][j])
        if sl[i][j]!="#":
            for k in range(-1,2):
                #print("in k:",k)
                for l in range(-1,2):
                    if (i==0 and k==-1) or (j==0 and l==-1) or (i==h-1 and k==1) or (j==w-1 and l==1):
                        continue
                    #print("in l:",k,l,":sl:",sl[i+k][j+l])
                    if sl[i+k][j+l]=="#":
                        mc+=1
                        #print("mc",str(mc))
                if (i==0 and k==-1) or (j==0 and l==-1) or (i==h and k==1) or (i==w and l==1):
                        continue
        if j == 0:
            sl[i]=sl[i][j].replace('.',str(mc))+sl[i][j+1:]
        elif j == w-1:
            sl[i]=sl[i][:j]+sl[i][j].replace('.',str(mc))
        else:
            #print("else",sl[i][:j-1])
            sl[i]=sl[i][:j]+sl[i][j].replace('.',str(mc))+sl[i][j+1:]
        #print("replaced:",sl[i],"sl:",sl)

#print(sl)

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

Submission status: Accepted

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

for i in range(1,h+1):
    for j in range(1,w+1):
        if m[i][j]=='.':
            c=0
            for a in [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]:
                if m[i+a[0]][j+a[1]]=='#':
                    c+=1
            m[i][j]=str(c)
for i in range(1,h+1):
    ans=''
    for j in range(1,w+1):
        ans+=m[i][j]
    print(ans)

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
m = [input() for _ in range(H)]
ans_m=[[0]*W for _ in range(H)]
dhw = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
def main():
  global ans_m
  for h in range(H):
    for w in range(W):
      if m[h][w]=="#":
        ans_m[h][w]="#"
      else:
        n_mines=0
        for dh, dw in dhw:
          nh, nw = h+dh, w+dw
          if 0<=nh<H and 0<=nw<W:
            if m[nh][nw]=="#":
              n_mines+=1
          ans_m[h][w]=str(n_mines)
  for _ in range(H):
    print("".join(ans_m[_]))
  return

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
# ABC075 B - Minesweeper
H,W = map(int,input().split())

G = [['' for _ in range(W+2)] for _ in range(H+2)]
Ans = [[0 for _ in range(W+2)] for _ in range(H+2)]

for i in range(1,H+1):
    S = input()
    for j,s in enumerate(S):
        G[i][j+1]= s

for i in range(1,H+1):
    for j in range(W):
        # 1行目
        if i == 1:
            if G[i][j+2] == '#':
                Ans[i][j+1] += 1
            # if G[i][j+1] == '#':
            #     Ans[i][j+1] = '#' 
            if G[i][j] == '#':
                Ans[i][j+1] += 1
            if G[i+1][j+2] == '#':
                Ans[i][j+1] += 1
            if G[i+1][j+1] == '#':
                Ans[i][j+1] += 1
            if G[i+1][j] == '#':
                Ans[i][j+1] += 1
            continue
        # 最終行
        if i == H:
            if G[i][j+2] == '#':
                Ans[i][j+1] += 1
            # if G[i][j+1] == '#':
            #     Ans[i][j+1] = '#' 
            if G[i][j] == '#':
                Ans[i][j+1] += 1
            if G[i-1][j+2] == '#':
                Ans[i][j+1] += 1
            if G[i-1][j+1] == '#':
                Ans[i][j+1] += 1
            if G[i-1][j] == '#':
                Ans[i][j+1] += 1
            continue

        if G[i][j+2] == '#':
            Ans[i][j+1] += 1
        # if G[i][j+1] == '#':
        #     Ans[i][j+1] = '#' 
        if G[i][j] == '#':
            Ans[i][j+1] += 1
        if G[i+1][j+2] == '#':
            Ans[i][j+1] += 1
        if G[i+1][j+1] == '#':
            Ans[i][j+1] += 1
        if G[i+1][j] == '#':
            Ans[i][j+1] += 1 
        if G[i-1][j+2] == '#':
            Ans[i][j+1] += 1
        if G[i-1][j+1] == '#':
            Ans[i][j+1] += 1
        if G[i-1][j] == '#':
            Ans[i][j+1] += 1

for i in range(1,H+1):
    for j in range(1,W+1):
        Ans[i][j] = str(Ans[i][j])
        if G[i][j] == '#':
            Ans[i][j] = '#'

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [["." for _ in range(W+2)]]
for _ in range(H):
    row = list(input())
    row.insert(0,".")
    row.append(".")
    S.append(row)
S.append(["." for _ in range(W+2)])
A = [["#" 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]==".":
            cnt = 0
            if S[i][j+1]=="#":
                cnt += 1
            if S[i-1][j+1]=="#":
                cnt += 1
            if S[i-1][j]=="#":
                cnt += 1
            if S[i-1][j-1]=="#":
                cnt += 1
            if S[i][j-1]=="#":
                cnt += 1
            if S[i+1][j-1]=="#":
                cnt += 1
            if S[i+1][j] =="#":
                cnt += 1
            if S[i+1][j+1]=="#":
                cnt += 1
            A[i-1][j-1] = str(cnt)
for i in range(H):
    print("".join(A[i]))

Submission status: Accepted

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

for h in range(H):
  s=list(input())
  li.append(s)

for h in range(H):
  for w in range(W):
    if li[h][w]=='.':
      li[h][w]=0

for h in range(H):
  for w in range(W):
    if li[h][w]=='#':
      if h!=0 and li[h-1][w]!='#':
        li[h-1][w]+=1
      if h!=H-1 and li[h+1][w]!='#':
        li[h+1][w]+=1
      if w!=0 and li[h][w-1]!='#':
        li[h][w-1]+=1
      if w!=W-1 and li[h][w+1]!='#':
        li[h][w+1]+=1
      if h!=0 and w!=0 and li[h-1][w-1]!='#':
        li[h-1][w-1]+=1
      if h!=0 and w!=W-1 and li[h-1][w+1]!='#':
        li[h-1][w+1]+=1
      if h!=H-1 and w!=0 and li[h+1][w-1]!='#':
        li[h+1][w-1]+=1
      if h!=H-1 and w!=W-1 and li[h+1][w+1]!='#':
        li[h+1][w+1]+=1

for h in range(H):
  for w in range(W):
    if li[h][w]!='#':
      li[h][w]=str(li[h][w])

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

Submission status: Accepted

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

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


def dfs(x, y):
    s = S[x][y]
    if s == '#':
        ans[x][y] = '#'
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < h and 0 <= ny < w and S[nx][ny] != '#':
                    ans[nx][ny] += 1


for i in range(h):
    for j in range(w):
        dfs(i, j)

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
m = [[0]*(w+2)]
for _ in range(h):
    m.append([0]+[0 if s == '.' else s for s in input()]+[0])
m.append([0]*(w+2))
around = [(i, j)for i in range(-1, 2) for j in range(-1, 2)]
for i in range(1, h+1):
    for j in range(1, w+1):
        if m[i][j] == '#':
            for x, y in around:
                if m[i+x][j+y] != '#':
                    m[i+x][j+y] += 1

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    h, w = list(map(int, input().rstrip('\n').split()))
    s = [list(str(input().rstrip('\n'))) for _ in range(h)]
    t = [[0] * w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                tq = [[i-1, j-1], [i-1, j], [i-1, j+1], [i, j-1], [i, j+1], [i+1, j-1], [i+1, j], [i+1, j+1]]
                for k, l in tq:
                    if 0 <= k < h and 0 <= l < w:
                        if s[k][l] != "#":
                            t[k][l] += 1
    for i in range(h):
        sp = []
        for j in range(w):
            if s[i][j] != "#":
                sp.append(str(t[i][j]))
            else:
                sp.append("#")
        print("".join(sp))


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input()for _ in range(h)]
x=[0,-1,-1,-1,0,1,1,1]
y=[1,1,0,-1,-1,-1,0,1]
for i in range(h):
    z=[]
    for j in range(w):
        if s[i][j]=='.':
            c=0
            for k in range(8):
                if 0<=i+x[k]<=h-1 and 0<=j+y[k]<=w-1:
                    if s[i+x[k]][j+y[k]]=='#':
                        c+=1
            z.append(c)
        else:
            z.append('#')
    print(*z,sep='')

Submission status: Accepted

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

S=[input() for _ in range(H)]

ans =[]

for i in range(H):
    s = ''
    for j in range(W):
        if S[i][j] != '#':
            cnt = 0
            for k in range(-1, 2):
                for m in range(-1, 2):
                    col = i + k
                    row = j + m
                    if (col < 0 or row < 0 or col > H-1 or row > W-1) or (k == 0 and m == 0):
                        continue
                    else:
                        if S[col][row] == '#':
                            cnt += 1
            s += str(cnt)
        else:
            s += '#'
    ans.append(s)

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [[] for _ in range(h+2)]
s[0] = ["." for _ in range(w+2)]
s[h+1] = ["." for _ in range(w+2)]
for i in range(1, h+1):
    s[i].append(".")
    line = list(input())
    for j in range(w):
        s[i].append(line[j])
    s[i].append(".")

def bomb(x,y):
    if s[y][x] == "#":
        return 1
    else:
        return 0

for y in range(1,h+1):
    for x in range(1,w+1):
        if s[y][x] == ".":
            s[y][x] = bomb(x-1,y-1) + bomb(x,y-1) + bomb(x+1,y-1) + bomb(x-1,y) + bomb(x+1,y) + bomb(x-1,y+1) + bomb(x,y+1) + bomb(x+1,y+1)

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
C=[]
for h in range(H):
    S=input()
    C.append([0 if s=='.' else s for s in S])
#print(C)
for h in range(H):
    for w in range(W):
        if C[h][w]=='#':
            down=False if h+1>=H else True
            up=False if h-1<0 else True
            left=False if w-1<0 else True
            right=False if w+1>=W else True
            if up:
                if C[h-1][w]!="#":
                    C[h-1][w]+=1
                if left:
                    if C[h-1][w-1]!="#":
                        C[h-1][w-1]+=1
                if right:
                    if C[h-1][w+1]!="#":
                        C[h-1][w+1]+=1
            if down:
                if C[h+1][w]!="#":
                    C[h+1][w]+=1
                if left:
                    if C[h+1][w-1]!="#":
                        C[h+1][w-1]+=1
                if right:
                    if C[h+1][w+1]!="#":
                        C[h+1][w+1]+=1
            if left:
                if C[h][w-1]!="#":
                    C[h][w-1]+=1
            if right:
                if C[h][w+1]!="#":
                    C[h][w+1]+=1
        else:
            continue
#print(C)
for c in C:
    for n in c:
        print(n,end="")
    print()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = ['.'*(w+2)]+['.'+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]=='.':
            mine = 0
            for k in range(-1,2):
                for l in range(-1,2):
                    if S[i+k][j+l]=='#' and not k==l==0:
                        mine += 1
            S[i] = S[i][:j]+str(mine)+S[i][j+1:]
for s in range(1,h+1):
    print(S[s][1:w+1])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
list1=[[0 for i in range(w)] for j in range(h)]
list2=[[0 for i in range(w)] for j in range(h)]
dx=[-1,-1,-1,0,0,1,1,1]
dy=[-1,0,1,-1,1,-1,0,1]

for j in range(h):
    s=input()
    for i in range(w):
        list1[j][i]=s[i]
        
for j in range(h):
    for i in range(w):
        if list1[j][i] == '#':
            for k in range(8):
                xk=i+dx[k]
                yk=j+dy[k]
                if xk >=0 and xk < w and yk >= 0 and yk < h:
                    list2[yk][xk] +=1
                    
for j in range(h):
    for i in range(w):
        if list1[j][i] == '#':
            list2[j][i] ='#'
        else:
            list2[j][i]=str(list2[j][i])

for j in range(h):
    print("".join(list2[j]))

Submission status: Accepted

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


def get_or_period(l, passed_x, passed_y):
    if passed_x in [-1, w]:
        return '.'
    if passed_y in [-1, h]:
        return '.'
    return l[passed_y][passed_x]


convertedMatrix = []

for y, row in enumerate(matrix):
    convertedMatrix.append([])
    for x, cell in enumerate(row):
        convertedCell = 0
        if matrix[y][x] == "#":
            convertedCell = "#"
            convertedMatrix[y].append(convertedCell)
            continue
        for exam_x in [-1, 0, 1]:
            for exam_y in [-1, 0, 1]:
                if get_or_period(matrix, x + exam_x, y + exam_y) == "#":
                    convertedCell += 1
        convertedMatrix[y].append(convertedCell)

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s_list = [list(input()) for _ in range(h)]
dx_list = [1, 1, 0, -1, -1, -1, 0, 1]
dy_list = [0, 1, 1, 1, 0, -1, -1, -1]
for i in range(h):
    for j in range(w):
        if s_list[i][j] == '#':
            continue
        num = 0
        for dx, dy in zip(dx_list, dy_list):
            x = j + dx
            y = i + dy
            if x < 0 or x >= w:
                continue
            if y < 0 or y >= h:
                continue
            if s_list[y][x] == '#':
                num += 1
        s_list[i][j] = str(num)
    answer = ''.join(s_list[i])
    print(answer)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
li = []
for i in range(h):
    li.append(list(input()))
#print(li)
for i in range(h):
    for j in range(w):
        if li[i][j] == "#":
            continue
        cnt = 0
        for kk in (-1,0,1):
            for ll in (-1,0,1):
                if not 0 <= i+kk < h or not 0<= j+ll < w:
                    continue
                if li[i+kk][j+ll] == "#":
                    cnt += 1
        li[i][j] = str(cnt)
for i in range(h):
    print("".join(li[i]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
li,li2,li3 = [],[],[]
for i in range(h):
    li.append((input()))
li2.append("."*(w+2))
for i in range(h):
    li2.append("."+li[i]+".")
li2.append("."*(w+2))
cnt = 0
ans = [[0 for x in range(w)] for y in range(h)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if li2[i][j] == ".":
            if li2[i-1][j-1] == "#":
                ans[i-1][j-1] += 1
            if li2[i-1][j] == "#":
                ans[i-1][j-1] += 1
            if li2[i-1][j+1] == "#":
                ans[i-1][j-1] += 1
            if li2[i][j-1] == "#":
                ans[i-1][j-1] += 1
            if li2[i][j+1] == "#":
                ans[i-1][j-1] += 1
            if li2[i+1][j-1] == "#":
                ans[i-1][j-1] += 1
            if li2[i+1][j] == "#":
                ans[i-1][j-1] += 1
            if li2[i+1][j+1] == "#":
                ans[i-1][j-1] += 1
        elif li2[i][j] == "#":
            ans[i-1][j-1] = "#"
for i in range(h):
    ans[i] = [str(x) for x in ans[i]]
for i in range(h):
    print("".join(ans[i]))