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())
sList = []
xList = [1, 1, 0, -1, -1, -1, 0, 1]
yList = [0, 1, 1, 1, 0, -1, -1, -1]

for i in range(H):
    sList.append(input())

for i in range(H):
    tmplist = list(sList[i])
    for j in range(W):
        if sList[i][j] == '#':
            continue

        tmpCnt = 0
        for k in range(8):
            tmpi = i + yList[k]
            tmpj = j + xList[k]
            if tmpi < 0 or tmpi >= H:
                continue
            if tmpj < 0 or tmpj >= W:
                continue
            if sList[tmpi][tmpj] == '#':
                tmpCnt += 1

        tmplist[j] = str(tmpCnt)
    print(''.join(tmplist))

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(H):
    for j in range(W):
        c=S[i+1][j+1]
        if c==".":
            c=sum([S[k][j:j+3].count("#") for k in range(i,i+3)])
        print(c,sep="",end="")
    print("")

Submission status: Accepted

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

M = [list("." + input() + ".") for i in range(H)]
M.insert(0, list(("."*(W+2))))
M.append(list("."*(W+2)))

cnt = 0
for i in range(1, H+1):
    for j in range(1, W+1):
        if M[i][j] == ".":
            for k in range(-1, 2):
                if M[i+k][j-1] == "#":
                    cnt += 1
                if M[i+k][j] == "#":
                    cnt += 1
                if M[i+k][j+1] == "#":
                    cnt += 1
            print(cnt, end = "")
            cnt = 0
        else:
            print("#", end = "")
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
fields = [['.'] * (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 fields[i][j] == '#':
      continue
    else:
      mine_cnt = 0
      for row in [-1, 0, 1]:
        for column in [-1, 0, 1]:
          if fields[i+row][j+column] == '#':
            mine_cnt += 1
      fields[i][j] = str(mine_cnt)
for i in range(1 , H+1):
  row = ''.join(fields[i][1:W+1])
  print(row)

Submission status: Accepted

Solution code:
# いつもの探索でいいです 終了

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

for i in range(h):
  for j in range(w):
    if table[i][j] != "#":
      sub_h = [0] if h==1 else [-1, 0, 1] if 0<i<h-1 else [0, 1] if i==0 else [-1, 0]
      sub_w = [0] if w==1 else [-1, 0, 1] if 0<j<w-1 else [0, 1] if j==0 else [-1, 0]
      count = 0
      for sh in sub_h:
        for sw in sub_w:
          if sh == sw == 0:
            continue
          if table[i+sh][j+sw] == "#":
            count += 1
      table[i][j] = str(count)

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
fld = []
mp = [[0] * W for i in range(H)]
dd = ((-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1))

for i in range(H):
  fld.append(input())

for i in range(W):
  for j in range(H):
    if fld[j][i] == "#":
      mp[j][i] = "#"
      for d in dd:
        if 0 <= j+d[0] <= H-1 and 0 <= i+d[1] <= W-1:
          if fld[j+d[0]][i+d[1]] != "#":
            mp[j+d[0]][i+d[1]] += 1

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h):
    s.append(list(input()))
ans = []
for i in range(h):
    temp = []
    for j in range(w):
        if s[i][j] == "#":
            temp.append("#")
        else:
            count = 0
            for k in range(3):
                for l in range(3):
                    if 0 <= i + k - 1 <= h - 1 and 0 <= j + l - 1 <= w - 1:
                        if k == 1 and l == 1:
                            pass
                        else:
                            if s[i + k - 1][j + l - 1] == "#":
                                count += 1
            temp.append(str(count))
    ans.append(temp)
for i in range(h):
    print("".join(ans[i]))

Submission status: Accepted

Solution code:
import itertools
H,W = map(int,input().split())
S = [input() for _ in range(H)]
a = [[0 if cell=='.' else '#' for cell in row] for row in S]

for i in range(H):
  for j in range(W):
    if S[i][j] != '#':
      continue
    for dx,dy in itertools.product([-1,0,1],repeat=2):
      x = i+dx
      y = j+dy
      if x < 0 or x >= H:
        continue
      if y < 0 or y >= W:
        continue
      if S[x][y] == '#':
        continue
      a[x][y] += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

ans = [[0 for i in range(W+1)] for j in range(H+1)]


S = []
for i in range(H):
    raw = input()
    S.append(raw)

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


for i in range(H):
    for j in range(W):
        print(str(ans[i][j]), end="")
    print("")

Submission status: Accepted

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

ans_array = [[0] * (W + 2) for i in range(H + 2)]
bomb_array = [[0] * W for i in range(H)]

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

            ans_array[i - 1][j - 1] += 1
            ans_array[i - 1][j] += 1
            ans_array[i - 1][j + 1] += 1

            ans_array[i + 1][j - 1] += 1
            ans_array[i + 1][j] += 1
            ans_array[i + 1][j + 1] += 1

            ans_array[i][j - 1] += 1
            ans_array[i][j + 1] += 1

            bomb_array[i - 1][j - 1] = 1
    
ans_line = ''
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if bomb_array[i - 1][j - 1] == 1:
            ans_line += '#'
        else:
            ans_line += str(ans_array[i][j])
    print(ans_line)
    ans_line = ''

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

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

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list("."*(w+2))]
for _ in range(h):
    s+=[list("."+input()+".")]
s+=[list("."*(w+2))]
ans=[list("#"*w) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i+1][j+1]!="#":
            c=0
            for k in range(3):
                for l in range(3):
                    if s[i+k][j+l]=="#":
                        c+=1
            ans[i][j]=str(c)
for m in range(h):
    answer=""
    answer=''.join(ans[m])
    print(answer)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = [list(input()) for _ in range(a)]
d = [[0] * b for _ in range(a)]

for i in range(len(c)):
    for j in range(len(c[i])):
        if c[i][j] == "#":
            for k in range(max(0, i - 1), min(a, i + 2)):
                for l in range(max(0, j - 1), min(b, j + 2)):
                    if c[k][l] != "#":
                        d[k][l] += 1
for i in range(a):
    e = ""
    for j in range(b):
        if c[i][j] == "#":
            e += c[i][j]
        else:
            e += str(d[i][j])
    print(e)

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 k in [-1,0,1]:
            if i+k<0 or H<=i+k:
                continue
                #iが端の時通過
            for l in [-1,0,1]:
                if j+l<0 or j+l>=W:
                    continue
                    #jが端の時通過
                cnt+=1*(S[i+k][j+l]=='#')
                #一個前一個上~一個下一個右までの#の数を足す
        S[i][j]=str(cnt)
        #見ていた数を足した数字に変更
for i in range(H):
    print("".join(S[i]))

Submission status: Accepted

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

def search(i,j,box_list = box_list):
  count = 0
  if j > 0 and box_list[i][j-1] == "#":
    count += 1
  if j < W-1 and box_list[i][j+1] == "#":
    count += 1
  if i > 0 and box_list[i-1][j] == "#":
    count += 1
  if i < H-1 and box_list[i+1][j] == "#":
    count += 1
  if j > 0 and i < H-1 and box_list[i+1][j-1] == "#":
    count += 1
  if j < W-1 and i < H-1 and box_list[i+1][j+1] == "#":
    count += 1
  if j > 0 and i > 0 and box_list[i-1][j-1] == "#":
    count += 1
  if j < W-1 and i > 0 and box_list[i-1][j+1] == "#":
    count += 1
  return count


for i in range(H):
  answer = ""
  for j in range(W):
    if box_list[i][j] != "#": 
      answer += str(search(i,j))
    else:
      answer += "#"
  print(answer)

Submission status: Accepted

Solution code:
def write(t):
    for i in range(len(t)):
        print(t[i])
x, y = map(int, input().split())
data = []
ans = []
around = 0
data.append("."*(y+2))
for i in range(x):
    a = input()
    data.append("."+a+".")
    ans.append("")
data.append("."*(y+2))
for i in range(1, x+1):
    for j in range(1, y+1):
        if data[i + 1][j + 1] == "#": around+=1
        if data[i + 1][j]     == "#": around += 1
        if data[i + 1][j - 1] == "#": around += 1
        if data[i]    [j - 1] == "#": around += 1
        if data[i - 1][j - 1] == "#": around += 1
        if data[i - 1][j]     == "#": around += 1
        if data[i - 1][j + 1] == "#": around += 1
        if data[i]    [j + 1] == "#": around += 1
        if data[i][j] == "#":ans[i-1]+="#"
        else: ans[i-1]+=str(around)
        around = 0
write(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
  
  h, w = map(int, input().split())
  s = []
  
  s.append('.' + '.' * w + '.')
  for i in range(h):
      s.append('.' + input() + '.')
  s.append('.' + '.' * w + '.')
  
  for i in range(h):
      result_list = []
      for j in range(w):
          target_cell = ''
          if s[i + 1][j + 1] == '.':
            target_cell += s[i][j:j+3]
            target_cell += s[i+1][j:j+3]
            target_cell += s[i+2][j:j+3]
              
            print(target_cell.count('#'), end="")
          else:
            print('#', end="")
            
      print()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
  
  h, w = map(int, input().split())
  s = []
  
  s.append('.' * w)
  for i in range(h):
      s.append('.' + input() + '.')
  s.append('.' * w)
  
  for i in range(h):
      result_list = []
      for j in range(w):
          target_cell = ''
          if s[i + 1][j + 1] == '.':
            target_cell += s[i][j:j+3]
            target_cell += s[i+1][j:j+3]
            target_cell += s[i+2][j:j+3]
              
            print(target_cell.count('#'), end="")
          else:
            print('#', end="")
            
      print()

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

h, w = MAP()

# a = [[0] * (w + 2)] * (h + 2)
s = [list(input()) for _ in range(h)]

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

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
            # スルー
        num = 0
        for d in range(8):
            ni = i + dy[d]
            nj = j + dx[d]
            # niとnjで有効なのはsの定義域．
            # ni w 0 <= ni <= w - 1
            # nj h 0 <= nj <= h - 1
            
            if ni < 0 or h <= ni:
                continue
                # スルー
            if nj < 0 or w <= nj:
                continue
                # スールー
            if s[ni][nj] == "#":
                num += 1
        s[i][j] = str(num)
            # (num + "0")

for i in range(h):
    print("".join(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
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
move = [(0, 1), (1, 1), (-1, 1), (0, -1), (1, -1), (-1, -1), (1, 0), (-1, 0)]
#A
def A():
    a, b, c = LI()
    if a == b:
        print(c)
    if b == c:
        print(a)
    if a == c:
        print(b)
    return

#B
def B():
    h, w = LI()
    s = SR(h)
    for y in range(h):
        for x in range(w):
            if s[y][x] == ".":
                b = 0
                for my, mx in move:
                    ym = my + y
                    xm = mx + x
                    if 0 <= ym < h and 0 <= xm < w:
                        if s[ym][xm] == "#":
                            b += 1
                s[y][x] = b
    for i in range(h):
        print("".join(map(str, s[i])))
    return

#C
def C():
    return

#D
def D():
    return

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

Submission status: Accepted

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

ans = []
for i in range(1, H + 1):
  s = str()
  for j in range(1, W + 1):
    tmp = 0
    if S[i][j] == '.':
      for k in range(-1, 2, 1):
        for l in range(-1, 2, 1):
          if S[i+k][j+l] == '#':
            tmp += 1
      s += str(tmp)
    else:
      s += '#'
  ans += [s]
    
for i in ans:
  print (i)

Submission status: Accepted

Solution code:
h,w =map(int,input().split())
s = [input() for i in range(h)]
dx = [1,1,1,0,0,-1,-1,-1]
dy = [1,-1,0,1,-1,1,-1,0]
s_out =["" for i in range(h)]
for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == "#":
                s_out[i] += "#"
                continue
        for k in range(8):
            x = i + dx[k]
            y = j + dy[k]
            
            if x >= 0 and x <= h-1 and y >= 0 and y<= w-1 and s[x][y]=="#":
                count += 1
        s_out[i] += str(count)
for i in s_out:
    print(i)

Submission status: Accepted

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

for i in range(h):
    s_out.append("")

def mine(a):
    count = 0
    for i in range(len(a)):
        if a[i] == "#":
            count += 1
    return count

if h == 1 and w == 1:
    if s[0][0] == "#":
        print("#")
        
    else:
        print("0")
        

elif h==1:
    for j in range(w):
        if s[0][j] == "#":
            s_out[0] += "#"
        elif j == 0:
            s_out[0] += str(mine(s[0][1]))
        elif j == w-1:
            s_out[0] += str(mine(s[i][j-1]))
        else:
            s_out[0] += str(mine(s[i][j-1])+mine(s[i][j+1]))
    print(s_out[0])
    
    
elif w == 1:
    for i in range(h):
        if s[i][0] == "#":
            s_out[i] += "#"
        elif i == 0:
            s_out[i] += str(mine(s[1][0]))
        elif i == h-1:
            s_out[i] += str(mine(s[i-1][0]))
        else:
            s_out[i] += str(mine(s[i+1][0])+mine(s[i-1][0]))
    for i in s_out:
        print(i)
    
else:
    for i in range(h):
            for j in range(w):
                if s[i][j] == "#":
                    s_out[i] += "#"
                elif i == 0 and j == 0:
                    s_out[i] += str(mine(s[0][1])+mine(s[1][0:2]))
                elif i == 0 and j == w-1:
                    s_out[i] += str(mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
                elif i == 0:
                    s_out[i] += str(mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
                elif i == h-1 and j == 0:
                    s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1]))
                elif i == h-1 and j == w-1:
                    s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1]))
                elif i == h-1:
                    s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1]))
                elif j == 0:
                    s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1])+mine(s[i+1][j:j+2]))
                elif j == w-1:
                    s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
                else:
                    s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
    for i in s_out:
        print(i)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[['$']*(W+2)]+list(['$']+list(input())+ ['$'] for _ in range(H)) +[['$']*(W+2)]
ans=list([0]*W for _ in range(H))
c=0
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=='.':
            c=0
            for x in [-1,1,0]:
                for y in [-1,1,0]:
                    if S[i+x][j+y]=='#':
                        c+=1
            ans[i-1][j-1]=c
        else:
            ans[i-1][j-1]=S[i][j]
for p in range(H):
    for q in range(W):
        print(ans[p][q],end='')
    else:
        print('')

Submission status: Accepted

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

def search(x,y):
    c = 0
    if x > 0:
        if l[y][x-1] == "#":
            c += 1
        if y > 0:
            if l[y-1][x-1] == "#":
                c += 1
        if y < h-1:
            if l[y+1][x-1] == "#":
                c += 1
    if x < w-1:
        if l[y][x+1] == "#":
            c += 1
        if y > 0:
            if l[y-1][x+1] == "#":
                c += 1
        if y < h-1:
            if l[y+1][x+1] == "#":
                c += 1
    if y > 0:
        if l[y-1][x] == "#":
            c += 1
    if y < h-1:
        if l[y+1][x] == "#":
            c += 1
    return c

for x in range(w):
    for y in range(h):
        pos = l[y][x]
        if pos == ".":
            l[y][x] = search(x,y)
for i in range(h):
    for j in range(w):
        print(l[i][j],end="")
    print()

Submission status: Accepted

Solution code:
import numpy as np

def main():
    H, W = map(int, input().split())
    s = [input() for i in range(H)]
    
    diffs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for y in range(H):
        anstr = ''
        for x in range(W):
            if s[y][x] == '#':
                anstr += '#'
                continue
            count = 0
            for dx, dy in diffs:
                if (x + dx < 0) or (x + dx > W - 1) or (y + dy < 0) or (y + dy > H - 1):
                    continue
                if s[y + dy][x + dx] == '#':
                    count += 1
            anstr += str(count)
        print(anstr)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            c = 0
            for y, x in zip(DY, DX):
                y = i + y
                x = j + x
                if y < 0 or x < 0 or w - 1 < x or h - 1 < y:
                    continue
                if s[y][x] == '#':
                    c += 1
            s[i][j] = c

for i in s:
    print(*i, sep='')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input() for _ in [0]*h]
l=[]
for i in range(h):
    for j in range(w):
        if s[i][j]=='#':l.extend([[i,j]])
a=''
for i in range(h):
    for j in range(w):
        if [i,j] in l:a+='#'
        else:
            c=0
            for m in l:
                if abs(i+j-sum(m))+abs(i-j-m[0]+m[1])==2:c+=1
            a+=str(c)
for i in range(h):print(a[w*i:w*(i+1)])

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = []
for i in range(a):
    c.append(list(input().replace(".", "0")))
for i in range(a):
    for j in range(b):
        if c[i][j] == "#":
            for x in range(i-1, i+2):
                for y in range(j-1, j+2):
                    if 0 <= x <= a-1 and 0 <= y <= b-1 and c[x][y] != "#":
                        c[x][y] = str(int(c[x][y]) + 1)
for i in range(a):
    print("".join(c[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input() for _ in range(h)]
 
for i in range(h):
    ans=""
    for j in range(w):
        if(s[i][j]=="#"):
            ans+="#"
        else:
            tmp=0
            for k in s[max(0, i-1):min(h, i+2)]:
                tmp+=k[max(0, j-1):min(w, j+2)].count("#")
                
            ans+=str(tmp)
    print(ans)

Submission status: Accepted

Solution code:
# ABC075-B
H, W = map(int, input().split())
S = []
for i in range(H+2):
    if i == 0 or i == H+1:
        S.append('x' * (W+2))
    else:
        S.append('x' + input() + 'x')
        
for i in range(1, H+1):
    ans_tmp_str = ''
    for k in range(1, W+1):
        ans_tmp = 0   # count of bomb
        # print(S[i][k])
        if S[i][k] == '.':
            if S[i - 1][k - 1] == '#':
                ans_tmp += 1
            if S[i - 1][k] == '#':
                ans_tmp += 1
            if S[i - 1][k + 1] == '#':
                ans_tmp += 1
            if S[i][k - 1] == '#':
                ans_tmp += 1
            if S[i][k + 1] == '#':
                ans_tmp += 1
            if S[i + 1][k - 1] == '#':
                ans_tmp += 1
            if S[i + 1][k] == '#':
                ans_tmp += 1
            if S[i + 1][k + 1] == '#':
                ans_tmp += 1
            ans_tmp_str += str(ans_tmp)
           # print(ans_tmp_str)
        elif S[i][k] == '#':
            ans_tmp_str += '#'
    print(ans_tmp_str)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
import sys
h,w = map(int, input().split())
s = [list(input().replace(".","0").replace("#","1")) for i in range(h)]
s = [list(map(int,s[i])) for i in range(h)]
ss = np.array(s)
s_new = np.zeros((h,w))
ans_arr = np.zeros((h,w))
def around(x,y):
  ans = 0
  if x > 0:
    ans += ss[x-1,y]
  if y > 0:
    ans += ss[x,y-1]
  if x > 0 and y > 0:
    ans += ss[x-1,y-1]
  if x<h-1:
    ans += ss[x+1,y]
  if y<w-1:
    ans += ss[x,y+1]
  if x<h-1 and y<w-1:
    ans += ss[x+1,y+1]
  if x > 0 and y<w-1:
    ans += ss[x-1,y+1]
  if x<h-1 and y>0:
    ans += ss[x+1,y-1]
  return ans

for i in range(h):
  for j in range(w):
    if ss[i,j] == 0:
      s_new[i,j] = around(i,j)
    else:
      s_new[i,j] = -1

sss = s_new.tolist()
for i in range(h):
  sss[i] = list(map(str, sss[i]))
for i in range(h):
  for j in range(w):
    if float(sss[i][j]) > -1:
      sss[i][j] = sss[i][j][:1]
    else:
      sss[i][j] = "#"
  print(''.join(sss[i]))

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#': continue
        cnt = 0
        if H > i - 1 >= 0 and W > j - 1 >= 0:
            if matrix[i - 1][j - 1] == '#':
                cnt += 1
        if H > i - 1 >= 0 and W > j >= 0:
            if matrix[i - 1][j] == '#':
                cnt += 1
        if H > i - 1 >= 0 and W > j + 1 >= 0:
            if matrix[i - 1][j + 1] == '#':
                cnt += 1
        if H > i >= 0 and W > j - 1 >= 0:
            if matrix[i][j - 1] == '#':
                cnt += 1
        if H > i >= 0 and W > j + 1 >= 0:
            if matrix[i][j + 1] == '#':
                cnt += 1
        if H > i + 1 >= 0 and W > j - 1 >= 0:
            if matrix[i + 1][j - 1] == '#':
                cnt += 1
        if H > i + 1 >= 0 and W > j >= 0:
            if matrix[i + 1][j] == '#':
                cnt += 1
        if H > i + 1 >= 0 and W > j + 1 >= 0:
            if matrix[i + 1][j + 1] == '#':
                cnt += 1
        matrix[i][j] = cnt

for h in range(H):
    r = ''
    for w in range(W):
        r += str(matrix[h][w])
    print(r)

Submission status: Accepted

Solution code:
def get(i, j, S, H, W):
    if 0 <= i < H and 0 <= j < W:
        return int(S[i][j] == "#")
    else:
        return 0


def f(i, j, S, H, W):
    indices = [
        (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),
    ]
    return sum([get(i, j, S, H, W) for i, j in indices])


H, W = map(int, input().split())
S = [input() for _ in range(H)]
M = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            M[i][j] = f(i, j, S, H, W)
        else:
            M[i][j] = "#"
for row in M:
    print(*row, sep="")

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
c=[str(input()) for i in range(a)]
for i in range(a):
  c[i]="."+c[i]+"."
c=["."*(b+2)]+c +["."*(b+2)]
d=[["0"for i in range(b)]for j in range(a)]
for i in range(1,a+1):
  for j in range(1,b+1):
    f=0
    for k in range(-1,2):
      for l in range(-1,2):
        if c[i+k][j+l]=="#":
          f+=1
    d[i-1][j-1]=str(f)
for i in range(1,a+1):
  for j in range(1,b+1):
    if c[i][j]=="#":
      d[i-1][j-1]="#"
for i in range(a):
  dd=""
  for j in range(b):
    dd+=str(d[i][j])
  print(dd)