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 -*-

def get_count(S, H, W, i, j):
    num = 0
    start_i = start_j = -1
    end_i = end_j = 2
    if W == 1:
        start_i = 0 
        end_i = 1
    else:
        if i == 0:
            start_i = 0
        elif i == W-1:
            end_i = 1
    if H == 1:
        start_j = 0
        end_j = 1
    else:
        if j == 0:
            start_j = 0
        elif j == H-1:
            end_j = 1

    for y in range(start_j, end_j, 1):
        for x in range(start_i, end_i, 1):
            if S[j+y][i+x] == "#":
                num += 1

    return num


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

for y in range(H):
    for x in range(W):
        if S[y][x] == ".":
            num = get_count(S, H, W, x, y)
            S[y][x] = str(num)

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=[list(input()) for i in range(h)]
ans= [["" for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    if S[i][j]=="#":
      ans[i][j]="#"
    else:
      cnt = 0
      for k in range(-1,2):
        for l in range(-1,2):
          if 0<=i+k<h and 0<=j+l<w and S[i+k][j+l]=="#":
            cnt += 1
      ans[i][j]=str(cnt)
for i in range(h):
  for j in range(w):
    print(ans[i][j],end="")
  print("")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

    for i in range(H):
        S[i] = list(S[i])

    for i in range(H):
        for k in range(W):

            if S[i][k]!="#":

                c = 0; #counter

                if i>0 and k>0 and S[i-1][k-1]=="#": # upper left
                    c += 1

                if i>0  and S[i-1][k]=="#": # right above
                    c += 1

                if i>0 and k<W-1 and S[i-1][k+1]=="#": # upper right
                    c+= 1

                if k>0 and S[i][k-1]=="#": # left
                    c += 1
           
                if k<W-1 and S[i][k+1]=="#": # right
                    c += 1

                if i<H-1 and k>0 and S[i+1][k-1]=="#": # lower left
                    c += 1

                if i<H-1 and S[i+1][k]=="#": # right under
                    c += 1

                if i<H-1 and k<W-1 and S[i+1][k+1]=="#": # lower right
                    c += 1

                S[i][k] = str(c)

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


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys

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

for i in range(H):
  for j in range(W):
    c=0
   
    if l[i][j]=="#":
      pass
   
    else:
      # 左上
      if 0<i and 0<j and l[i-1][j-1]=='#':
        c+=1
      # 上
      if 0<i and l[i-1][j]=='#':
        c+=1
      # 右上
      if 0<i and j<W-1 and l[i-1][j+1]=='#':
        c+=1
      # 左
      if 0<j and l[i][j-1]=='#':
        c+=1
      # 右
      if j<W-1 and l[i][j+1]=='#':
        c+=1
      # 左下
      if i<H-1 and 0<j and l[i+1][j-1]=='#':
        c+=1
      # 下
      if i<H-1 and l[i+1][j]=='#':
        c+=1
      # 右下
      if i<H-1 and j<W-1 and l[i+1][j+1]=='#':
        c+=1
        
              # 地雷隣接数を記載
      l[i][j]=str(c)

# 出力
for i in range(H):
  print(''.join(l[i]))

Submission status: Accepted

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

for i in range(X):
  for j in range(Y):
    if F[i][j] == '.':
      cnt = 0
      for k in range(8):
        x = i + DX[k]
        y = j + DY[k]
        if 0 <= x < X and 0 <= y < Y and F[x][y] == '#':
          cnt += 1
      F[i][j] = str(cnt)
      
for f in F:
  print(''.join(f))

Submission status: Accepted

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        c = 0
        for ii in range(i-1,i+2):
            if ii < 0 or ii >= H:
                continue
            for jj in range(j-1,j+2):
                if jj < 0 or jj >= W:
                    continue
                if S[ii][jj] == '#':
                    c += 1
        S[i][j] = str(c)

for row in S:
    print("".join(row))

Submission status: Accepted

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

for h in range(H):
    new_s = ''
    for w in range(W):
        if S[h][w] == '#': 
            new_s += '#'
            continue
        count = 0
        ary = [-1, 0, 1]
        for i in ary:
            for j in ary:
                if h+i < 0 or h+i >= H or w+j < 0 or w+j >=W: continue
                if S[h+i][w+j] == '#':
                    count += 1
        new_s += str(count)
    
    S[h] = new_s

for s in S:
    print(s)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            c = 0
            for x in range(-1, 2):
                for y in range(-1, 2):
                    if x == 0 and y == 0:
                        continue
                    else:
                        if 0 <= i+y and 0 <= j+x and i+y < h and j+x < w:
                            if s[i+y][j+x] == "#":
                                c += 1
            s[i][j] = str(c)
for a in range(h):
    print("".join(s[a]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h):
  s.append(list(input()))
  
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
    
    tmp = 0
    for k in range(8):
      ni = i + dx[k]
      nj = j + dy[k]
      
      if ni < 0 or ni >= h:
        continue
      if nj < 0 or nj >= w:
        continue
      if s[ni][nj] == "#":
        tmp += 1
    s[i][j] = str(tmp)

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

Submission status: Accepted

Solution code:
import typing
from typing import List, Counter
from itertools import product
class Grid:
    def __init__(self, field: List[str]):
        self.setField(field)
    
    def setField(self, field) -> None:
        self.column: int = len(field)
        self.row: int = len(field[0])
        self.field: List[List[str]] = [list(line) for line in field]

    def searchBomb(self) -> None:
        for i,j in product(range(self.column), range(self.row)):
            conn = tuple((i-1+k//3, j-1+k%3) for k in range(9))
            conn = tuple((x,y) for x,y in conn if 0<=x<self.column and 0<=y<self.row)
            if self.field[i][j] == '.':
                self.field[i][j] = str(Counter(self.field[x][y] for x,y in conn)['#'])
    
    def print(self) -> None:
        for line in self.field:
            print(''.join(line))

def main():
    with open(0) as f:
        H, W = map(int, f.readline().split())
        S = [line.strip() for line in f.readlines()]
    grid = Grid(S)
    grid.searchBomb()
    grid.print()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
m = [input() for _ in range(h)]
vx = [0,1,1,1,0,-1,-1,-1]
vy = [1,1,0,-1,-1,-1,0,1]
ans = [['#']*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if m[i][j] == '#':
            ans[i][j] = '#'
            continue
        count = 0
        for k in range(8):
            ny = vy[k] + i
            nx = vx[k] + j
            if 0<=ny and ny<h and 0<=nx and nx<w:
                if m[ny][nx] == '#':
                    count += 1
        ans[i][j] = str(count)
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)]

di=[1,1,0,-1,-1,-1,0,1]
dj=[0,1,1,1,0,-1,-1,-1]

for i in range(H):
    for j in range(W):
        ans=0
        if S[i][j]=='#':
            continue
        for k in range(8):
            ni=i+di[k]
            nj=j+dj[k]
            if ni<0 or nj<0 or ni>=H or nj>=W:
                continue
            if S[ni][nj]=='#':
                ans+=1
        S[i][j]=str(ans)
for i in range(H):
    for j in range(W):
        print(S[i][j],end='')
    print()

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    h, w = input_int_list()
    grid = []
    grid.append(["."] * (w + 2))
    for _ in range(h):
        grid.append(["."] + list(input()) + ["."])
    grid.append(["."] * (w + 2))

    directions = ((1, 1), (0, 1), (1, 0), (-1, 0), (0, -1), (-1, -1), (-1, 1), (1, -1))

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            cnt = 0
            # 周辺の爆弾を数える
            if grid[i][j] == ".":
                for x, y in directions:
                    if grid[i + x][j + y] == "#":
                        cnt += 1
                grid[i][j] = cnt

    for i in range(1, h + 1):
        print("".join(str(i) for i in grid[i][1:-1]))
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = [[0 for _ in range(W)] for _ in range(H)]
move = [(-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] == "#":
      ans[i][j] = "#"
    else:
      for x, y in move:
        nx, ny = i+x, j+y
        if nx < 0 or H <= nx or ny < 0 or W <= ny:
          continue
        elif S[nx][ny] == "#":
          ans[i][j] += 1
          
for a in ans:
  for i in range(W):
    a[i] = str(a[i])
  print("".join(a))

Submission status: Accepted

Solution code:
l = [int(c) for c in input().split()]
H = l[0]
W = l[1]
S = [input() for c in range(H)]

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
m = [input() for _ in range(h)]
vx=[0,1,1,1,0,-1,-1,-1]
vy=[1,1,0,-1,-1,-1,0,1]
ans=[['#']*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if m[i][j]=='#':
            ans[i][j]='#'
            continue
        count=0
        for k in range(8):
            ny=vy[k]+i
            nx=vx[k]+j
            if 0<=ny and ny<h and 0<=nx and nx<w:
                if m[ny][nx]=='#':
                    count+=1
        ans[i][j]=str(count)
for i in ans:
    print(''.join(i))

Submission status: Accepted

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

s = [['.']*(w+2)]+ [list('.'+ input() + '.') for i in range(h)] + [['.']*(w+2)]
a = [[0]*w for i in range(h)]


for i in range(1, h+1):
  for j in range(1,w+1):
    if s[i][j] == '#':
      a[i-1][j-1] = '#'
    else:
      b = [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]]
      c = b.count('#')
           
      a[i-1][j-1] = c

for i in range(h):
  a[i] = [str(x) for x in a[i]]
  print(''.join(a[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
C=[list(input()) for i in range(h)]
import copy
D=copy.deepcopy(C)
for i in range(h):
    for j in range(w):
        cnt=0
        if C[i][j]=="#":
            D[i][j]="#"
        else:
            if i>0 and j>0 and  C[i-1][j-1]=="#":
                cnt+=1
            if i>0 and  C[i-1][j]=="#":
                cnt+=1
            if i>0 and j<w-1 and C[i-1][j+1]=="#":
                cnt+=1
            if j>0  and C[i][j-1]=="#":
                cnt+=1
            if j<w-1 and C[i][j+1]=="#":
                cnt+=1
            if i<h-1 and j>0 and  C[i+1][j-1]=="#":
                cnt+=1
            if i<h-1 and  C[i+1][j]=="#":
                cnt+=1
            if i<h-1 and j<w-1 and  C[i+1][j+1]=="#":
                cnt+=1
            D[i][j]=str(cnt)
for k in range(h):
    mojiretu = ''.join(D[k])
    print(mojiretu)

Submission status: Accepted

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

mas = [list(map(str,input())) for _ in range(H)]
mas2 = [[0 for _ in range(W+2)] for __ in range(H+2)]

for i in range(1,H+1):
  for j in range(1,W+1):
    if mas[i-1][j-1] == '#':
      mas2[i][j] = -9
      mas2[i+1][j] += 1
      mas2[i+1][j-1] += 1
      mas2[i][j-1] += 1
      mas2[i-1][j-1] += 1
      mas2[i-1][j] += 1
      mas2[i-1][j+1] += 1
      mas2[i][j+1] += 1
      mas2[i+1][j+1] += 1

mas3 = [l[1:-1] for l in mas2[1:-1]]

for k in range(H):
  print(*["#" if i < 0 else i for i in mas3[k]],sep="")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
A = []
for i in range(H):
  A.append(input())
dis = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
for i in range(H):
  s = ""
  for j in range(W):
    if A[i][j] == "#":
      s += "#"
    else:
      count = 0
      for d in dis:
        if 0 <= i + d[0] < H and 0 <= j + d[1] < W:
          if A[i+d[0]][j+d[1]] == "#":
            count += 1
      s += str(count)
  print(s)

Submission status: Accepted

Solution code:
def period_to_zero(value:str):
    if value == ".":
        return 0
    return value

H,W = map(int, input().split())
S = list()
S.append([0]*(W+2))
for i in range(H):
    s = list()
    s.append(0)
    s.extend(list(map(period_to_zero, list(input()))))
    s.append(0)
    S.append(s)
S.append([0]*(W+2))

for i in range(1, len(S)-1):
    s_prev = S[i-1]
    s_current = S[i]
    s_next = S[i+1]
    for j in range(1, len(s_current)-1):
        if s_current[j] != "#":
            s_current[j] = (s_prev[j-1:j+2] + s_next[j-1:j+2] + [s_current[j-1], s_current[j+1]]).count("#")
        
for i in range(1, len(S)-1):
    s = S[i]
    print(*s[1:len(s)-1],sep="")

Submission status: Accepted

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

di = [1, 0, -1, 0, 1, -1, -1, 1]
dj = [0, 1, 0, -1, 1, 1, -1, -1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        cnt = 0
        for d in range(8):
            if i + di[d] <0 or i + di[d] > h - 1 or j + dj[d] < 0 or j + dj[d] > w - 1:
                continue
            if s[i + di[d]][j +dj[d]] == '#':
                cnt += 1
        ans[i][j] = cnt

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

Submission status: Accepted

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

for i in range(h):
    line = ""
    for j in range(w):
        hs = max(0, i - 1)
        he = min(h - 1, i + 1)
        ws = max(0, j - 1)
        we = min(w - 1, j + 1)
        counter = 0
        for hi in range(hs, he + 1):
            for wi in range(ws, we + 1):
                if table[hi][wi] == "#":
                    counter += 1
        if table[i][j] == "#":
            line += "#"
        else:
            line += str(counter)
    print(line)

Submission status: Accepted

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

p = [[0] * (w + 2) for i in range(h + 2)]

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

for m in range(1, h+1):
    for n in range(1, w+1):
        i = m - 1
        j = n - 1
        if s[i][j] == '#':
            p[m][n] ='#'

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

Submission status: Accepted

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

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

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] == '.':
            m = 0
            if i-1 >= 0 and j-1 >= 0:
                if s[i-1][j-1] == '#':
                    m += 1
            if i-1 >= 0:
                if s[i-1][j] == '#':
                    m += 1
            if i-1 >= 0 and j+1 < w:
                if s[i-1][j+1] == '#':
                    m += 1
            if j-1 >= 0:
                if s[i][j-1] == '#':
                    m += 1
            if j+1 < w:
                if s[i][j+1] == '#':
                    m += 1
            if i+1 < h and j-1 >= 0:
                if s[i+1][j-1] == '#':
                    m += 1
            if i+1 < h:
                if s[i+1][j] == '#':
                    m += 1
            if i+1 < h and j+1 < w:
                if s[i+1][j+1] == '#':
                    m += 1
            s[i][j] = str(m)
for i in range(h):
    z = list(s[i])
    print(''.join(z))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = list()
t = list()
a = [0 for i in range(w+2)]
s.append(a)
for i in range(h):
    b = str(input())
    c = list(('0'+b+'0').replace('.','0').replace('#','1'))
    d = list(map(int, c))
    s.append(d)
    t.append(b)
s.append(a)

for j in range(1,h+1):
    for k in range(1,w+1):
        if s[j][k] == 0:
            cnt = 0
            cnt = (s[j-1][k-1]+s[j-1][k]+s[j-1][k+1]) + (s[j][k-1]+s[j][k+1]) + (s[j+1][k-1]+s[j+1][k]+s[j+1][k+1])
            t[j-1] = t[j-1].replace('.', str(cnt), 1)
            
        else:
            continue
for i in range(h):
    print(t[i])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(H: int, W: int, S: "List[str]"):
    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                print("#", end="")
                continue
            n = 0
            if i - 1 >= 0:
                if S[i - 1][j] == "#":
                    n += 1
                if j + 1 < W and S[i - 1][j + 1] == "#":
                    n += 1
                if j - 1 >= 0 and S[i - 1][j - 1] == "#":
                    n += 1
            if i + 1 < H:
                if S[i + 1][j] == "#":
                    n += 1
                if j + 1 < W and S[i + 1][j + 1] == "#":
                    n += 1
                if j - 1 >= 0 and S[i + 1][j - 1] == "#":
                    n += 1
            if j + 1 < W and S[i][j + 1] == "#":
                n += 1
            if j - 1 >= 0 and S[i][j - 1] == "#":
                n += 1
            print(n, end="")
        print()
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    S = [list(next(tokens)) for _ in range(H)]  # type: "List[str]"
    solve(H, W, S)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i - 1][j - 1] == "#":
            ans[i - 1][j - 1] += 1
            ans[i - 1][j] += 1
            ans[i - 1][j + 1] += 1
            ans[i][j - 1] += 1
            ans[i][j + 1] += 1
            ans[i + 1][j - 1] += 1
            ans[i + 1][j] += 1
            ans[i + 1][j + 1] += 1

del ans[0]
del ans[-1]
for i in range(h):
    del ans[i][0]
    del ans[i][-1]
for i, _ in enumerate(s):
    tmp = ""
    for j, word in enumerate(_):
        if word == "#":
            tmp += "#"
        else:
            tmp += str(ans[i][j])
    print(tmp, end="\n")

Submission status: Accepted

Solution code:
import sys
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush

sys.setrecursionlimit(10**6)
INF = 10**20

def mint():
    return map(int,input().split())
def lint():
    return map(int,input().split())
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])

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

for i in range(H):
    for j in range(W):
        if S[i][j]=='#':
            print('#',end='')
        else:
            tmp = 0
            for a in [-1,0,1]:
                for b in [-1,0,1]:
                    if (a,b)!=(0,0) and 0<=i+a<H and 0<=j+b<W and S[i+a][j+b]=='#':
                        tmp += 1
            print(tmp,end='')
    print('')

Submission status: Accepted

Solution code:
from itertools import product as pro
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
ans = [["#"] * len(s[0]) for i in range(len(s))]
visited = []

def dfs(p):
    if p in visited or s[p[0]][p[1]] == "#":
        return None

    temp = [1 if s[y+p[0]][x+p[1]] == '#' else 0 for (y, x) in pro([-1, 0, 1], [-1, 0, 1]) if 0 <= x+p[1] < len(s[0]) and 0 <= y+p[0] < len(s)]
    s[p[0]][p[1]] = sum(temp)


for i in range(len(s)):
    for j in range(len(s[0])):
        dfs([i, j])


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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7


h, w = rm()
s = [rr() for _ in range(h)]
x = [0, 0, 1, -1, 1, 1, -1, -1]
y = [1, -1, 0, 0, 1, -1, 1, -1]
ans = ['']*h
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i] += '#'
        else:
            c = 0
            for dx, dy in zip(x, y):
                if 0 <= i + dx < h and 0 <= j + dy < w:
                    if s[i+dx][j+dy] == '#':
                        c += 1
            ans[i] += str(c)
for i in ans:
    print(i)

Submission status: Accepted

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

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

for i in range(h):
  for j in range(w):
    if l[i][j]=='#':
      ans[i][j]='#'
    else:
      for p in range(-1,2):
        for q in range(-1,2):
          if 0<=i+p<h and 0<=j+q<w and l[i+p][j+q]=='#':
            ans[i][j]+=1
            
for i in range(h):
  print(*ans[i],sep='')

Submission status: Accepted

Solution code:
# coding:utf-8
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = [[0] * w for _ in range(h)]
x = [-1, 0, 1, -1, 1, -1, 0, 1]
y = [-1, -1, -1, 0, 0, 1, 1, 1]

# print(s)


for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i][j] = '#'
        else:
            for k in range(len(x)):
                if 0 <= (i + x[k]) < h and 0 <= (j + y[k]) < w and s[i + x[k]][j + y[k]] == '#':
                    ans[i][j] += 1

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

Submission status: Accepted

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


for h in range(H):
    ans = ""
    for w in range(W):
        if XY[h][w] == "#":
            ans += "#"
            continue
        cnt = 0
        for h_diff in [-1, 0, 1]:
            for w_diff in [-1, 0, 1]:
                if h + h_diff < 0 or w + w_diff < 0:
                    continue
                elif h + h_diff >= H or w + w_diff >= W:
                    continue
                else:
                    if XY[h + h_diff][w + w_diff] == "#":
                        cnt += 1
        ans += str(cnt)
    print(ans)

Submission status: Accepted

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

data=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j]=='#':
      data[i][j]='#'
    else:
      for p in range(-1,2):
        for q in range(-1,2):
          if 0<=i+p<h and 0<=j+q<w and s[i+p][j+q]=='#':
            data[i][j]+=1
for i in range(h):
  print(*data[i],sep='')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
l = [["."]*(w+2)]
for i in range(h):
    l.append(["."] + [i for i in input()] + ["."])
l.append(["."]*(w+2))
 
def count(i,j):
    if l[i][j] == "#":
        return "#"
    c = 0
    for n in range(i-1,i+2):
        for m in range(j-1,j+2):
            if l[n][m] == "#":
                c += 1
    return str(c)
 
ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]
 
print("\n".join(ans))

Submission status: Accepted

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

S = [input() for _ in range(H)]
M = [[""]*W for _ in range(H)]

def bomb_cnt(i,j):
  min_h = max(0,i-1)
  max_h = min(i+2,H)
  min_w = max(0,j-1)
  max_w = min(j+2,W)
  cnt = 0
  for k in range(min_h,max_h):
    for g in range(min_w,max_w):
      if (S[k][g] == "#"):
        cnt += 1
        
  return str(cnt)

for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      M[i][j] = "#"
    else:
      M[i][j] = bomb_cnt(i,j)
      
      
for i in range(H):
  print(*M[i],sep="")