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:
# -*- coding: utf-8 -*-
# 整数の入力
h, w = map(int, input().split())
s = [""]*h

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

# 探索対象になっている周囲のマス目との添字をあらかじめ記録する
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            continue
        else:
            count = 0
            for d in range(len(dx)):
                x = j + dx[d]
                y = i + dy[d]

                if not 0 <= x < w:
                    continue
                if not 0 <= y < h:
                    continue
                if s[y][x] == "#":
                    count += 1
            s[i][j] = str(count)

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

Submission status: Accepted

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

news = []
for y in range(h):
    tmp = ''
    for x in range(w):
        cnt = 0
        if s[y][x] == '#':
            tmp += '#'
            continue
        for dy in range(-1, 2):
            for dx in range(-1, 2):
                if dx == 0 and dy == 0:
                    continue
                nx = dx + x
                ny = dy + y
                if not (0 <= nx < w and 0 <= ny < h):
                    continue
                if s[ny][nx] == '#':
                    cnt += 1
        tmp += str(cnt)
    news.append(tmp)
for i in range(h):
    print(news[i])

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = []
for i in range(H):
  S.append(list(input()))
  
if H == 1:
  if W == 1:
    if S[0][0] == '.':
      S[0][0] = str(0)
    print("".join(S[0]))
    exit()
  if W == 2:
    if S[0][0] == '.' and S[0][1] == '.':
      S[0][0] = str(0)
      S[0][1] = str(0)
    elif S[0][0] == '.' and S[0][1] == '#':
      S[0][0] = str(1)
    elif S[0][0] == '#' and S[0][1] == '.':
      S[0][1] = str(1)
    else:
      pass
    print("".join(S[0]))
    exit()
  
  if W > 2:
    for i in range(W):
      if S[0][i] == '.':
        if i == 0:
          S[0][i] = str(int(S[0][i+1] == '#'))
        elif 0 < i < W-1:
          S[0][i] = str(int(S[0][i-1] == '#') + int(S[0][i+1] == '#'))
        elif i == W-1:
          S[0][i] = str(int(S[0][i-1] == '#'))

    print("".join(S[0]))
    exit()
    
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      if i == 0:
        if j == 0:
          cnt = S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif 0<i<H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#')+S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#')+ S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif i == H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#') + S[i][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#') + S[i][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#')
          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())
s = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#": ans = "#"
        else:
            ans = 0
            for k in [-1, 0, 1]:
                for l in [-1, 0, 1]:
                    if k == l == 0: continue
                    if 0 <= i+k < h and 0 <= j+l < w and s[i+k][j+l] == "#":
                        ans += 1
        print(ans, sep="", end="")
    print("")

Submission status: Accepted

Solution code:
tmp = [int(i) for i in input().split()]
H, W = tmp[0], tmp[1]

matrix = [[0 for i in range(W+2)] for j in range(H+2)]
real = [input().split() for _ in range(H)]
tmp = []

for rs in real:
  for r in rs:
    tmp.append([1 if l == '#' else 0 for l in r])

for i in range(H):
    for j in range(W):
        matrix[i+1][j+1] = tmp[i][j]
tmp = [[0 for i in range(W)] for j in range(H)]

for i in range(1, H+1):
    for j in range(1, W+1):
        if not matrix[i][j] == 1:
            tmp[i-1][j-1] = (sum(matrix[i-1][j-1:j+2]) + matrix[i][j-1] + matrix[i][j+1] + matrix[i+1][j] + matrix[i+1][j-1] + matrix[i+1][j+1])
for i in range(1, H+1):
    for j in range(1, W+1):
        if matrix[i][j] == 1:
            tmp[i-1][j-1] = '#'
        else:
            tmp[i-1][j-1] = str(tmp[i-1][j-1])
for t in tmp:
    mojiretsu = ""
    for i in t:
        mojiretsu += i
    print(mojiretsu)

Submission status: Accepted

Solution code:
HandW = input().split()
h = HandW[0]
w = HandW[1]

list1 = [["." for i in range(int(w)+2)] for j in range(int(h)+2)]

for i in range(int(h)):
    str1 = input()
    for j in range(int(w)):
        list1[i+1][j+1] = str1[j]

for i in range(int(h)):
    for j in range(int(w)):
        if list1[i+1][j+1] != '#':
            #count = [l1[j:j+3] for l1 in list1[i:i+3]].count('#') 0になる
            count = sum(l[j:j+3].count('#') for l in list1[i:i+3])
            list1[i+1][j+1] = count

#print([l[1:int(w)+1] for l in list1[1:int(h)+1]])

for i in range(1,int(h) + 1):
    for j in range(1,int(w) + 1):
        print(list1[i][j],end = '')
    print('')

Submission status: Accepted

Solution code:
# ABC 75 B

def resolve():
    H, W = map(int, input().split())
    s = [[0] * W for _ in range(H)]
    for i in range(H):
        a = input()
        for j in range(W):
            s[i][j] = a[j]

    # 与えられた座標が範囲内でかつ爆弾なら True を返す
    def check(h, w):
        if (h < 0 or w < 0 or h >= H or w >= W):
            return False
        if (s[h][w] == '#'):
            return True
        return False

    for h in range(H):
        for w in range(W):
            if s[h][w] == '#':
                continue
            mines = 0
            for i in [-1, 0, 1]:
                for j in [-1, 0, 1]:
                    if check(h+i, w+j):
                        mines += 1
            s[h][w] = mines

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
d = [[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):
        if S[i][j] == ".":
            cnt = 0
            for x in d:
                if 0 <= i+x[0] < H and 0 <= j+x[1] < W and S[i+x[0]][j+x[1]] == "#":
                    cnt += 1
            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())
s=[]*h
b=[[0]*w]*h

for i in range(h):
    b[i]=list(input())
       
for y in range(h):
    for x in range(w):
        if b[y][x]==".":
            b_no=0
            for dy in range(-1,2):
                for dx in range(-1,2):
                    if 0 <= y+dy and y+dy < h and 0 <= x+dx and x+dx <w: 
                        if b[y+dy][x+dx]=="#":
                            b_no += 1
            b[y][x]=b_no
for y in range(h):
    for l in b[y]:
        print(l,end="")
    print()

Submission status: Accepted

Solution code:
# ABC075B - Minesweeper
import sys
input = sys.stdin.readline

def check_neighbors(i: int, j: int) -> int:
    # count the number of landmines adjacent to S[i][j]
    ret = 0
    for x, y in neighbors:
        a, b = i + x, j + y
        if 0 <= a < H and 0 <= b < W:
            ret += 1 if S[a][b] == "#" else 0
    return ret


def main():
    global H, W, S, neighbors
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    neighbors = [(-1, -1), (-1, 0), (-1, 1), (0, -1)]
    neighbors += [(0, 1), (1, -1), (1, 0), (1, 1)]
    A = [""] * H
    for i, s in enumerate(S):
        for j, a in enumerate(s):
            if a == "#":
                A[i] += "#"
            else:
                cnt = check_neighbors(i, j)
                A[i] += str(cnt)
    print(*A, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC075B - Minesweeper
import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    moves = [(-1, -1), (-1, 0), (-1, 1), (0, -1)]
    moves += [(0, 1), (1, -1), (1, 0), (1, 1)]
    A = [""] * H
    for i, s in enumerate(S):
        for j, a in enumerate(s):
            if a == "#":
                A[i] += "#"
            else:
                cnt = sum(
                    S[i + x][j + y] == "#"
                    for x, y in moves
                    if 0 <= i + x < H and 0 <= j + y < W
                )
                A[i] += str(cnt)
    print(*A, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline
h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]

ans=[[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=='#':
      ans[i-1][j-1]='#'
    else:
      cnt=(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]=="#")
      ans[i-1][j-1]=cnt

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]

ans=[[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=='#':
      ans[i-1][j-1]='#'
    else:
      cnt=(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]=="#")
      ans[i-1][j-1]=cnt

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

Submission status: Accepted

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

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

for i in range(1, h+1):
    for j in range(1, w+1):
        cnt = 0
        if s[i][j] == '.':
            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][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

            ans[i-1][j-1] = cnt
        else:
            ans[i-1][j-1] = '#'

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]
ans=[[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=="#":
      ans[i-1][j-1]="#"
    else:
      count=(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]=="#")
      ans[i-1][j-1]=count
for i in range(h):
  print(*ans[i],sep="")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
count = 0
X = []
p = []
X.append(["@"]*(w+2))
for _ in range(h):
  p = []
  x = input()
  for i in range(w):
    p.append(x[i])
  X.append(["@"]+p+["@"])  
X.append(["@"]*(w+2))
for i in range(1,h+1):
  for j in range(1,w+1):
    count=0
    if X[i][j]==".":
      if X[i-1][j-1]=="#":count+=1
      if X[i-1][j]=="#":count+=1
      if X[i-1][j+1]=="#":count+=1
      if X[i][j-1]=="#":count+=1
      if X[i][j+1]=="#":count+=1
      if X[i+1][j-1]=="#":count+=1
      if X[i+1][j]=="#":count+=1
      if X[i+1][j+1]=="#":count+=1
      count=str(count)
      X[i][j]=count
for k in range(1,h+1):
  print(*X[k][1:w+1],sep="")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [ [0] + [0 if a=='.' else 1 for a in input()] + [0] for _ in range(h)]
s = [[0 for _ in range(w+2)]] + s + [[0 for _ in range(w+2)]]

for j in range(1,h+1):
  ans = []
  for i in range(1,w+1):
    if s[j][i] == 0:
      ans.append(str(sum(s[j-1][i-1:i+2])+sum(s[j][i-1:i+2])+sum(s[j+1][i-1:i+2])))
    else:
      ans.append('#')
  print(''.join(ans))

Submission status: Accepted

Solution code:
import numpy as np
h,w=map(int,input().split())
a=np.array([list(input()) for _ in range(h)])
for i in range(h):
    for j in range(w):
        if a[i,j]!='#':
            a[i,j]=np.count_nonzero(a[max(i-1,0):min(i+2,h),max(j-1,0):min(j+2,w)]=='#')
for i in range(h):
    print(''.join(a[i,:]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
Ss = ["," * (W+2)]
Ss.extend(["," + input() + "," for _ in range(H)])
Ss.extend(["," * (W+2)])
# print(Ss)

def sweep(x, y):
  count = 0
  
  if Ss[x-1][y-1] == "#":
    count += 1
  if Ss[x-1][y] == "#":
    count += 1
  if Ss[x-1][y+1] == "#":
    count += 1
  if Ss[x][y-1] == "#":
    count += 1
  if Ss[x][y+1] == "#":
    count += 1
  if Ss[x+1][y-1] == "#":
    count += 1
  if Ss[x+1][y] == "#":
    count += 1
  if Ss[x+1][y+1] == "#":
    count += 1
  
  return count
  
  
for i in range(1, H+1):
  for j in range(1, W+1):
    if Ss[i][j] == "#":
      print("#", end="")
    else:
      print(sweep(i, j), end="")
  print("")

Submission status: Accepted

Solution code:
h,w = list(map(int, input().split()))
s = [ list(input()) for _ in range(h)]
res = [ [0] * w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if(s[i][j] == "#"):
      res[i][j] = "#"
      for k in [-1,0,1]:
        for l in [-1,0,1]:
          if(0 <= i + k and i + k < h):
            if(0 <= j + l and j + l < w):
              if(res[i+k][j+l] != "#"):
                res[i+k][j+l] += 1
for i in range(h):
  print("".join(list(map(str, res[i]))))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
ans=[['' for _ in range(w)] for _ in range(h)]
for i in range(h):
  s.append(list(input()))
for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      ans[i][j]="#"
    else:
      count=0
      for p in range(max(0,i-1),min(h-1,i+1)+1):
        for q in range(max(0,j-1),min(w-1,j+1)+1):
          if s[p][q]=="#":
            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())
Ss = []
for i in range(H):
  S = input()
  Ss.append(S)
Ns = [[0]*W for i in range(H)]
for i in range(H):
  for j in range(W):
    if Ss[i][j] == "#":
      Ns[i][j] = "#"
    else:
      r = 0
      for p in range(max(0, i-1), min(H-1, i+1)+1):
        for q in range(max(0, j-1), min(W-1, j+1)+1):
          if Ss[p][q] == "#":
            r += 1
      Ns[i][j] = r
for i in range(H):
  N = Ns[i]
  print("".join(map(str, N)))

Submission status: Accepted

Solution code:
y, x= map(int,input().split())
bomb = [0 for i in range(y)]
ary = [[0 for i in range(x)]for i in range(y)]

for i in range(y):
    bomb[i] = list(input())

for a in range(y):
    for b in range(x):
        if bomb[a][b] == "#":
            ary[a][b] = "#"
        else :
            ary[a][b] = 0
# この配列を用意することによって’＃’の周りの計算を楽にできるようになる
height_ary =[-1,-1,-1, 0,0, 1,1,1]
width_ary  =[-1, 0 ,1,-1,1,-1,0,1]

for a in range(y):
    for b in range(x):
        if ary[a][b] == "#":
            for i in range(8):
                h = height_ary[i] + a
                w = width_ary [i] + b
                if h < 0 or h >= y: continue
                if w < 0 or w >= x: continue
                if not ary[h][w] == "#":
                    ary[h][w] += 1

for i in range(y):
    result = map(str,ary[i])
    print("".join(result))

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
s = [input() for i in range(h)]
cel = [[[""]for i in range(w)] for j in range(h)]
dir = [[-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] == "#":
            cel[y][x] = "#"
for y in range(h):
    for x in range(w):
        if cel[y][x] == "#":
            continue
        bomb = 0
        for dx, dy in dir:
            cx = x+dx
            cy = y+dy
            if cx >= 0 and cx < w and cy >= 0 and cy < h:
                if cel[cy][cx] == "#":
                    bomb += 1
        cel[y][x] = bomb
for y in range(h):
    output = ""
    for x in range(w):
        output += str(cel[y][x])
    print(output)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
b = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            for k in range(max(0, i - 1), min(i + 2, h)):
                for l in range(max(0, j - 1), min(j + 2, w)):
                    b[k][l] += 1
for i in range(h):
    t = ""
    for j in range(w):
        if a[i][j] == "#":
            t += "#"
        else:
            t += str(b[i][j])
    print(t)

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
S = []
for h in range(H):
    S.append(input())
def countsh(x,y):#x,yの周囲にある＃を数える
    count=0
    if W != 1:
        if x == 0:
            if S[y][x+1] == '#':
                count+=1
        elif x == W - 1:
            if S[y][x-1] == '#':
                count+=1
        else:
            if S[y][x+1] == '#':
                count+=1
            if S[y][x-1] == '#':
                count+=1
    if H != 1:
        if y == 0:
            if S[y+1][x] == '#':
                count+=1
        elif y == H - 1:
            if S[y-1][x] == '#':
                count+=1
        else:
            if S[y+1][x] == '#':
                count+=1
            if S[y-1][x] == '#':
                count+=1
    if W != 1 and H != 1:
        if x > 0 :
            if y > 0:
                if S[y-1][x-1] == '#':
                    count+=1
            if y < H - 1:
                if S[y+1][x-1] == '#':
                    count+=1
        if x < W - 1:
            if y > 0:
                if S[y-1][x+1] == '#':
                    count+=1
            if y < H - 1:
                if S[y+1][x+1] == '#':
                    count+=1
    return count
result = [[0 for i in range(W)] for j in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            result[h][w] = str(countsh(w,h))
        else:
            result[h][w] = '#'
for h in range(H):
    print(''.join(result[h]))

Submission status: Accepted

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

ret = [[0] * W for _ in range(H)]
for y in range(H) :
    for x in range(W) :
        if S[y][x] == '#' :
            for yy in range(max(0, y-1), min(H, y+2)) :
                for xx in range(max(0, x-1), min(W, x+2)) :
                    if S[yy][xx] != '#' :
                        ret[yy][xx] += 1
                    else :
                        ret[yy][xx] = '#'
for i in range(H) :
    s = ''.join(map(str, ret[i]))
    print(s)

Submission status: Accepted

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

dot = [input() for _ in range(h)]

shape = [[j if j == '#' else 0 for j in i] for i in dot]

for i in range(h):
    for j in range(w):
        if shape[i][j] != '#':
            for a in range(-1, 2):
                for b in range(-1, 2):
                    if 0 <= i + a < h and 0 <= j + b < w and shape[i + a][j + b] == '#':
                        shape[i][j] += 1


ans = []
for i in shape:
    ans.append(list(map(str, i)))

for i in ans:
    for j in i:
        print(j, end = '')
    print()

Submission status: Accepted

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

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

dx = (1, 1, 0, -1, -1, -1, 0, 1)
dy = (0, 1, 1, 1, 0, -1, -1, -1)

ans = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == '#':
      ans[i][j] = '#'
    else:
      count = 0
      for k in range(8):
        if i + dx[k] < 0 or h <= i + dx[k]:
          continue
        elif j + dy[k] < 0 or w <= j + dy[k]:
          continue
        elif s[i+dx[k]][j+dy[k]] == '#':
          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())
s = []
s.append('.' * (w + 2))
for _ in range(h):
    s.append('.' + input() + '.')
s.append('.' * (w + 2))


def count_bomb(i, j):
    ret = 0
    l = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for dy, dx in l:
        if s[i + dy][j + dx] == '#':
            ret += 1

    return ret


for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == '.':
            print(count_bomb(i, j), end='')
        else:
            pass
            print(s[i][j], end='')
    print()

Submission status: Accepted

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

    mv = [[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]]
    for i in range(H):
        t = ""
        for j in range(W):
            if S[i][j] == "#":
                t += "#"
            else:
                count = 0
                for di, dj in mv:
                    if 0<=i+di < H and 0 <= j+dj < W:
                        if S[i+di][j+dj] == "#":
                            count += 1
                t += str(count)
        print(t)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
mine_map = [list(input()) for x in range(H)]
for i in range(H):
  for j in range(W):
    count = 0
    if mine_map[i][j] == ".":
      for k in range(-1,2):
        for l in range(-1,2):
          if i+k >= 0 and i+k < H and j+l >= 0 and j+l < W:
            if mine_map[i+k][j+l] == "#":
              count += 1
      mine_map[i][j] = count
    print(mine_map[i][j],end="")
  print()

Submission status: Accepted

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

d = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            cnt = 0
            for r in range(8):
                y = i + d[r][0]
                x = j + d[r][1]
                if 0 <= x < W and 0 <= y < H and S[y][x] == '#':
                        cnt = cnt + 1
            S[i][j] = str(cnt)

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

Submission status: Accepted

Solution code:
import numpy as np
H, W = map(int, input().split())

grid = np.array([[0] * (W + 2)] * (H + 2))

bombs = [0] * H
for h in range(H):
    bombs[h] = input()

for h in range(H):
    for w in range(W):
        if bombs[h][w] == '#':
            grid[h + 2][w + 2] += 1
            grid[h + 2][w + 1] += 1
            grid[h + 2][w + 0] += 1
            grid[h + 1][w + 2] += 1
            grid[h + 1][w + 0] += 1
            grid[h + 0][w + 2] += 1
            grid[h + 0][w + 1] += 1
            grid[h + 0][w + 0] += 1
            grid[h + 1][w + 1] = 100

grid[grid>=100] = 100
arr = grid.astype('str')
arr[arr=='100'] = '#'
for i in range(1, H + 1):
    print(''.join(arr[i][1:-1]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))
for i in range(H):
    for j in range(W):
        count = 0
        if(S[i][j]!="#"):
            for k in [-1,0,1]:
                for l in [-1,0,1]:
                    if(i+k>=H or i+k<0 or j+l>=W or j+l<0):
                        continue
                    elif(S[i+k][j+l]=="#"):
                        count += 1
            S[i][j] = str(count)
for i in range(H):
    print("".join(S[i]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
h, w = (int(i) for i in 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):
        cnt = 0
        if s[i][j] == "#":
            ans[i][j] = "#"
            continue
        for k in range(-1,2):
            for l in range(-1,2):
                if 0 <= i+k <= h-1 and 0 <= j+l <= w-1 and s[i+k][j+l] == "#":
                    cnt += 1
        ans[i][j] = cnt

for i in ans:
    print(*i,sep="")

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(h+2):
    for j in range(w+2):
        if(n[i][j]=="."):
            n[i][j]=0
        else:
            n[i][j]="1"


for i in range(h):
    for j in range(w):
        if(n[i+1][j+1]=="1"):
            if(n[i][j]!="1"):
                n[i][j]+=1
            if(n[i][j+1]!="1"):
                n[i][j+1]+=1
            if(n[i+1][j]!="1"):
                n[i+1][j]+=1
            if(n[i][j+2]!="1"):
                n[i][j+2]+=1
            if(n[i+2][j]!="1"):
                n[i+2][j]+=1
            if(n[i+1][j+2]!="1"):
                n[i+1][j+2]+=1
            if(n[i+2][j+1]!="1"):
                n[i+2][j+1]+=1
            if(n[i+2][j+2]!="1"):
                n[i+2][j+2]+=1

for i in range(h+2):
    for j in range(w+2):
        if(n[i][j]=="1"):
            n[i][j]="#"

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
L = [[0 for w in range(W)] for h in range(H)]
_L = [['' for w in range(W)] for h in range(H)]
for h in range(H):
    tmp = list(input())
    for w in range(W):
        if tmp[w] == "#":
            _L[h][w] = "#"
            if h+1 < H:
                L[h + 1][w] += 1
            if h > 0:
                L[h - 1][w] += 1
            if h+1 < H and w+1 < W:
                L[h + 1][w + 1] += 1
            if h > 0 and w+1 < W:
                L[h - 1][w + 1] += 1
            if w+1 < W:
                L[h][w + 1] += 1
            if h+1 < H and w > 0:
                L[h + 1][w - 1] += 1
            if h > 0 and w > 0:
                L[h - 1][w - 1] += 1
            if w > 0:
                L[h][w - 1] += 1
for h in range(H):
    for w in range(W):
        if _L[h][w] == "":
            _L[h][w] = str(L[h][w])

for h in range(H):
    print("".join(_L[h]))

Submission status: Accepted

Solution code:
def solve():
  h,w = (int(i) for i in input().split())
  String = [input() for _ in range(h)]
  query = []
  for i in range(h):
    for j in range(w):
      if String[i][j] == "#":
        query.append((i,j))

  dx = [(-1,-1),(-1,0),(0,-1),(-1,1),(0,1),(1,-1),(1,0),(1,1)]
  newString = [[0]*w for _ in range(h)]
  while query:
    i,j = query.pop()
    for x,y in dx:
      nx = i+x
      ny = j+y
      if nx < 0 or ny < 0 or nx >= h or ny >= w:
        continue
      else:
        newString[nx][ny] += 1
  for i in range(h):
    for j in range(w):
      if not String[i][j] =="#":
        newString[i][j] = str(newString[i][j])
      else:
        newString[i][j] = "#"
  for i in range(h):
    print("".join(newString[i]))
solve()

Submission status: Accepted

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

S = []
for _ in range(H):
	S.append([0 if x == '.' else x for x in list(input())])



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] == '#':
			for k in range(8):
				x = i + dx[k]
				y = j + dy[k]
				if x >= 0 and y >= 0 and x < H and y < W:
					if S[x][y] != '#':
						S[x][y] += 1

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 = []
for _ in range(H):
	S.append([0 if x == '.' else x for x in list(input())])

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] == '#':
			for k in range(8):
				x = i + dx[k]
				y = j + dy[k]
				if x >= 0 and y >= 0 and x < H and y < W:
					if S[x][y] != '#':
						S[x][y] += 1

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