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:
def solve():
  H, W = map(int, input().split())
  S = [0]*(H+2)
  S[0] = '*'*(W+2)
  S[H+1] = '*'*(W+2)
  for h in range(1,H+1):
    S[h] = '*' + input() + '*'
  ans = [[0]*W for _ in range(H)]
  dh = [1,1,1,0,-1,-1,-1,0]
  dw = [1,0,-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][w-1]='#'
        continue
      for i in range(8):
        if S[h+dh[i]][w+dw[i]]=='#':
          ans[h-1][w-1] += 1
    ans[h-1] = ''.join(map(str,ans[h-1]))
  return ans
print(*solve(),sep='\n')

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    h, w = i_map()
    str_list = s_row_list(h)

    for i in range(h):
        for j in range(w):
            if str_list[i][j] == '#':
                continue
            count = 0
            for dx, dy in ([1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]):
                ni = i + dx
                nj = j + dy
                if ni < 0 or ni >= h or nj < 0 or nj >= w:
                    continue
                if str_list[ni][nj] == '#':
                    count += 1
            str_list[i][j] = count

    for i in range(h):
        for j in range(w):
            print(str_list[i][j], end='')
        print('')
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = []
S.append('.'*(2+W))
for i in range(H):
    S.append('.'+input()+'.')
S.append('.'*(2+W))
Sans = []
for i in range(1,H+1):
    S1 = ''
    for j in range(1,W+1):
        ans = 0
        if S[i][j] == '.':
            if S[i-1][j-1] == '#':
                ans +=1
            if S[i-1][j] == '#':
                ans +=1
            if S[i-1][j+1] == '#':
                ans +=1
            if S[i][j-1] == '#':
                ans +=1
            if S[i][j+1] == '#':
                ans +=1
            if S[i+1][j-1] == '#':
                ans +=1
            if S[i+1][j] == '#':
                ans +=1
            if S[i+1][j+1] == '#':
                ans +=1
        if S[i][j] == '#':
            ans = '#'
        S1 += str(ans)
    Sans.append(S1)

for i in range(H):
    print(Sans[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 j in range(w):
        if s[i][j] == ".":
            bomb = 0
            dx = [0]
            dy = [0]
            if i != 0:
                dx.append(-1)
            if i != h - 1:
                dx.append(1)
            if j != 0:
                dy.append(-1)
            if j != w - 1:
                dy.append(1)

            for di in dx:
                for dj in dy:
                    if s[i + di][j + dj] == "#":
                        bomb += 1

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

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

Submission status: Accepted

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

S = ['.'*(W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.'*(W + 2)]

for x in range(1, H+1):
    for y in range(1, W+1):
        if S[x][y] == '.':
            Bomb = (S[x-1][y-1:y+2] + S[x][y-1:y+2] + S[x+1][y-1:y+2]).count('#')
            S[x] = S[x][:y] + str(Bomb) +S[x][y+1:]

for i in range(1, H+1):
    print(S[i][1:W+1])

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [list(input()) for _ in range(n)]
#a = [int(input()) for _ in range(n)]


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

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

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

for i in range(h):
    for j in range(w):
        print(s[i][j], end="")
    print("")

Submission status: Accepted

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

for i in range(1, h + 1):
  for j in range(1, w + 1):
    if s[i][j] != "#":
      tmp = ""
      tmp += s[i-1][j-1]+s[i-1][j]+s[i-1][j+1]
      tmp += s[i][j-1]+s[i][j+1]
      tmp += s[i+1][j-1]+s[i+1][j]+s[i+1][j+1]
      s[i][j] = str(tmp.count("#"))

for i in range(1, h + 1):
  for j in range(1, w + 1):
    print(s[i][j], end="")
  print()

Submission status: Accepted

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

ans = []
for i in range(1, h + 1):
  tmp = []
  for j in range(1, w + 1):
    if s[i][j] == "1":
      tmp.append("#")
    else:
      tmp2 = ""
      tmp2 += s[i-1][j-1]
      tmp2 += s[i-1][j]
      tmp2 += s[i-1][j+1]
      tmp2 += s[i][j-1]
      tmp2 += s[i][j+1]
      tmp2 += s[i+1][j-1]
      tmp2 += s[i+1][j]
      tmp2 += s[i+1][j+1]
      tmp.append(tmp2.count("1"))
  ans.append(tmp)

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())
s0='.'*(W+2)
s1='.'+input()+'.'

ans=''
for i in range(H):
    if i==H-1:
        s2='.'*(W+2)
    else:
        s2='.'+input()+'.'
    
    for j in range(1,W+1):
        if s1[j]=='.':
            x=s0[j-1:j+2]+s1[j-1]+s1[j+1]+s2[j-1:j+2]
            ans+=str(x.count('#'))
        else:
            ans+='#'
    s0=s1
    s1=s2

for i in range(H):
    print(ans[i*W:i*W+W])

Submission status: Accepted

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

padded = []
for i in range(H+2):
  if i == 0 or i == H+1:
    padded.append('.'*(W+2))
  else:
    padded.append('.'+S[i-1]+'.')
    
def calc(row,col):
  out=0
  for i in range(3):
    for j in range(3):
      if padded[row-1+i][col-1+j]=='#':out+=1
  return out

ans=[[] for _ in range(H)]
for i in range(1,H+1):
  for j in range(1,W+1):
    if padded[i][j]=='#':
      ans[i-1].append('#')
      continue
    tmp = calc(i,j)
    ans[i-1].append(str(tmp))
  ans[i-1] = ''.join(ans[i-1])

for i in range(H):
  print(ans[i])

Submission status: Accepted

Solution code:
def f(L, i, j):
  h = len(L)
  w = len(L[0])
  if L[i][j] != ".":
    return L[i][j]
  else:
    around = [L[i-1][j-1], L[i-1][j], L[i-1][j+1], L[i][j-1], L[i][j+1], L[i+1][j-1], L[i+1][j], L[i+1][j+1]]
    return str(around.count("#"))

H, W = list(map(lambda x: int(x), input().split(" ")))
A = []
A.append(["X"] * (W + 2))
for i in range(H):
  A.append(["X"])
  A[i + 1].extend(list(input()))
  A[i + 1].extend(["X"])
A.append(["X"] * (W + 2))

for i in range(len(A)):
  for j in range(len(A[0])):
    A[i][j] = f(A, i, j)

print("\n".join(["".join(a[1:-1]) for a in A[1:-1]]))

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
T = []
T.append(["."]*(W+2))
for _ in range(H):
  s = "."+input()+"."
  T.append(list(s))
T.append(["."]*(W+2))
for h in range(1, H+1):
  for w in range(1, W+1):
    if T[h][w]==".":
      S = [T[a][b] for b in range(w-1, w+2) \
           for a in range(h-1, h+2)]
      T[h][w]=S.count("#")
for h in range(1, H+1):
  print(*T[h][1:W+1], sep="")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    H,W=mi()
    G = [[] for _ in range(H)]
    for i in range(H):
        S = input()
        G[i] = list(S)
    
    # print(G)
    for h in range(H):
        for w in range(W):
            if G[h][w] == "#": continue
            res = 0
            for dh in [-1,0,1]:
                for dw in [-1,0,1]:
                    if dh == dw == 0: continue
                    if 0 <= h+dh < H and 0<= w+dw < W:
                        if G[h+dh][w+dw] == "#":
                            res += 1
            
            G[h][w] = str(res)

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



if __name__ == "__main__":
    main()

Submission status: Accepted

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

def count_mine(i, j, s, h, w):
  mine = 0
  # 一段上
  if i-1 >= 0 and j-1 >= 0 and s[i-1][j-1] == "#":
    mine += 1
  if i-1 >= 0 and s[i-1][j] == "#":
    mine += 1
  if i-1 >= 0 and j+1 < w and s[i-1][j+1] == "#":
    mine += 1
  
  # 真ん中
  if j-1 >= 0 and s[i][j-1] == "#":
    mine += 1
  if j+1 < w and s[i][j+1] == "#":
    mine += 1
    
  # 一段下
  if i+1 < h and j-1 >= 0 and s[i+1][j-1] == "#":
    mine += 1
  if i+1 < h and s[i+1][j] == "#":
    mine += 1
  if i+1 < h and j+1 < w and s[i+1][j+1] == "#":
    mine += 1
    
  return mine

for i in range(h):
  for j in range(w):
    if s[i][j] == ".":
      s[i][j] = count_mine(i, j, s, h, w)
  print(''.join(map(str, s[i])))

Submission status: Accepted

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

maps = [[0 for _ in range(W+2)]]

for _ in range(H):
    line = [dot for dot in ("."+input()+".")]
    line_ = [1 if point == "#" else 0 for point in line]
    maps.append(line_)
maps.append([0 for _ in range(W+2)])

    
def calc(x, y):
    return maps[y-1][x] + maps[y+1][x] + maps[y][x+1] + maps[y][x-1] \
            + maps[y+1][x+1] + maps[y-1][x-1] + maps[y+1][x-1] + maps[y-1][x+1]

for y in range(1,H+1):
    for x in range(1,W+1):
        if maps[y][x] != 1:
            print(calc(x,y), end="")
        else:
            print("#",end= "")
    print()

Submission status: Accepted

Solution code:
def resolve():
    H, W = map(int, input().split())
    S = []
    for _ in range(H):
        s = input()
        S.append(s)
    for i in range(H):
        ans = ""
        for j in range(W):
            count = 0
            if S[i][j] == "#":
                ans += "#"
            else:
                if i >= 1 and j >= 1:
                    if S[i-1][j-1] == "#":
                        count += 1
                if i >= 1:
                    if S[i-1][j] == "#":
                        count += 1
                if i >= 1 and j <= W-2:
                    if S[i-1][j+1] == "#":
                        count += 1
                if j >= 1:
                    if S[i][j-1] == "#":
                        count += 1
                if j <= W-2:
                    if S[i][j+1] == "#":
                        count += 1
                if j >= 1 and i <= H-2:
                    if S[i+1][j-1] == "#":
                        count += 1
                if i <= H-2:
                    if S[i+1][j] == "#":
                        count += 1
                if i <= H-2 and j <= W-2:
                    if S[i+1][j+1] == "#":
                        count += 1
                ans += str(count)
        print(ans)
resolve()

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,-1,1,-1,0,1]
dy = [1,1,1,0,0,-1,-1,-1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        cnt = 0
        for t in range(8):
            ni=i+dx[t]
            nj=j+dy[t]
            if ni<0 or h<=ni: continue
            if nj<0 or w<=nj: continue
            if s[ni][nj] == '#':
                cnt += 1
        
        s[i][j] = str(cnt)

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

Submission status: Accepted

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

maze = [['.']*(W+2)]
for i in range(H):
    maze.append(list('.'+input()+'.'))
maze.append(['.']*(W+2))

ans = [[0]*W for _ in range(H)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if maze[i][j] == '#':
            ans[i-1][j-1] = '#'
        else:
            for p,q in [[1,0],[-1,0],[0,1],[0,-1],[1,1],[-1,-1],[1,-1],[-1,1]]:
                if maze[i+p][j+q] == '#':
                    ans[i-1][j-1] += 1

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

Submission status: Accepted

Solution code:
h, w = [int(x) for x in input().split()]
s = []
s.append(list("･" * (w + 2)))
for _ in range(h):
  s.append(list("･" + input() + "･"))
  
s.append(list("･" * (w + 2)))

for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j] == "#":
      continue
    
    neighbors = [
      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],
    ]
    s[i][j] = str(len(list(filter(lambda x : x == "#", neighbors))))

for l in s[1:len(s)-1]:
  print(''.join(l[1:len(l)-1]))

Submission status: Accepted

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

nums = [[] for _ in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == "#": nums[i].append("#")
    else:
      t = 0
      for a in range(i-1, i+2):
        if a < 0 or h <= a: continue
        for b in range(j-1, j+2):
          if b < 0 or w <= b: continue
          if s[a][b] == "#": t += 1
      nums[i].append(t)

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(h):
    s.append(list(input()))
a=["+" for i in range(w)]
s.append(a)
s.insert(0,a)
for i in range(h+2):
    s[i].append("+")
    s[i].insert(0,"+")
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]==".":
            m=0
            for x in range(3):
                for y in range(3):
                    if s[i-1+x][j-1+y]=="#":
                        m+=1
            s[i][j]=str(m)
for i in range(1,h+1):
    ans=s[i][1]
    for j in range(w-1):
        ans=ans+s[i][j+2]
    print(ans)

Submission status: Accepted

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

neigh = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        cnt = 0
        flag = False
        if s[i][j] == '.':
            for k in neigh:
                for l in neigh:
                    if k==l==0 or i+k < 0 or i+k >= h or j+l < 0 or j+l >= w:
                        continue
                    elif s[i+k][j+l] == '#':
                        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())
c=['.'*(W+2)]+['.'+input()+'.' for _ in range(H)]+['.'*(W+2)]
ans=[['0']*(W+2) for _ in range(H+2)]
for h in range(1, H+1):
  for w in range(1, W+1):
    if c[h][w]=='#':
      ans[h][w]='#'
    else:
      x=0
      if c[h-1][w-1]=='#':
        x+=1
      if c[h-1][w]=='#':
        x+=1
      if c[h-1][w+1]=='#':
        x+=1
      if c[h][w-1]=='#':
        x+=1
      if c[h][w+1]=='#':
        x+=1
      if c[h+1][w-1]=='#':
        x+=1
      if c[h+1][w]=='#':
        x+=1
      if c[h+1][w+1]=='#':
        x+=1
      ans[h][w]=str(x)
for i in range(1, H+1):
  print(''.join(ans[i][1:-1]))

Submission status: Accepted

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

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue

        tmp = 0
        for t in range(8):
            ni = i + dx[t]
            nj = j + dy[t]
            if ni < 0 or h <= ni: continue
            if nj < 0 or w <= nj: continue
            if s[ni][nj] == '#':
                tmp += 1

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

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input() for i in range(h)]
for i in range(h):
    s[i]=[t.replace(".","0") for t in s[i]]
    s[i]=[t.replace("#","-9") for t in s[i]]
    s[i]=[int(t) for t in s[i]]
for i in range(h):
    for j in range(w):
        if s[i][j]<0:
            if i>0:
                s[i-1][j]+=1
                if j>0:
                    s[i-1][j-1]+=1
                if j<w-1:
                    s[i-1][j+1]+=1
            if i<h-1:
                s[i+1][j]+=1
                if j>0:
                    s[i+1][j-1]+=1
                if j<w-1:
                    s[i+1][j+1]+=1
            if j>0:
                s[i][j-1]+=1
            if j<w-1:
                s[i][j+1]+=1

for i in range(h):
    s[i]=["#" if t<0 else t for t in s[i]]
    s[i]=[str(t) for t in s[i]]
    print("".join(s[i]))

Submission status: Accepted

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

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

for i in range(H):
    S = input()
    for j,s in enumerate(S):
        if s == '#':
            ans[i][j] = '#'
            for a in range(-1,2):
                for b in range(-1,2):
                    if i+a >= 0 and j+b >= 0\
                        and i+a < H and j+b < W\
                        and ans[i+a][j+b] != '#':
                        ans[i+a][j+b] = str(int(ans[i+a][j+b])+1)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 075
# B - Minesweeper

def f(i,j):
    ans[i+1][j+1]+=10**9+7
    ans[i][j]+=1
    ans[i][j+1]+=1
    ans[i][j+2]+=1
    ans[i+1][j]+=1
    # ans[i+1][j+1]+=1
    ans[i+1][j+2]+=1
    ans[i+2][j]+=1
    ans[i+2][j+1]+=1
    ans[i+2][j+2]+=1


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

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

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

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


for i in range (H):
    A=ans[1+i][1:-1]
    for j in range (len(A)):
        if A[j]>10**9:
            A[j]="#"
        else:
            A[j]=str(A[j])
    print("".join(A))

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    dxdy8 = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))

    for x in range(H):
        for y in range(W):
            if S[x][y] == '.':
                res = 0
                for dx, dy in dxdy8:
                    nx, ny = x + dx, y + dy
                    if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == '#':
                        res += 1
                S[x][y] = str(res)

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

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#n = int(input())
h, w = map(int,input().split())
#l = list(map(int,input().split()))
l = [list(input()) for i in range(h)]
def possible(i, j):
    if i == -1 or i == h:
        return False
    if j == -1 or j == w:
        return False    
    return True

for i in range(h):
    for j in range(w):
        count = 0
        if l[i][j] == '.':
            if possible(i-1, j) and l[i-1][j] == '#':
                count += 1
            if possible(i+1, j) and l[i+1][j] == '#':
                count += 1
            if possible(i, j+1) and l[i][j+1] == '#':
                count += 1
            if possible(i, j-1) and l[i][j-1] == '#':
                count += 1
            if possible(i-1, j+1) and l[i-1][j+1] == '#':
                count += 1
            if possible(i+1, j+1) and l[i+1][j+1] == '#':
                count += 1
            if possible(i-1, j-1) and l[i-1][j-1] == '#':
                count += 1
            if possible(i+1, j-1) and l[i+1][j-1] == '#':
                count += 1
            l[i][j] = str(count)
for li in l:
    print("".join(li))

Submission status: Accepted

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

a = [[0 for _ in range(w)] for _ in range(h)]

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

#print(a)

for i in range(h):
    for j in range(w):
        if(a[i][j] == "#"):
            continue
        else:
            cnt = 0
            for k in [-1,0,1]:
                for l in [-1,0,1]:
                    if(k == 0 and l == 0):
                        continue
                    else:
                        x = i + k
                        y = j + l 
                        if(0<= x < h and 0<= y < w and a[x][y] == "#"):
                            cnt += 1
            
            a[i][j] = str(cnt)


#print(a)

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

Submission status: Accepted

Solution code:
Y_DIR=[-1,0,1]
X_DIR=[-1,0,1]
def count_bomb(y,x):
  cnt=0
  for yd in Y_DIR:
    yy=y+yd
    if not (0<=yy<h):
      continue
    for xd in X_DIR:
      xx=x+xd
      if not (0<=xx<w):
        continue
      if s[yy][xx]=="#":
        cnt+=1
  return cnt

h,w=map(int,input().split())
s=[]
for i in range(h):
  s.append(input())
  
ans=["" for _ in range(h)]
for y in range(h):
  for x in range(w):
    if s[y][x] == ".":
      c=count_bomb(y,x)
      ans[y]+=str(c)
    else:
      ans[y]+=s[y][x]

print("\n".join(ans))

Submission status: Accepted

Solution code:
#6re
h,w = map(int,input().split())
s = []
s.append(list("." for i in range(w+2)))
for i in range(h):
    s.append(list("."+input()+"."))
s.append(list("." for i in range(w+2)))
ans = [[0 for j in range(w) ] for i in range(h) ]
    
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] != "#":
            for m in range(i-1,i+2):
                for n in range(j-1,j+2):
                    if s[m][n] == "#":
                        ans[i-1][j-1] += 1
        else:
            ans[i-1][j-1] = "#"
                
for i in range(h):
    for j in range(w):
        ans[i][j] = str(ans[i][j])
        
for i in range(h):
    print("".join(ans[i]))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [input() for _ in range(H)]
A = [[0]*W for _ in range(H)]
B = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            A[i][j] = "#"
for i in range(H):
    for j in range(W):
        if A[i][j] != "#":
            for dx,dy in B:
                if 0 <= j+dx <= W-1 and 0 <= i+dy <= H-1:
                    if A[i+dy][j+dx] == "#":
                        A[i][j] += 1
for i in A:
    print(*i, sep = "")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [input() for _ in range(H)]
A = []
for _ in range(H):
    A.append([0]*W)
B = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            A[i][j] = "#"
for i in range(H):
    for j in range(W):
        if A[i][j] != "#":
            for dx,dy in B:
                if 0<=j+dx<=W-1 and 0<=i+dy<=H-1:
                    if A[i+dy][j+dx] == "#":
                        A[i][j] += 1
for i in range(H):
    for j in range(W):
        print(A[i][j], end = "")
    print("")

Submission status: Accepted

Solution code:
H, W = list(map(int, input().split()))
S_list = []
for i in range(H):
    S_list.append(input())
for i in range(H):
    for j in range(W):
        m = S_list[i][j]
        if m == '#':
            print(m, end='')
        else:
            sum = 0
            if i != 0 and j != 0 and S_list[i-1][j-1] == '#': sum += 1
            if i != 0 and S_list[i-1][j] == '#': sum += 1
            if i != 0 and j != W-1 and S_list[i-1][j+1] == '#': sum += 1
            if j != 0 and S_list[i][j-1] == '#': sum += 1
            if j != W-1 and S_list[i][j+1] == '#': sum += 1
            if i != H-1 and j != 0 and S_list[i+1][j-1] == '#': sum += 1
            if i != H-1 and S_list[i+1][j] == '#': sum += 1
            if i != H-1 and j != W-1 and S_list[i+1][j+1] == '#': sum += 1
            print(sum, end='')
    print('')

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = [list(input()) for _ in range(h)]
p = [-1,0,1]
for i in range(h):
  for j in range(w):
    count = 0
    for ph in p:
      for pw in p:
        if (ph==0 and pw==0) or not(0<=i-ph<=h-1) or not(0<=j-pw<=w-1):
          continue
        if s[max(i-ph,0)][max(j-pw,0)]=='#':
          count += 1
    if s[i][j]=='.': s[i][j]=str(count)
for si in s:
  print(''.join(si))

Submission status: Accepted

Solution code:
def count_bomb(x, y):
    cnt = 0
    for i in range(x-1, x+2):
        for j in range(y-1, y+2):
            if 0<=i<w and 0<=j<h:
                if s[j][i] == '#':cnt += 1
    return cnt

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
k = [["0"]*w for _ in range(h)]

for i in range(w):
    for j in range(h):
        if s[j][i] == '#':
            k[j][i] = "#"
        else:
            k[j][i] = str(count_bomb(i, j))

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

Submission status: Accepted

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

field=[]
for _ in range(h):
    field.append(list(input()))

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

for i in range(h):
    for j in range(w):
        if field[i][j]=="#":
            continue
        count=0
        for x,y in zip(dx,dy):
            if i+x<0 or i+x>h-1 or j+y<0 or j+y>w-1:
                continue
            if field[i+x][j+y]=="#":
                count+=1
        field[i][j] = str(count)

for f in field:
    print(''.join(f))

Submission status: Accepted

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

ans = []
s=[]
s.insert(0,'.'*(w+2))
for i in range(h):
    s.append('.'+input()+'.')
s.append('.'*(w+2))

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

Submission status: Accepted

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

## 下準備(1)：　H*Wマスの周囲を0で囲む
S.insert(0, "0"*W)
S.append("0"*W)
S = ["0" + s + "0" for s in S]
S = [s.replace("#", "1") for s in S]

## 下準備(2): 8方向をforループで回すためのリストを作る
delta_H = [-1, -1, -1, 0, 0, 1, 1, 1]
delta_W = [-1, 0, 1, -1, 1, -1, 0, 1]


## 実行：　一文字ずつ見ていき、"."の場合、周囲8方向の数字を足し算する
S_new = [""]*H

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j] == ".":
            ans = 0
            
            for k in range(8):
                if S[i + delta_H[k]][j + delta_W[k]] != ".":
                    ans += int(S[i + delta_H[k]][j + delta_W[k]])
                    
            S_new[i-1] += str(ans)
            
        else:
            S_new[i-1] += "#"
            
print(*S_new, sep="\n")

Submission status: Accepted

Solution code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(input())
    return x

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

for i in range(0,H):
    for j in range(0,W):

        if S[i][j] == ".":
            cnt = 0
            for k in range(max(0,i-1),min(i+2,H)):
                for l in range(max(0,j-1),min(j+2,W)):
                    if S[k][l] == "#":
                        cnt += 1
            Scopy[i][j] = str(cnt)

for i in range(0,H):
    print("".join(Scopy[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[[c for c in input()] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            continue
        c=0
        for a,b in [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]:
            if i+a<0 or i+a>h-1 or j+b<0 or j+b>w-1:
                continue
            if s[i+a][j+b]=='#':
                c+=1
        s[i][j]=str(c)
for i in s:
    print(''.join(i))