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())
s = [input() for _ in range(h)]
"""
    全ての位置が0のマップ(二次元リスト)を新しく作る
    #の位置を調べてその位置を+9、周囲を+1
        位置が端なら加算は片側のみ
    8以下の数値を文字列として、9以上の数値を#として結合
"""
new_map = [[0 for _ in range(w)] for _ in range(h)]


def mine_count(i, j):
    """現在地に+9と周囲に+1"""
    new_map[i][j] += 9
    if i != 0:
        new_map[i-1][j] += 1
    if i != (h-1):
        new_map[i+1][j] += 1
    if j != 0:
        new_map[i][j-1] += 1
    if j != (w-1):
        new_map[i][j+1] += 1
    if i != 0 and j != 0:
        new_map[i-1][j-1] += 1
    if i != 0 and j != (w-1):
        new_map[i-1][j+1] += 1
    if i != (h-1) and j != 0:
        new_map[i+1][j-1] += 1
    if i != (h-1) and j != (w-1):
        new_map[i+1][j+1] += 1


# 白紙だったnew_mapを完成させる
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            mine_count(i, j)
# 文字列に変換、内包表記できるだろうか？
for i in range(h):
    for j in range(w):
        if new_map[i][j] <= 8:
            new_map[i][j] = str(new_map[i][j])
        else:
            new_map[i][j] = '#'
# 連結して出力
for i in range(h):
    print(''.join(new_map[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=['.'+input()+'.' for _ in range(h)]
s=['.'*(w+2)]+s+['.'*(w+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
        cnt=0
        if s[i][j]=='#': print('#',end='')
        else:
            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
            print(cnt,end='')
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            cnt = 0
            for k in range(8):
                if(i+dy[k] >= 0 and i+dy[k] < H and j+dx[k]>=0 and j+dx[k] < W and S[i+dy[k]][j+dx[k]]=='#'):
                    cnt+=1
            print(cnt, end='')
        else:
            print(S[i][j], end='')
    print('')

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=(int(x) for x in input().split())
s=[]
for i in range(h):
  s.append(input())
for i in range(h):
  res=[]
  for j in range(w):
    if s[i][j]==".":
      ini_i=i if i==0 else i-1
      fin_i=i if i==h-1 else i+1
      ini_j=j if j==0 else j-1
      fin_j=j if j==w-1 else j+1
      count=0
      for x in range(ini_i,fin_i+1):
        for y in range(ini_j,fin_j+1):
          if s[x][y]=="#":
            count+=1
      res.append(str(count))
    else:
      res.append("#")
  print("".join(res))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[]
for i in range(h):
    d=list(map(str,input()))
    d=['.']+d+['.']
    l.append(d)
l=[['.']*(w+2)]+l+[['.']*(w+2)]
#print(l)
for i in range(1,h+1):
    for j in range(1,w+1):
        if l[i][j]=='#':
            continue
        else:
            ans=0
            ans+=l[i-1][j-1:j+2].count('#')
            ans+=l[i+1][j-1:j+2].count('#')
            if l[i][j-1]=='#':ans+=1
            if l[i][j+1]=='#':ans+=1
            l[i][j]=ans
for i in range(1,h+1):
    print("".join(map(str,l[i][1:-1])))

Submission status: Accepted

Solution code:
# B
import numpy as np
h, w = map(int, input().split())
fi = np.array([['.' for _ in range(w)] for _ in range(h)])
for i in range(h):
    si = input()
    for j, s in enumerate(si):
        fi[i, j] = s
xs = [-1, 0, 1]
ys = [-1, 0, 1]
for i in range(h):
    for j in range(w):
        if fi[i, j] == '#':
            continue
        else:
            cnt = 0
            for x in xs:
                for y in ys:
                    ni = i + x
                    nj = j + y
                    if (0 <= ni <= h-1) and (0 <= nj <= w-1):
                        if fi[ni, nj] == '#':
                            cnt += 1
            fi[i, j] = str(cnt)
for row in fi:
    print(''.join(row))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
S = [input() for i in range(h)]
for r in range(h):
    l = ''
    for c in range(w):
        if S[r][c] == '#':
            l += '#'
        else:
            l += str(sum([x[max(0,c-1):min(c+2,w)].count('#') for x in S[max(0,r-1):min(r+2,h)]]))
    print(l)

Submission status: Accepted

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

for h in range(H):
  for w in range(W):
    if S[h][w] == ".":
      bomb = 0
      for nx, ny in zip(dx, dy):
        if h+nx<0 or w+ny<0 or h+nx>=H or w+ny>=W:
          pass
        else:
          if S[h+nx][w+ny] == '#':
            bomb+=1
      S[h][w] = str(bomb)
    else:
      pass

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

Submission status: Accepted

Solution code:
def count_neighbor_bomb(H,W,ci,cj, grid):
    count = 0
    for i in range(-1,2):
        if (ci + i)< 0 or (ci + i) > H-1:
            continue
        for j in range(-1,2):
            if (cj + j)< 0 or (cj + j) > W-1:
                continue
            if grid[ci + i][cj + j] == "#":
                count += 1
    return count

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

for i in range(H):
    for j in range(W):
        if grid[i][j] == "#":
            print("#", end="")
            continue
        bomb_num = count_neighbor_bomb(H,W,i,j,grid)
        print(grid[i][j].replace(".", str(bomb_num)), end="")
    print("")

Submission status: Accepted

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

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

for i in range(1, h + 1):
    for j in range(1, w + 1):
        cnt = 0
        if m[i][j] == "#":
            ans[i - 1][j - 1] = "#"
            continue
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if m[i + dx][j + dy] == "#":
                    cnt += 1
        ans[i - 1][j - 1] = str(cnt)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def count_mine(m, i, j):
    count = 0
    for k in range(i - 1, i + 2):
        for l in range(j - 1, j + 2):
            if m[k][l] == "#":
                count += 1
    return count

H, W = map(int, input().split())
l = [list(map(str, input().split())) for i in range(H)]
m = []
m.append("*" * (W + 2))
for i in range(len(l)):
    add_colum = "*" + ''.join(l[i]) + "*"
    m.append(add_colum)
m.append("*" * (W + 2))
answer = []
for i in range(1, H + 1):
    answer_column = ""
    for j in range(1, W + 1):
        if m[i][j] == ".":
            answer_column += str(count_mine(m, i, j))
        else:
            answer_column += "#"
    answer.append(answer_column)
for i in range(H):
    print(answer[i])

Submission status: Accepted

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

grid = []

for i in range(h):
    grid.append(list(input()))

#print(grid)

#cnt = 0

dir = [[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1],[1,0],[1,1]]

for i in range(h):
    for j in range(w):
        if grid[i][j] == '.':
            bomb=0
            #cnt += 1
            for k in range(8):
                if 0 <= i + dir[k][0] <= h-1 and 0 <= j + dir[k][1] <=w-1:
                    if grid[i + dir[k][0]][j + dir[k][1]] == '#':
                        bomb += 1
            grid[i][j] = str(bomb)

#print(grid)
#print(cnt)

for i in range(h):
    result = ''.join(grid[i])
    print(result)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[input() for _ in range(H)]
for i in range(H):
  T=''
  for j in range(W):
    if S[i][j] == '#':
      T+='#'
    else:
      T+=str(sum([x[max(0,j-1):min(j+2,W)].count('#') for x in S[max(0,i-1):min(i+2,H)]]))
    
  print(T)

Submission status: Accepted

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

#下、右、上、左、右下、右上、左下、左上
dy=[1,0,-1,0,1,-1,1,-1]
dx=[0,1,0,-1,1,1,-1,-1]

H,W=map(int,input().split())
field=[]

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

for y in range(H):
  for x in range(W):
    if field[y][x]=='#':
      continue
      
    cnt=0
      
    for n in range(8):
      nx=x+dx[n]
      ny=y+dy[n]
        
      if ny<0 or H<=ny or nx<0 or W<=nx:
        continue
        
      if field[ny][nx]=='#':
        cnt+=1
      
    field[y][x]=str(cnt)

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
H,W=map(int,input().split())
boxes=list()
pad=[0]*(W+2)
boxes.insert(0,pad)
for i in range(H):
  x=input()
  box=list()
  box.append(0)
  for j in range(W):
    if x[j]=='.':
      box.append(0)
    elif x[j]=='#':
      box.append(1)
  box.append(0) 
  boxes.insert(i+1,box)
boxes.insert(H+1,pad)
answers=list()
padd=[0]*W
for n in range(H):
  answer=list()
  for k in range(W):
    count=0
    if boxes[n][k]==1:
      count+=1
    if boxes[n][k+1]==1:
      count+=1
    if boxes[n][k+2]==1:
      count+=1
    if boxes[n+1][k]==1:
      count+=1
    if boxes[n+1][k+2]==1:
      count+=1
    if boxes[n+2][k]==1:
      count+=1
    if boxes[n+2][k+1]==1:
      count+=1
    if boxes[n+2][k+2]==1:
      count+=1
    if boxes[n+1][k+1]==1:
      count='#'
    else:
      count=str(count)
    answer.insert(k,count)
  answers.insert(n,answer)
for v in range(H):
  answe=''.join(answers[v])
  print(answe)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
vec = [-1,0,1]
for i in range(h): #H
    for j in range(w): #W
        if s[i][j] == '.':
            s[i][j] = '0'
        if s[i][j] != '#':
            for m in vec: #3
                for n in vec: #3
                    ver = i + m
                    hor = j + n
                    if 0 <= ver and ver <= h-1 and 0 <= hor and hor <= w-1 and s[ver][hor] == '#':
                        s[i][j] = str(1+int(s[i][j]))
for i in range(h):
    print(''.join(s[i]))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

S = [['.'] * (W + 2)] + S + [['.'] * (W + 2)]

for i in range(H):
    for j in range(W):
        temp = 0
        if S[i + 1][j + 1] == '.':
            for k in l:
                for kk in l:
                    if S[i + 1 + k][j + 1 + kk] == '#':
                        temp += 1
            S[i + 1][j + 1] = str(temp)

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

Submission status: Accepted

Solution code:
H , W = map (int, input().split())
S = [input() for _ in range(H)]
S_prime = ['0'] * (H+2)
ans = []
for i in range(H+2):
  if i == 0 or i == H+1:
    S_prime[i] = '0' * (W+2)
  else:
    S_prime[i] = '0' + S[i-1] + '0'
def f(x, y):
  aro = [S_prime[x][y], S_prime[x][y+1], S_prime[x][y+2], S_prime[x+1][y], S_prime[x+1][y+2], S_prime[x+2][y], S_prime[x+2][y+1], S_prime[x+2][y+2]]
  t = aro.count('#') 
  return str(t)
for i in range(H):
  X = ''
  for j in range(W):
    if S[i][j] == '#':
      X = X + '#'
    else:
      X = X + f(i,j)
  ans.append(X)
for i in range(H):
	print(ans[i])

Submission status: Accepted

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


def main():
    H, W = map(int, input().split())
    S = [list(input()[:-1]) for _ in range(H)]

    C = [['0']*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i][j]=='#':
                C[i][j] = '#'
                for op in itertools.product([-1, 0, +1], repeat=2):
                    ni = i + op[0]
                    nj = j + op[1]
                    if ni>=0 and ni<H and nj>=0 and nj<W and S[ni][nj]!='#':
                        C[ni][nj] = str(int(C[ni][nj]) + 1)
    
    for i in range(H):
        print(''.join(C[i]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

t = [['']*w for i in range(h)]
# print(t.count)

dx = [0, 0, 1, -1, 1, 1, -1, -1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
for y in range(h):
    for x in range(w):
        if s[y][x] == '#':
            t[y][x] = '#'
        else:
            count = 0
            for i in range(8):
                if x + dx[i] < 0 or x + dx[i] >= w or y + dy[i] < 0 or y + dy[i] >= h:
                    continue
                nx = x + dx[i]
                ny = y + dy[i]
                if s[ny][nx] == '#':
                    count += 1
            t[y][x] = str(count)

for i in range(len(t)):
    print(''.join(t[i]))

Submission status: Accepted

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

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

mapping = {'.':0, '#':1}

for i in range(H):
    for j in range(W):
        if Ss[i][j] == '#':
            ans[i][j] = '#'
            continue
            
        if i>0 and j>0:
            ans[i][j] += mapping[Ss[i-1][j-1]]
        if j>0:
            ans[i][j] += mapping[Ss[i+0][j-1]]
        if i<H-1 and j>0:
            ans[i][j] += mapping[Ss[i+1][j-1]]
            
        if i>0:
            ans[i][j] += mapping[Ss[i-1][j+0]]
        if i<H-1:
            ans[i][j] += mapping[Ss[i+1][j+0]]
            
        if i>0 and j<W-1:
            ans[i][j] += mapping[Ss[i-1][j+1]]
        if j<W-1:
            ans[i][j] += mapping[Ss[i+0][j+1]]
        if i<H-1 and j<W-1:
            ans[i][j] += mapping[Ss[i+1][j+1]]
        
for i in range(H):
  ans_=''
  for j in range(W):
    ans_ += str(ans[i][j])
  print(ans_)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[]
ll=[[1,0],[-1,0],[0,-1],[0,1],[1,1],[-1,1],[1,-1],[-1,-1]]
ans=[[0]*w for i in range(h)]
s=set()
for i in range(h):
    tmp=input()
    l.append(tmp)
    for j in range(w):
        if tmp[j]=="#":
            ans[i][j]="#"
            for x,y in ll:
                if 0<=i+x<h and 0<=j+y<w:
                    if ans[i+x][j+y]!="#":
                         ans[i+x][j+y]+=1
for i in range(h):
    tmp=""
    for j in range(w):
        tmp+=str(ans[i][j])
    print(tmp)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
m = []
for y in range(h):
  s = input()
  l = []
  for i in s:
    if i == '#':
      l.append(1)
    else:
      l.append(0)
  m.append(l)

for y in range(h):
  s = ''
  for x in range(w):
    bomb = 0
    for i,j in ((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)):
      if y+j < 0 or x+i < 0 or x+i >= w or y+j >= h:
        continue
      if m[y+j][x+i] == 1:
        bomb += 1
    if m[y][x] == 1:
      bomb = '#'
    s += str(bomb)
  print(s)

Submission status: Accepted

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

def mine(i,j):
  if i<0 or j<0 or i>=H or j>=W:
    return 0
  else:
    if S[i][j]=="#":
      return 1
    else:
      return 0

ij=[-1,0,1]
    
    
for i in range(H):
  for j in range(W):
    if S[i][j]==".":
      tmp=0
      for ii in ij:
        for jj in ij:
          if ii==jj and ii==0:
            1
          else:
            tmp+=mine(i+ii,j+jj)
      S[i][j]=tmp
#print(S)

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

Submission status: Accepted

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

for i in range(H):
    S_list[i] = ',' + S_list[i] + ','

U_list = [',' * (W + 2)]
S_newlist = U_list + S_list + U_list

pre_result = []
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S_newlist[i][j] == '#':
            pre_result.append('#')
        else:
            num = S_newlist[i][j - 1: j + 2].count('#') + \
                  S_newlist[i - 1][j - 1: j + 2].count('#') + \
                  S_newlist[i + 1][j - 1: j + 2].count('#')
            pre_result.append(str(num))

comb_result = ''
for s in pre_result:
    comb_result += s

result = [comb_result[n: n + W] for n in range(0, len(comb_result), W)]

ans = '\n'.join(result)
print(ans)

Submission status: Accepted

Solution code:
def func():
    h,w = map(int, input().split())
    S = ['.' + input() + '.' for _ in range(h)]
    S.insert(0,'.'*(w+2))
    S.append('.'*(w+2))
    
    for r in range(1, h+1):
        for c in range(1, w+1):
            if S[r][c] == '.':
                count = 0
                count += S[r-1][c-1:c+2].count('#')
                count += S[r][c-1:c+2].count('#')
                count += S[r+1][c-1:c+2].count('#')
                print(count, end='')
            else:
                print('#', end='')
        print()

if __name__ == '__main__':
    func()

Submission status: Accepted

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

for r in range(1, h+1):
    for c in range(1, w+1):
        if S[r][c] == '.':
            count = 0
            count += S[r-1][c-1:c+2].count('#')
            count += S[r][c-1:c+2].count('#')
            count += S[r+1][c-1:c+2].count('#')
            print(count, end='')
        else:
            print('#', end='')
    print()

Submission status: Accepted

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

for r in range(1, h+1):
    for c in range(1, w+1):
        if S[r][c] == '#':
            print('#', end='')
            continue
        else:
            count = 0
            count += S[r-1][c-1:c+2].count('#')
            count += S[r][c-1:c+2].count('#')
            count += S[r+1][c-1:c+2].count('#')
            print(count, end='')
    print()

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
S = [input() for _ in range(h)]
for r in range(h):
    l = ''
    for c in range(w):
        if S[r][c] == '#':
            l += '#'
        else:
            l += str(sum([x[max(0,c-1):min(c+2,w)].count('#') for x in S[max(0,r-1):min(r+2,h)]]))
    print(l)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
S = [input() for _ in range(h)]
for r in range(h):
    for c in range(w):
        count = 0
        if S[r][c] == '#':
            print('#', end='')
        else:
            for x in S[max(0,r-1):min(r+2,h)]:
                count += x[max(0,c-1):min(c+2,w)].count('#')
            print(count, end='')
    print()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
table = [[False] * w for _ in range(h)]
for i in range(h):
    s = input().rstrip()
    for j, c in enumerate(s):
        if c == '#':
            table[i][j] = True
res = [[] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if table[i][j]:
            res[i].append('#')
            continue
        count = 0
        for k in range(-1, 2):
            for l in range(-1, 2):
                if i + k < 0 or i + k >= h or j + l < 0 or j + l >= w:
                    continue
                if k == 0 and l == 0:
                    continue
                if table[i + k][j + l]:
                    count += 1
        res[i].append(str(count))
for l in res:
    print(''.join(l))

Submission status: Accepted

Solution code:
import itertools
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        H, W = map(int, input().split())
        S = [input().rstrip() for _ in range(H)]

        for h in range(H):
            for w in range(W):
                if S[h][w] == '#':
                    continue

                cnt = 0
                for a in itertools.product((-1, 0, 1), repeat=2):
                    x, y = h + a[0], w + a[1]
                    if 0 <= x < H and 0 <= y < W and S[x][y] == '#':
                        cnt += 1
                S[h] = S[h][:w] + str(cnt) + S[h][w + 1:]
        print(*S, sep='\n')


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
h, w = map(int,input().split())
a = ["."+input()+"." for _ in range(h)]
a.insert(0,"."*(w+2))
a.append("."*(w+2))
y = [1,1, 1,-1,-1,-1,0, 0]
x = [0,1,-1, 0, 1,-1,1,-1]
for i in range(1,h+1):
    l =[]
    for j in range(1,w+1):
        if a[i][j] == "#":
            l += "#"
        else: l += str(sum([1 for k in range(8) if a[i+y[k]][j+x[k]] == "#"]))
    print("".join(l))

Submission status: Accepted

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

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

def dfs(i, j):
  if s[i][j] == '#':
    return '#'
  else:
    cnt = 0
    moves = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
    for move in moves:
      if 0 <= i + move[0] < h and 0 <= j + move[1] <w:
        if s[i + move[0]][j + move[1]] == '#':
          cnt += 1
    return str(cnt)

for i in range(h):
  for j in range(w):
    print(dfs(i, j), end='')
  print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
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 h in range(H):
    for w in range(W):
        if S[h][w] == "#": 
            continue
        num = 0
        for d in range(8):
            hd, wd = h + dy[d], w + dx[d]

            if hd < 0 or H <= hd: continue
            if wd < 0 or W <= wd: continue
            if S[hd][wd] == "#": num += 1
        S[h][w] = str(num)

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

Submission status: Accepted

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

def f(m, i, j):
    if 0 <= i-1:
        start = j-1 if 0 <= j-1 else j
        end = j+2 if j+2 <= W else j+1
        for k in range(start, end):
            if m[i-1][k] != "#":
                m[i-1][k] = str(int(m[i-1][k]) + 1)
    if i+1 < H:
        start = j-1 if 0 <= j-1 else j
        end = j+2 if j+2 <= W else j+1
        for k in range(start, end):
            if m[i+1][k] != "#":
                m[i+1][k] = str(int(m[i+1][k]) + 1)
    if 0 <= j-1:
        if m[i][j-1] != "#":
            m[i][j-1] = str(int(m[i][j-1]) + 1)
    if j+1 < W:
        if m[i][j+1] != "#":
            m[i][j+1] = str(int(m[i][j+1]) + 1)
    return m

for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            S = f(S, h, w)

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

Submission status: Accepted

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

S = [list(input().replace(".", "0")) for _ in range(H)]  # .を0に置換しておく。文字列にしか置換できないので注意

def fill_cells_around_mine(i,j): # 地雷の座標(i,j)を与え、その周りのセルの値を+1する
    for y in range(max(0,i-1),min(H,i+2)):
        for x in range(max(0, j-1), min(W,j+2)):
            if S[y][x] != "#":
                S[y][x] = str(int(S[y][x])+1)

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            fill_cells_around_mine(i, j)

for idx in range(len(S)):
    print("".join(S[idx]))

Submission status: Accepted

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

S = []
for _ in range(H):
    S.append(list(str(input().replace(".", "0"))))  # .を0に置換しておく。文字列にしか置換できないので注意

def fill_blank_around_mine(i,j): # 地雷の座標(i,j)を与え、その周りのセルの値を+1する
    for y in range(max(0,i-1),min(H,i+2)):
        for x in range(max(0, j-1), min(W,j+2)):
            if S[y][x] != "#":
                S[y][x] = str(int(S[y][x])+1)

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            fill_blank_around_mine(i,j)

for idx in range(len(S)):
    print("".join(S[idx]))

Submission status: Accepted

Solution code:
import pprint
H, W = list(map(int, input().split()))
S = []
for h in range(H):
    S.append([c for c in input()])
# pprint.pprint(S)
for h in range(H):
    for w in range(W):
        if S[h][w] == ".":
            count = 0
            for pos_y in [-1, 0, 1]:
                for pos_x in [-1, 0, 1]:
                    y = h + pos_y
                    x = w + pos_x
                    if x < 0 or x >= W or y < 0 or y >= H:
                        continue
                    if S[y][x] == "#":
                        count += 1
            S[h][w] = str(count)
# pprint.pprint(S)
S = ["".join(s) for s in S]
S = "\n".join(S)
print(S)

Submission status: Accepted

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

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]==".":
            num = 0
            for k in range(3):
                for l in range(3):
                    x = k-1
                    y = l-1
                    if S[i+x][j+y]=="#":
                        num += 1
            S[i] = S[i][:j] + str(num) + S[i][j+1:]
for i in range(1,H+1):
    s = S[i]
    print(s[1:W+1])

Submission status: Accepted

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

grid = [['x'] * (w + 2)]

for _ in range(h):
    new_row = list(input())
    new_row.insert(0, 'x')
    new_row.append('x')
    grid.append(new_row)
grid.append(['x'] * (w + 2))

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if grid[i][j] == '.':
            mine_num = 0
            for krow in range(-1, 2):
                for kcol in range(-1, 2):
                    if grid[i + krow][j + kcol] == '#':
                        mine_num += 1
            grid[i][j] = str(mine_num)

for i in range(1, h + 1):
    print(''.join(grid[i][1:w + 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 n in range (w):

		if (s[i][n] == "#"):

			r = "#"

		else:

			cnt = 0
			r = 0

			for a in ([-1, 1]):
				if(i + a < 0) or (i + a > h - 1):
					continue
				for t in ([-1, 0, 1]):
					if ((n + t) < 0) or ((n + t) > (w - 1)):
						continue
					if (s[i + a][n + t] == "#"):
						cnt += 1
			for t in ([-1, 1]):
				if ((n + t) < 0) or ((n + t) > (w - 1)):
					continue
				if (s[i][n + t] == "#"):
					cnt += 1

			r = cnt

		print (r, end="")

	print ("")