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 = list(list(input()) for _ in range(H))

for h in range(H):
    for w in range(W):
        cnt = 0
        if s[h][w] != "#":
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)):
                next_h, next_w = h + dh, w + dw
                if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                    continue
                elif s[next_h][next_w] == "#":
                    cnt += 1
            s[h][w] = str(cnt
                          )
for i in s:
    print("".join(i))

Submission status: Accepted

Solution code:
from collections import deque
h,w=map(int,input().split())
s=deque([deque(input()) for _ in range(h)])
s.appendleft(deque([0]*w))
s.append(deque([0]*w))
s=list(s)
for i in range(h+2):
  s[i].appendleft(0)
  s[i].append(0)
  s[i]=[0 if j==0 or j=='.' else '#' for j in tuple(s[i])]
for i in range(1,h+1):
  for j in range(1,w+1):
    if s[i][j]=='#':
      for a in range(3):
        for b in range(3):
          x=s[i-1+a][j-1+b]
          x=x+1 if type(x)==int else '#'
          s[i-1+a][j-1+b]=x
for i in range(1,h+1):
  print(''.join([str(j) for j in s[i]][1:-1]))

Submission status: Accepted

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

dh = [1,1,0,-1,-1,-1,0,1]
dw = [0,1,1,1,0,-1,-1,-1]

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


for H in range(h):
    for W in range(w):
        cnt = 0
        for i in range(8):
            if 0<=H+dh[i]<=h-1 and 0<=W+dw[i]<=w-1:
                if s[H+dh[i]][W+dw[i]] =='#':
                    cnt += 1
        if s[H][W] == '.':
            s[H] = s[H][:W] + str(cnt) + s[H][W+1:]

for c in s:
    print(c)

Submission status: Accepted

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

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

def top(i,j):
    ans=0
    if i==0:
        pass
    else:
        check=masu[i-1][max(0,j-1):min(w,j+2)]
        ans+=check.count("#")
    return ans

def bottom(i,j):
    ans=0
    if i==h-1:
        pass
    else:
        check=masu[i+1][max(0,j-1):min(w,j+2)]
        ans+=check.count("#")
    return ans

def side(i,j):
    ans=0
    check=masu[i][max(0,j-1):min(w,j+2)]
    ans+=check.count("#")
    return ans
    
def mawari(i,j):
    taishou=masu[i][j]
    if taishou==".":
        return str(top(i,j)+bottom(i,j)+side(i,j))
    else:
        return "#"

answer=[]
for i in range(h):
    gyou=[]
    for j in range(w):
        gyou.append(mawari(i,j))
    answer.append("".join(gyou))
    
for i in answer:
    print(i)

Submission status: Accepted

Solution code:
dh=[1,1,0,-1,-1,-1,0,1]
dw=[0,1,1,1,0,-1,-1,-1]
h,w=map(int, input().split())
s=[]
for _ in range(h):
    s.append(str(input()))

for H in range(h):
    for W in range(w):
        cnt=0
        for i in range(8):
            if 0<=H+dh[i]<=h-1 and 0<=W+dw[i]<=w-1:
                if s[H+dh[i]][W+dw[i]]=='#':
                    cnt+=1
        if s[H][W]!='#':
            s[H]=s[H][:W]+str(cnt)+s[H][W+1:]
for c in s:
    print(c)

Submission status: Accepted

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

l = []
l.append(["."]*(W+2))
for h in range(H):
  l.append(["."] + list(input()) + ["."])
l.append(["."]*(W+2))

ans = [[0 for w in range(W+2)] for h in range(H+2)]

for h in range(1,H+1):
  for w in range(1,W+1):
    if l[h][w] == "#":
      ans[h+1][w] += 1
      ans[h-1][w] += 1
      ans[h][w+1] += 1
      ans[h][w-1] += 1
      ans[h+1][w+1] += 1
      ans[h-1][w-1] += 1
      ans[h+1][w-1] += 1
      ans[h-1][w+1] += 1
      
for h in range(1,H+1):
  a = []
  for w in range(1,W+1):
    if l[h][w] == "#":
      a.append("#")
    else:
      a.append(str(ans[h][w]))
  print("".join(a))

Submission status: Accepted

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

for _ in range(H):
    row = list(input())
    for i in range(W):
        if row[i] == ".":
            row[i] = 0
    matrix.append(row)

for i in range(H):
    for j in range(W):
        if matrix[i][j] == "#":
            for k in [-1, 0, 1]:
                for l in [-1, 0, 1]:
                    if i+k < 0 or i+k == H or j+l < 0 or j+l == W:
                        continue
                    try:
                        matrix[i+k][j+l] += 1
                    except:
                        pass

for row in matrix:
    row = [str(x) for x in row]
    print("".join(row))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc075/tasks/abc075_b

H, W = map(int, input().split())

S_list = []

for h in range(H):
    S = list(input())
    S_list.append(S)


check_list = [[-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_list[i][j] == '.':
            count = 0
            for k in check_list:
                k_i = i - k[1]
                k_j = j - k[0]
                if k_i >= 0 and k_j >= 0 and k_i < H and k_j < W:
                    if S_list[k_i][k_j] == '#':
                        count += 1
            
            S_list[i][j] = str(count)

    print(''.join(S_list[i]))

Submission status: Accepted

Solution code:
def solve():
    H, W = map(int, input().split())
    S = [list(input()) for i in range(H)]
    c = 0
    lis = [[0]*W for i in range(H)]
    for i in range(H):
        for j in range(W):
            c = 0

            if S[i][j] == '#':
                lis[i][j] = '#'
                continue
            else:
                for k in range(-1, 2):
                    for l in range(-1, 2):
                        if k == 0 and l == 0:
                            pass
                        elif judge(i + k, j + l, W, H, S) == True:
                            c += 1
                if c == 0:
                    lis[i][j]= '0'
                else:
                    lis[i][j] = str(c)


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

# 位置が正しいかを判定
def judge(x, y, W, H, S):
    if (x < 0 or x > H - 1 or y < 0 or y >W - 1) == True:
        return False
    else:
        if S[x][y] == '#':
            return True
        else:
            return False

def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
S = []
result=[]
for n in range(H):
    a = list(input())
    a.insert(0,".")
    a.append(".")
    S.append(a)
a = ["."]*(W+2)
S.insert(0,a)
S.append(a)
result=[]


for h in range(H):
    result.append("")
    for w in range(W):
        if S[h+1][w+1]=="#":
            result[h] = result[h] + "#"
        else:
            sum=0
            for i in range(3):
                for j in range(3):
                    if S[h+i][w+j]=="#":
                        sum=sum+1
            sum = sum
            result[h] = result[h] + str(sum)

for h in range(H):
    print(result[h])

Submission status: Accepted

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

dx=[1,1,0,-1,-1,-1,0,1]
dy=[0,1,1,1,0,-1,-1,-1]
minesweeper=[['#']*W for _ in range(H)]
for y in range(H):
    for x in range(W):
        if S[y][x]=='.':
            cnt=0
            for i in range(8):
                if 0<=y+dy[i]<H and 0<=x+dx[i]<W:
                    if S[y+dy[i]][x+dx[i]] =='#':
                        cnt+=1
            minesweeper[y][x]=str(cnt)
#print(minesweeper)
for row in minesweeper:
    print(''.join(row))

Submission status: Accepted

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

    dx = (-1,0,1)
    dy = (-1,0,1)
    d_pattern = []
    for x in dx:
        for y in dy:
            if x == 0 and y == 0:
                continue
            d_pattern.append([y,x])

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

            cnt = 0
            for y, x in d_pattern:
                if 0 <= y+h < H and 0 <= x+w < W:
                    if S[y+h][x+w] == '#':
                        cnt += 1
            line.append(str(cnt))
        ans.append("".join(line))
    
    print(*ans, sep='\n')

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
m,n=map(int,input().split())
l=[list(input()) for _ in range(m)]
# print(l)
dx=[1,1,0,-1,-1,-1,0,1]
dy=[0,1,1,1,0,-1,-1,-1]

for i in range(m):
    for j in range(n):
        if l[i][j]=="#":
            # print(i,j)
            continue
        elif l[i][j]==".":
            cnt=0
            for k in range(8):
                x=dx[k]+i
                y=dy[k]+j
                if (x<0 or x>=m or y<0 or y>=n):continue
                elif l[x][y]=="#" :
                    cnt+=1
            l[i][j]=str(cnt)
for i in range(m):
    print("".join(l[i]))

Submission status: Accepted

Solution code:
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    h,w = map(int,input().split())
    s = []
    for i in range(h):
        s.append(list(input()))
    ans = [[0 for i in range(w+2)] for j in range(h+2)]
    for i in range(h):
        for j in range(w):
            if s[i][j]=="#":
                ans[i][j] += 1
                ans[i+1][j] += 1
                ans[i+2][j] += 1
                ans[i][j+1] += 1
                ans[i+2][j+1] += 1
                ans[i][j+2] += 1
                ans[i+1][j+2] += 1
                ans[i+2][j+2] += 1
    for i in range(h):
        for j in range(w):
            if s[i][j]=="#":
                ans[i+1][j+1] = "#"
            else:
                ans[i+1][j+1] = str(ans[i+1][j+1])
    for i in range(1,h+1):
        print(''.join(ans[i][1:w+1]))
    return

if __name__=='__main__':
    main()

Submission status: Accepted

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

A = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      A[i][j] = '#'
    else:
      c =0
      for k in [-1,0,1]:
        for l in [-1,0,1]:
          if i+k < 0 or i+k >= H or j+l < 0 or j+l >= W or (k == 0 and l == 0):
            continue
          if S[i+k][j+l] == '#':
            c += 1
      A[i][j] = c
for a in A:
  print(*a,sep='')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
grid = [['x'] + [str(c) for c in str(input())] + ['x'] for i in range(h)]
grid = [['x'] * (w+2)] + grid + [['x'] * (w+2)]
#print(grid)
ans = [[None for j in range(w+2)] for i in range(h+2)]
#print(ans)
for i in range(1, h+1):
  for j in range(1, w+1):
    if grid[i][j] == '#':
      ans[i][j] = '#'
      continue
    else:
      tmp = 0
      for k in range(i-1, i+2):
        tmp += grid[k][j-1:j+2].count('#')
      ans[i][j] = tmp
#print(ans)
for i in range(1, h+1):
  print(''.join([str(a) for a in ans[i][1:w+1]]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
list = []
for i in range(H):
  s = input()
  list.append(s)
ans = [[0] * W for i in range(H)]  

for i in range(H):
  for j in range(W):
    if list[i][j] == '#':
      try:
        ans[i][j] = ''
        ans[i][j] = '#'
      except:
        pass
      try:
        if i-1 == -1 or j-1 == -1:
          pass
        else:
          ans[i-1][j-1] += 1
      except:
        pass
      try:
        if i-1 == -1:
          pass
        else:
          ans[i-1][j] += 1
      except:
        pass
      try:
        if i-1 == -1:
          pass
        else:
          ans[i-1][j+1] += 1
      except:
        pass
      try:
        if j-1 == -1:
          pass
        else:
          ans[i][j-1] += 1
      except:
        pass
      try:
        ans[i][j+1] += 1
      except:
        pass
      try:
        if j-1 == -1:
          pass
        else:
          ans[i+1][j-1] += 1
      except:
        pass
      try:
        ans[i+1][j] += 1
      except:
        pass
      try:
        ans[i+1][j+1] += 1
      except:
        pass  
      
for i in range(H):
  line = ''
  for j in range(W):
    line += str(ans[i][j])
  print(line)

Submission status: Accepted

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

def check(i, j):
    ans = 0
    for k in [-1, 0, 1]:
        for l in [-1, 0, 1]:
            if k == 0 and l == 0:
                continue
            elif 0 <= i+k < H and 0 <= j+l < W:
                if tizu[i+k][j+l] == "#":
                    ans += 1
    return ans

for a in range(H):
    for b in range(W):
        if tizu[a][b] == "#":
            continue
        else:
            tizu[a][b] = check(a, b)

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

Submission status: Accepted

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

grid = [list(input()) for _ in range(H)]
num = [[0] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            for di in [-1, 0, 1]:
                for dj in [-1, 0, 1]:
                    ni = i + di
                    nj = j + dj
                    if 0 <= ni < H and 0 <= nj < W:
                        if grid[ni][nj] == '#':
                            num[ni][nj] = '#'
                        else:
                            num[ni][nj] += 1

for i in range(H):
    print(*num[i], sep='')

Submission status: Accepted

Solution code:
import sys

h, w = map(int, input().split())

s = [list(map(str, input())) for i 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 k in range(8):
            ni = i + dy[k]
            nj = j + dx[k]

            if ni < 0 or ni >= h:
                continue
            if nj < 0 or nj >= w:
                continue
            if s[ni][nj] == '#':
                num += 1
        
        s[i][j] = str(num)

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
SS = []
for i in range(H):
    SS.append(list(input()))
    
def seekindices(i,j,H,W):
    seeks = []
    for x in [-1,0,1]:
        for y in [-1,0,1]:
            if i + x >= 0 and i + x < H:
                if j + y >= 0 and j + y < W:
                    seeks.append((i+x,j+y))
    return(seeks)

AS = []

for h in range(H):
    s = SS[h]
    
    a = []
    
    for w in range(W):
        if s[w] == '.':
            seeks = seekindices(h,w,H,W)
            count = 0
            for zahyo in seeks:
#                 print(zahyo)
                if SS[zahyo[0]][zahyo[1]] == '#':
                    count += 1
            a.append(str(count))
        else:
            a.append(s[w])
    AS.append(a)
                    
for a in AS:
    print("".join(a))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = [input() for i in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == '#' :
            print('#',end='')
            continue
        cnt = 0
        for dx in [-1,0,1]:
            for dy in [-1,0,1]:
                if not 0 <= i+dx < h or not 0 <= j+dy < w: continue
                if S[i+dx][j+dy] == '#' : cnt += 1
        print(cnt,end='')
    print()

Submission status: Accepted

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

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

for i in range(1,h+1):
    for j in range(1,w+1):
        if data[i][j] == '#':
            print('#',end='')
            continue
        cnt = 0
        for k in range(8):
            if data[i+dx[k]][j+dy[k]] == '#': cnt += 1
        print(cnt,end='')
    print()

Submission status: Accepted

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

for i in range(H):
  S[i] = ["."] + S[i] + ["."]
S = [["."]*(W+2)] + S + [["."]*(W+2)]
ans = [[0]*W for _ 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] = "#"
      continue
    cnt = 0
    for k in range(i-1, i+2):
      for l in range(j-1, j+2):
        if S[k][l] == "#":
          cnt += 1
    ans[i-1][j-1] = str(cnt)
for a in ans:
  print("".join(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
mine = []
dx = [-1, 0, 1, 1, 1, 0, -1, -1]
dy = [-1, -1, -1, 0, 1, 1 ,1, 0]
for i in range(H):
    s_list = list(input())
    mine.append(s_list)
for i in range(H):
    for j in range(W):
        if mine[i][j] == "#":
            continue
        num = 0
        for k in range(8):
            nx = i + dx[k]
            ny = j + dy[k]
            if nx<0 or nx>=H or ny < 0 or ny >= W:
                continue
            else:
                if mine[nx][ny]=="#":
                    num += 1
        mine[i][j]=str(num)
for i in mine:
    print("".join(i))

Submission status: Accepted

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

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

for y in range(h):
    for x in range(w):
        if (map_data[y][x] == "."):
            bs = 0
            for yy in range(max(0, y - 1), min(h, y + 2)):
                for xx in range(max(0, x - 1),min(w, x + 2)):
                    bs += map_data[yy][xx] == "#"
            map_data[y][x] = str(bs)

for y in range(h):
    print("".join(map_data[y]))

Submission status: Accepted

Solution code:
n,m= map(int, input().split())
a=[list(input()) for i in range(n)]
b=0
for i in range(n):
    for j in range(m):
        if a[i][j]==".":
            if n-1>=i+1 and j-1>=0 and a[i+1][j-1]=='#': #左下
                b+=1
            if j-1>=0 and a[i][j-1]=='#': #左
                b+=1
            if i-1>=0 and j-1>=0 and a[i-1][j-1]=='#': #左上
                b+=1
            if i-1>=0 and a[i-1][j]=='#': #上
                b+=1
            if i-1>=0 and m-1>=j+1 and a[i-1][j+1]=='#': #右上
                b+=1
            if m-1>=j+1 and a[i][j+1]=='#': #右
                b+=1
            if n-1>=i+1 and m-1>=j+1 and a[i+1][j+1]=='#': #右下
                b+=1
            if n-1>=i+1 and a[i+1][j]=='#': #下
                b+=1
            a[i][j]=b
            b=0
        print(a[i][j], end="")
    print()

Submission status: Accepted

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

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

for h in range(1, H+1):
    for w in range(1, W+1):
        if S[h][w] == '.':
            cnt = 0
            for dy, dx in table:
                if S[h+dy][w+dx] == '#':
                    cnt += 1
            S[h] = S[h][:w] + str(cnt) + S[h][w+1:]

for h in range(1, H+1):
    print(S[h][1:-1])

Submission status: Accepted

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

ans = []

for row in mass:
  ans.append(list(row))
  
dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]

for y in range(h):
  for x in range(w):
    if mass[y][x] == '#':
      continue
    c = 0
    for dx,dy in dxy:
      if x + dx < 0 or x + dx > w - 1 or y + dy < 0 or y + dy > h - 1:
        continue
      if mass[y + dy][x + dx] == "#":
        c += 1
    ans[y][x] = c
    
for row in ans:
  print(''.join( list(map(str,row))))

Submission status: Accepted

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

mass = [input() for i in range(h)]

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

bomb_pos = []


for i in range(h):
  if mass[i].find("#") == -1:
    continue
  index_num = [n for n, v in enumerate(mass[i]) if v == "#"]
  for index in index_num:
    bomb_pos.append([i,index])

for xy in bomb_pos:
  for i in range(max(0,xy[0]-1),min(xy[0]+2,h)):
      for j in range(max(0,xy[1]-1),min(xy[1]+2,w)):
        ans[i][j] += 1

for xy in bomb_pos:
  ans[xy[0]][xy[1]] = '#'
  
for arr in ans:
  print(''.join([str(n) for n in arr]))

Submission status: Accepted

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

ans = []
for y in range(H):
    tmp=[]
    for x in range(W):
        if S[y][x]=='.':
            cnt=0
            if y-1>=0:
                if S[y-1][x]=='#':cnt+=1
            if y-1>=0 and x+1<W:
                if S[y-1][x+1]=='#':cnt+=1
            if x+1<W:
                if S[y][x+1]=='#':cnt+=1
            if y+1<H and x+1<W:
                if S[y+1][x+1]=='#':cnt+=1
            if y+1<H:
                if S[y+1][x]=='#':cnt+=1
            if y+1<H and x-1>=0:
                if S[y+1][x-1]=='#':cnt+=1
            if x-1>=0:
                if S[y][x-1]=='#':cnt+=1
            if y-1>=0 and x-1>=0:
                if S[y-1][x-1]=='#':cnt+=1
            tmp.append(cnt)
#             print(cnt)
        else:
            tmp.append('#')
#             print('#')
    ans.append(tmp)
for row in ans:
    print(''.join(map(str, row)))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = [['' for i in range(w+2)] for j in range(h+2)]
for i in range(h):
    S[i+1][1:w+1] = input()
for i in range(h):
    for j in range(w):
        if S[i+1][j+1]=='.':
            S[i+1][j+1]=str(sum(S[k+i][j:j+3].count('#') for k in range(3)))
    print(*S[i+1][1:w+1],sep='')

Submission status: Accepted

Solution code:
def f(s):
    if s == "#":
        return 1
    else:
        return 0

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

            # if 0 <= i-1:
            #     cnt += f(s[i-1][j])
            # if i+1 < h:
            #     cnt += f(s[i+1][j])
            # if 0 <= j-1:
            #     cnt += f(s[i][j-1])
            # if j+1 < w:
            #     cnt += f(s[i][j+1])
            
            # if 0 <= i-1 and 0 <= j-1:
            #     cnt += f(s[i-1][j-1])
            # if 0 <= i-1 and j+1 < w:
            #     cnt += f(s[i-1][j+1])
            # if i+1 < h and 0 <= j-1:
            #     cnt += f(s[i+1][j-1])
            # if i+1 < h and j+1 < w:
            #     cnt += f(s[i+1][j+1])
            
            
            s[i][j] = cnt

for i in range(h):
    for j in range(w):
        print(s[i][j], end="")
    if i != h-1:
        print("\n")

Submission status: Accepted

Solution code:
def f(s):
    if s == "#":
        return 1
    else:
        return 0

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):
        if s[i][j] == ".":
            cnt = 0
            if 0 <= i-1:
                cnt += f(s[i-1][j])
            if i+1 < h:
                cnt += f(s[i+1][j])
            if 0 <= j-1:
                cnt += f(s[i][j-1])
            if j+1 < w:
                cnt += f(s[i][j+1])
            
            if 0 <= i-1 and 0 <= j-1:
                cnt += f(s[i-1][j-1])
            if 0 <= i-1 and j+1 < w:
                cnt += f(s[i-1][j+1])
            if i+1 < h and 0 <= j-1:
                cnt += f(s[i+1][j-1])
            if i+1 < h and j+1 < w:
                cnt += f(s[i+1][j+1])
            
            
            s[i][j] = cnt

for i in range(h):
    for j in range(w):
        print(s[i][j], end="")
    if i != h-1:
        print("\n")

Submission status: Accepted

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

def bombNumber(y, x):
    tmp = 0
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            if x + dx == -1 or x + dx == w or y + dy == -1 or y + dy == h:
                continue

            if S[y + dy][x + dx] == "#":
                tmp = tmp + 1

    return(tmp)


for i in range(h):
    for j in range(w):
        if S[i][j] == ".":
            S[i][j] = str(bombNumber(i, j))

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

Submission status: Accepted

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

for y in range(h):
    for x in range(w):
        if g[y][x] == '.':
            cnt = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    nx = x + dx
                    ny = y + dy
                    if nx < 0 or w <= nx or ny < 0 or h <= ny:
                        continue
                    if g[ny][nx] == "#":
                        cnt += 1
            g[y][x] = str(cnt)

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

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(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == '#':
            ans[h - 1].append('#')
        else:
            ans[h - 1].append(str(sum([S[h + nh][w + nw] == '#' for nh, nw in N])))
for h in range(H):
    print(''.join(ans[h]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
ans = [[''] * W for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            ans[h][w] = '#'
        else:
            ans[h][w] = str(sum([S[h + nh][w + nw] == "#" for nh, nw in N if 0 <= h + nh < H and 0 <= w + nw < W]))

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

Submission status: Accepted

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

def func1(a, th):
    tmp = [a]
    if a-1 >= 0:
        tmp.append(a-1)

    if a+1 < th:
        tmp.append(a+1)
    return tmp

def func(x,y):
    count = 0
    for i in func1(x, h):
        for j in func1(y, w):
            if map_list[i][j] == "#":
                count += 1
    return str(count)

for x, rows in enumerate(map_list):
    new_lines = ""
    for y, p in enumerate(rows):
        if p == ".":
            new_lines += func(x, y)
        else:
            new_lines += p
    print(new_lines)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
h, w = map(int,input().split())
s = []
for i in range(h):
    s.append(list(str(input())))

dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
ans = [['' for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i][j] = '#'
        else:
            tmp = 0
            for k in range(8):
                if 0 <= i + dy[k] < h and 0 <= j + dx[k] < w:
                    if s[i + dy[k]][j + dx[k]] == '#':
                        tmp += 1
            ans[i][j] = str(tmp)
for i in range(h):
    print(''.join(ans[i]))

Submission status: Accepted

Solution code:
import sys 
MOD = 10 ** 9 + 7
INF = 10**11
from heapq import heapify,heappush,heappop

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

def main():
    h,w = map(int,input().split())
    grid = [list(input()) for _ in range(h)]

    for i in range(h):
        for j in range(w):
            if grid[i][j] == '#':
                continue
            cnt = 0
            for k in range(8):
                y = i + dy[k]
                x = j + dx[k]
                if 0 <= y < h  and 0 <= x < w and grid[y][x] == '#':
                    cnt += 1
            grid[i][j] = str(cnt)
    
    for i in range(h):
        print(''.join(grid[i]))

if __name__=='__main__':
    main()

Submission status: Accepted

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

output = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            output[i][j] = '#'

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

for i in range(H):
    for j in range(W):
        if output[i][j] == '#':
            continue
        #cnt = 0
        for dxi, dyi in zip(dx, dy):
            nx = j + dxi
            ny = i + dyi
            if 0 <= nx < W and 0 <= ny < H: 
                if maze[ny][nx] == '#':
                    #cnt += 1
                    output[i][j] += 1
                
for i in range(H):
    print(''.join(map(str, output[i])))