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:
if __name__ == '__main__':
    h, w = map(int, input().split())
    maiz = []
    count =[]
    for i in range(h):
        a = input()
        maiz.append(a)
        count.append([0]*w)

    for i in range(h):
        for j in range(w):
            if maiz[i][j] == "#":
                count[i][j] = "#"
                if i > 0 and maiz[i-1][j] == ".":
                    count [i-1][j] +=1
                if i < h -1  and maiz[i+1][j] == ".":
                    count [i+1][j] +=1
                if j > 0 and maiz[i][j-1] == ".":
                    count [i][j-1] +=1
                if j < w -1  and maiz[i][j+1] == ".":
                    count [i][j+1] +=1
                if i > 0 and j > 0 and maiz[i-1][j-1] == ".":
                    count [i-1][j-1] +=1
                if i < h -1 and j < w-1  and maiz[i+1][j+1] == ".":
                    count [i+1][j+1] +=1
                if i > 0 and j < w-1 and maiz[i-1][j+1] == ".":
                    count [i-1][j+1] +=1
                if i < h -1 and j >0  and maiz[i+1][j-1] == ".":
                    count [i+1][j-1] +=1


    for i in count:
        ans = ""
        for j in i :
            ans += str(j)
        print(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[list(input()+'.') for i in range(H)]
S.append(['.']*(W+1))
a=[[0]*W for i in range(H)]
for y in range(H):
    for x in range(W):
        if S[y][x]=='.':
            a[y][x]=len([1 for i in range(-1,2) for j in range(-1,2) if S[i+y][j+x]=='#'])
        else: a[y][x]='#'
for i in a:print(''.join(list(map(str,i))))

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())


h,w = readInts()
dist = [['0'] * w for _ in range(h)]
maze = [input() for _ in range(h)]
dx = [-1,-1,-1,0,0,1,1,1]
dy = [-1,0,1,-1,1,-1,0,1]

def bfs(y,x):
    dist[y][x] = '#'
    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if not(0 <= nx < w and 0 <= ny < h):
            continue
        if maze[ny][nx] == '#':
            continue
        dist[ny][nx] = str(int(dist[ny][nx]) + 1)


for y in range(h):
    for x in range(w):
        if maze[y][x] == '#':
            bfs(y,x)
for i in range(h):
    print("".join(dist[i]),sep = '')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
A = [['#' for i in range(W)] for j in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            a = 0
            if i > 0 and j > 0:
                if S[i - 1][j - 1] == '#':
                    a += 1
            if i > 0:
                if S[i - 1][j] == '#':
                    a += 1
            if i > 0 and j < W - 1:
                if S[i - 1][j + 1] == '#':
                    a += 1
            if j > 0:
                if S[i][j - 1] == '#':
                    a += 1
            if j < W - 1:
                if S[i][j + 1] == '#':
                    a += 1
            if i < H - 1 and j > 0:
                if S[i + 1][j - 1] == '#':
                    a += 1
            if i < H - 1:
                if S[i + 1][j] == '#':
                    a += 1
            if i < H - 1 and j < W - 1:
                if S[i + 1][j + 1] == '#':
                    a += 1
            A[i][j] = a
for i in range(H):
    ans = [str(a) for a in A[i]]
    ans = ''.join(ans)
    print(ans)

Submission status: Accepted

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

bom = []
for i in range(H):
    for j in range(W):
        if s[i][j] == ".": s[i][j] = 0
        else: bom.append([i,j])

for i in range(len(bom)):
    h,w = bom[i][0], bom[i][1]
    for x,y in ([1,0], [-1,0], [0,1], [0,-1], [1,1], [-1,-1], [1,-1], [-1,1]):
        new_h, new_w =  h+x, w+y
        if new_h<0 or new_h>=H or new_w<0 or new_w>=W: continue
        if s[new_h][new_w] != "#":
            s[new_h][new_w] += 1
for ans in s:
    print("".join(list(map(str, ans))))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = ["."*(W+2)]
for i in range(H):
    x=input().strip()
    x = "."+x+"."
    S.append(x)
S.append("."*(W+2))
A = [[0 for _ in range(W+1)] for _ in range(H+1)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=="#":
            A[i][j] = "#"
        else:
            cnt = 0
            if S[i][j+1]=="#":
                cnt += 1
            if S[i-1][j+1]=="#":
                cnt += 1
            if S[i-1][j]=="#":
                cnt += 1
            if S[i-1][j-1]=="#":
                cnt += 1
            if S[i][j-1]=="#":
                cnt += 1
            if S[i+1][j-1]=="#":
                cnt += 1
            if S[i+1][j]=="#":
                cnt += 1
            if S[i+1][j+1]=="#":
                cnt += 1
            A[i][j]=str(cnt)
    print("".join(A[i][1:W+1]))

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        cnt = 0
        for ii, jj in zip(di, dj):
            ni = i + ii
            nj = j + jj
            if 0 <= ni < h and 0 <= nj < w:
                if s[ni][nj] == '#':
                    cnt += 1
        s[i][j] = chr(ord('0') + cnt)

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

Submission status: Accepted

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

T = [[0]*(W+2) for _ in range(H+2)]

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            T[i+1][j+1] = 9

for i in range(1,H+1):
    for j in range(1,W+1):
        if T[i][j] >= 9:
            T[i-1][j-1] += 1
            T[i-1][j+0] += 1
            T[i-1][j+1] += 1
            T[i+0][j-1] += 1
            T[i+0][j+0] += 1
            T[i+0][j+1] += 1
            T[i+1][j-1] += 1
            T[i+1][j+0] += 1
            T[i+1][j+1] += 1

for i in range(1,H+1):
    ans = ""
    for j in range(1,W+1):
        if T[i][j] >= 9:
            T[i][j] = "#"
        ans += str(T[i][j])
    print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    h,w = I2()
    s = [L() for _ in range(h)]
    dx = [1,0,-1,0,1,-1,-1,1]
    dy = [0,1,0,-1,1,1,-1,-1]
    x,y = 0,0
    for i in range(h):
        for j in range(w):
            if s[i][j] == "#":continue
            cnt = 0
            for k in range(8):
                x = j+dx[k]
                y = i+dy[k]
                if (y < 0 or h <= y):continue
                if (x < 0 or w <= x):continue
                if s[y][x] == "#":cnt += 1
            s[i][j] = cnt
    for i in s:
        for j in i:
            print(j,end="")
        print()

Submission status: Accepted

Solution code:
Height, Width = map(int, input().split())

minse=[]

for i in range(Height):
    mine = list(input())
    minse.append(mine)

ans_list = [[0 for j in range(Width)] for i in range(Height)]


for h in range(Height):
    for w in range(Width):
        me = minse[h][w]
        if (me == '#'):

            for i in range(3):
                for j in range(3):
                    if (i != 1 or j != 1):
                        index_h = h - 1 + j
                        index_w = w - 1 + i
                        if (index_h < Height and index_h >=0 and index_w>=0 and index_w < Width):
                            ans_list[index_h][index_w] += 1
                            

for i in range(Height):
    for j in range(Width):
        value = minse[i][j]
        if (value == '#'):
            ans_list[i][j]='#'

for i in range(Height):
    tmp = ans_list[i]
    print(''.join(map(str,tmp)))

Submission status: Accepted

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

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

        cnt = 0
        for d in range(8):
            xi = i + dx[d]
            yj = j + dy[d]

            if xi < 0 or xi > h-1 or yj < 0 or yj > w-1:
                continue

            if field[xi][yj] == '#':
                cnt += 1
        
        field[i][j] = cnt

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

Submission status: Accepted

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

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]
#    S = []
#    for _ in range(N):
#        S.append(list(input()))

H,W= map(int,input().split()) 
S = []
S.append(['.']*(W+2))
for _ 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] == "#":
            print("#", end='')
        else:
            Stmp = 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]
            print(Stmp.count("#"), end='')
    print()

Submission status: Accepted

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

ans = [[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      ans[i][j] = "#"
      for i2 in range(i-1, i+2):
        if i2 < 0 or h-1 < i2: continue
        for j2 in range(j-1, j+2):
          if (j2 < 0 or w-1 < j2): continue
          elif ans[i2][j2] == "#": continue
          ans[i2][j2] += 1
for i in ans:
  print("".join(map(str,i)))

Submission status: Accepted

Solution code:
import sys

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())


def main():
    H, W = mi()
    S = ['w'*(W+2)]+['w'+input()+'w' for i in range(H)]+['w'*(W+2)]
    ans = [[0]*W for i in range(H)]

    for j in range(1, W+1):
        for i in range(1, H+1):
            if S[i][j] == '#':
                ans[i-1][j-1] = '#'
            else:
                t = S[i-1][j-1]+S[i-1][j]+S[i-1][j+1] \
                +S[i][j-1]+S[i][j+1] \
                +S[i+1][j-1]+S[i+1][j]+S[i+1][j+1]
                ans[i-1][j-1] = str(t.count('#'))
    print(*(''.join(ans[i]) for i in range(H)), sep='\n')



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    H,W = map(int,input().split())
    S = list()
    INF = 10**18
    S.append([-INF]*(W+2))
    for i in range(H):
        line = list(input())
        for j,c in enumerate(line):
            if c == '.':
                line[j] = 0
            else:
                line[j] = -INF

        S.append([-INF]+line+[-INF])
    S.append([-INF]*(W+2))
    for i in range(1,H+1):
        for j in range(1,W+1):
            if S[i][j] < 0:
                S[i-1][j-1] += 1
                S[i-1][j] += 1
                S[i-1][j+1] += 1
                S[i][j-1] += 1
                S[i][j+1] += 1
                S[i+1][j-1] += 1
                S[i+1][j] += 1
                S[i+1][j+1] += 1
    
    out = ''
    for i in range(1,H+1):
        for j in range(1,W+1):       
            out += '#' if S[i][j] < 0 else str(S[i][j])
        out += '\n'
    print(out)
                
main()

Submission status: Accepted

Solution code:
###==================================================
### import
#import bisect
#from collections import Counter, deque, defaultdict
#from copy import deepcopy
#from functools import reduce, lru_cache
#from heapq import heappush, heappop
#import itertools
#import math
#import string
import sys
### stdin
def input(): return sys.stdin.readline()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInM1(): return map(int1, input().split())
def iInLH(): return list(map(int, input().split()))
def iInLH1(): return list(map(int1, input().split()))
def iInLV(n): return [iIn() for _ in range(n)]
def iInLV1(n): return [iIn()-1 for _ in range(n)]
def iInLD(n): return [iInLH() for _ in range(n)]
def iInLD1(n): return [iInLH1() for _ in range(n)]
def sInLH(): return list(input().split())
def sInLV(n): return [input().rstrip('\n') for _ in range(n)]
def sInLD(n): return [sInLH() for _ in range(n)]
### stdout
def OutH(lst, delimiter=' '): print(delimiter.join(map(str, lst)))
def OutV(lst): print('\n'.join(map(str, lst)))
### setting
sys.setrecursionlimit(10 ** 6)
### utils
int1 = lambda x: int(x) - 1
### constants
INF = int(1e9)
MOD = 1000000007
dx = (-1, 0, 1, 0)
dy = (0, -1, 0, 1)
###---------------------------------------------------

N_AROUND = 8
ddx = (-1, 0, 1, -1, 1, -1, 0, 1)
ddy = (-1, -1, -1, 0, 0, 1, 1, 1)

H, W = iInM()
S = sInLV(H)

## 周りに空きマスを入れる
T = [['.'] * (W + 2)]
for i in range(H):
    var = ['.'] + list(S[i]) + ['.']
    T.append(var)
T.append(['.'] * (W + 2))
#OutV(T)

ans = []
for i in range(1, H+1):
    ans.append([])
    for j in range(1, W+1):
        if T[i][j] == '#':
            ans[-1].append('#')
            continue
        cnt = 0
        for k in range(N_AROUND):
            if T[i + ddy[k]][j + ddx[k]] == '#':
                cnt += 1
        ans[-1].append(str(cnt))
        #print(ans)
#OutV(ans)

for i in range(H):
    OutH(ans[i], delimiter='')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
a, b = [], []
ans = 0
for _ in range(H):
    a += [0 if x == '.' else 1 for x in input()]
for i in range(H*W):
    if a[i] == 0:
        if i % W != 0: ans += a[i-1]
        if (i+1) % W != 0: ans += a[i+1]
        if i < (H-1)*W: ans += a[i+W]
        if i < (H-1)*W and i % W != 0: ans += a[i+W-1]
        if i < (H-1)*W and (i+1) % W != 0: ans += a[i+W+1]
        if W <= i: ans += a[i-W]
        if i % W != 0 and W <= i: ans += a[i-W-1]
        if W <= i and (i+1) % W != 0: ans += a[i-W+1]
        b += [str(ans)]
        ans = 0
    else:
        b += ['#']
        
for i in range(H):
    print(''.join(b[W*i:W*(i+1)]))

Submission status: Accepted

Solution code:
import sys
H,W = map(int,input().split())
array = []
for I in range(H):
    array.append(list(map(str,input())))

if not (1 <= H <= 50 and 1 <= W <= 50):
    sys.exit()

result = [[0]*(W+2) for i in range((H+2))]
# test_result = [[0,1,2,3,4,5],[6,7,8,9,10,11],[12,13,14,15,16,17],[18,19,20,21,22,23]]
def input_result_array(x,y,result):
    result[x-1][y-1] += 1
    result[x-1][y] += 1
    result[x-1][y+1] += 1
    result[x][y-1] += 1
    result[x][y+1] += 1
    result[x+1][y-1] += 1
    result[x+1][y] += 1
    result[x+1][y+1] += 1
    return result

bomb_point = []
for I in range(H):
    for J in range(W):
        if array[I][J] == '#':
            bomb_point.append([I,J])
            result = input_result_array(I+1,J+1,result)

for I in range(len(bomb_point)):
    x,y=bomb_point[I]
    result[x+1][y+1] = '#'

str_result = ''
for I in [I[1:-1] for I in result[1:-1]]:
    for J in I:
        str_result += str(J)
    str_result += '\n'
print(str_result)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

H,W = I()
maze = [list(s()) for _ in range(H)]
ans = [[0]*(W-1) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            cnt = 0
            Flag1 = 0
            Flag2 = 0
            Flag3 = 0
            Flag4 = 0
            if i != 0:
                if maze[i-1][j] == '#':
                    cnt += 1
                Flag1 = 1
            if i != H-1:
                if maze[i+1][j] == '#':
                    cnt += 1
                Flag2 = 1
            if j != 0:
                if maze[i][j-1] == '#':
                    cnt += 1
                Flag3 = 1
            if j != W-1:
                if maze[i][j+1] == '#':
                    cnt += 1
                Flag4 = 1
            if Flag1 == Flag3 == 1:
                if maze[i-1][j-1] == '#':
                    cnt += 1
            if Flag1 == Flag4 == 1:
                if maze[i-1][j+1] == '#':
                    cnt += 1
            if Flag2 == Flag3 == 1:
                if maze[i+1][j-1] == '#':
                    cnt += 1
            if Flag2 == Flag4 == 1:
                if maze[i+1][j+1] == '#':
                    cnt += 1
            maze[i][j] = str(cnt)
for m in maze:
    print(''.join(m))

Submission status: Accepted

Solution code:
from collections import defaultdict

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

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

for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      if i!=0:
        if j-1>=0:
          if s[i-1][j-1]==".":
            s[i-1][j-1] = 1
          elif s[i-1][j-1]!="#":
            s[i-1][j-1]+=1
        if s[i-1][j]==".":
          s[i-1][j] = 1
        elif s[i-1][j]!="#":
          s[i-1][j]+=1
        if j+1<=w-1:
          if s[i-1][j+1]==".":
            s[i-1][j+1] = 1
          elif s[i-1][j+1] != "#":
            s[i-1][j+1]+=1
      if j-1>=0:
        if s[i][j-1]==".":
          s[i][j-1] = 1
        elif s[i][j-1]!="#":
          s[i][j-1]+=1
      if j+1<=w-1:
        if s[i][j+1]==".":
          s[i][j+1] = 1
        elif s[i][j+1] != "#":
          s[i][j+1]+=1
      if i+1<=h-1:
        if j-1>=0:
          if s[i+1][j-1]==".":
            s[i+1][j-1] = 1
          elif s[i+1][j-1]!="#":
            s[i+1][j-1]+=1
        if s[i+1][j]==".":
          s[i+1][j] = 1
        elif s[i+1][j]!="#":
          s[i+1][j]+=1
        if j+1<=w-1:
          if s[i+1][j+1]==".":
            s[i+1][j+1] = 1
          elif s[i+1][j+1] != "#":
            s[i+1][j+1]+=1

for i in s:
  for j in i:
    if j==".":
      print("0",end="")
    else:
      print(j,end="")
  print()

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            cnt = 0
            for _i in range(max(0, i - 1), min(H, i + 2)):
                for _j in range(max(0, j - 1), min(W, j + 2)):
                    if S[_i][_j] == '#':
                        cnt += 1
            S[i][j] = str(cnt)
print('\n'.join([''.join(s) for s in S]))

Submission status: Accepted

Solution code:
def main():
  h,w = list(map(int,input().split()))
  s = [input() for i in range(h)]
  for i in range(0,h):
    for j in range(0,w):
      if s[i][j]=='#':
        print('#',end="")
      else:
        count=0
        for k in range(i-1,i+2):
          if k>=0 and k<h:
            for l in range(j-1,j+2):
              if l>=0 and l<w and s[k][l]=='#':
                count+=1
        print(count,end="")
    print()
    
main()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
s=[['.']*(W+2) for i in range(H+2)]
for i in range(H):
  S=input()
  t='.'+S+'.'
  s[i+1]=t
for i in range(H):
  for j in range(W):
    b=0
    if s[i+1][j+1]!='#':
      for k in range(-1,2,1):
        for l in range(-1,2,1):
          if s[i+1+k][j+1+l]=='#':
            b+=1
    else:
      b='#'
    print(b,end='')
  print()

Submission status: Accepted

Solution code:
import copy
ym,xm=map(int,input().split())
ym-=1
xm-=1
field=[]
for i in range(ym+1):
  field.append(list(input()))
ans=copy.deepcopy(field)
def check(x,y):
  ret=0
  chx=[0]
  chy=[0]
  if x!=0:
    chx.append(-1)
  if x!=xm:
    chx.append(1)
  if y!=0:
    chy.append(-1)
  if y!=ym:
    chy.append(1)
  for px in chx:
    for py in chy:
      if px==0 and py==0:
        continue
      if field[y+py][x+px]=="#":
        ret+=1
  return ret

for x in range(xm+1):
  for y in range(ym+1):
    if field[y][x]=="#":
      continue
    ans[y][x]=check(x,y)

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

Submission status: Accepted

Solution code:
dy = [-1, 1, 0, 0, -1, 1, -1, 1]
dx = [ 0, 0,-1, 1, -1, 1,  1,-1]
h, w = map(int, input().split())
S = [[*input()] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == '#': continue
        c = 0
        for k in range(8):
            y = i + dy[k]; x = j + dx[k]
            if y<0 or h<=y or x<0 or w<=x: continue
            if S[y][x] == '#': c += 1
        S[i][j] = str(c)
print(*map(''.join, S), sep='\n')

Submission status: Accepted

Solution code:
dy = [-1, 1, 0, 0, -1, 1, -1, 1]
dx = [ 0, 0,-1, 1, -1, 1,  1,-1]
h, w = map(int, input().split())
S = [input() for _ in range(h)]
ans = [[-1]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == '#': continue
        ans[i][j] = 0
        for k in range(8):
            y = i + dy[k]; x = j + dx[k]
            if y<0 or h<=y or x<0 or w<=x: continue
            if S[y][x] == '#': ans[i][j] += 1
for i in range(h):
    for j in range(w): print('#' if ans[i][j] < 0 else ans[i][j], end='')
    print('')

Submission status: Accepted

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

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

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

for i in range(h):
    for j in range(w):
        if S[i][j] == '#': continue
        ans[i][j] = 0
        for k in range(8):
            y = i + dy[k]
            x = j + dx[k]
            if y<0 or h<=y: continue
            if x<0 or w<=x: continue
            if S[y][x] == '#':
                ans[i][j] += 1

for i in range(h):
    for j in range(w):
        if ans[i][j] == -1:
            print('#',end='')
        else:
            print(ans[i][j],end='')
    print('')

# print(h,w)
# print(*S,sep='\n')
# print(*ans,sep='\n')

Submission status: Accepted

Solution code:
h, w = map(int, input().split()) #hが行(x)、wが列(y)
maze = [input() for i in range(h)]
bomb = [[0] * w for _ in range(h)]

for x in range(h):
    for y in range(w):
        if maze[x][y] == "#":
            bomb[x][y] = "#"
            continue

        for dx, dy in [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]:
            nx = x + dx
            ny = y + dy 
            if not 0 <= nx < h or not 0 <= ny < w:
                continue
            if maze[nx][ny] == "#":
                bomb[x][y] += 1
               



for i in range(h):
    ans = ""
    for j in range(w):     
        ans +=str( bomb[i][j])
    print(ans)

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
L = [[x if x=='#' else 0 for x in input()] for _ in range(H)]

for i in range(H):
  l = L[i]
  l_before=None
  l_after=None
  if i>0:
    l_before=L[i-1]
  if i<H-1:
    l_after=L[i+1]
    
  for j in range(W):
    if l[j] != '#':
      try:
        if j>=1 and l_before[j-1]=='#':
          l[j]+=1
      except:
        pass
      try:
        if l_before[j]=='#':
          l[j]+=1
      except:
        pass
      try:
        if l_before[j+1]=='#':
          l[j]+=1
      except:
        pass
      try:
        if j>=1 and l[j-1]=='#':
          l[j]+=1
      except:
        pass
      try:
        if l[j+1]=='#':
          l[j]+=1
      except:
        pass
      
      try:
        if j>=1 and l_after[j-1]=='#':
          l[j]+=1
      except:
        pass
      try:
        if l_after[j]=='#':
          l[j]+=1
      except:
        pass
      try:
        if l_after[j+1]=='#':
          l[j]+=1
      except:
        pass


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

Submission status: Accepted

Solution code:
import itertools
H,W = map(int,input().split())
S = [input() for h in range(H)]
a = [[0 if cell=="." else "#" for cell in row] for row in S]

for h in range(H):
  for w in range(W):
    if S[h][w]!="#":
      continue
    for dx,dy in itertools.product([-1,0,1],repeat=2):
      x = h+dx
      y = w+dy
      if x<0 or H<=x:
        continue
      if y<0 or W<=y:
        continue
      if S[x][y]=="#":
        continue
      a[x][y]+=1

for row in a:
  print(*row,sep="")

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	B
# CreatedDate:  2020-06-25 15:41:57 +0900
# LastModified: 2020-06-25 16:11:34 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    h, w = map(int, input().split())
    s = []
    s.append((w+2)*'.')
    for _ in range(h):
        s.append('.'+input()+'.')
    s.append((w+2)*'.')
#    print(s)
    cnt = [[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][j] == '#':
                continue
            if s[i-1][j-1] == '#':
                cnt[i][j] += 1
            if s[i-1][j] == '#':
                cnt[i][j] += 1
            if s[i-1][j+1] == '#':
                cnt[i][j] += 1
            if s[i][j-1] == '#':
                cnt[i][j] += 1
            if s[i][j+1] == '#':
                cnt[i][j] += 1
            if s[i+1][j-1] == '#':
                cnt[i][j] += 1
            if s[i+1][j] == "#":
                cnt[i][j] += 1
            if s[i+1][j+1] == '#':
                cnt[i][j] += 1

    for i in range(1, h+1):
        for j in range(1, w+1):
            if s[i][j] == '#':
                print('#', end='')
                continue
            print(cnt[i][j], end='')
        print()






if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s = [["."for i in range(w+2)]]
for i in range(h):s.append(["."]+list(input())+["."])
s.append(["."]*(w+2))
l=[]
for i in range(1,h+1):
 l2=[]
 for j in range(1,w+1):
  if s[i][j]=="#":l2.append("#")
  else:l2.append(str([s[i+1][j],s[i+1][j+1],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]].count("#")))
 l.append("".join(l2))
print(*l,sep="\n")

Submission status: Accepted

Solution code:
def bakudanda (y,x):
  if y < 0 or y >= len(s):
    return 0
  if x < 0 or x >= len(s[y]):
    return 0
  if s[y][x] == '#':
    return 1
  else:
    return 0
H,W = map(int,input().split())
s=[]
for i in range(H):
  s.append(input())
  
for i in range(len(s)):
  a = s[i]
  fp = ''
  for j in range(len(a)):
    if a[j] == '.':
      c = 0
      ds = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0,1), (1, -1), (1, 0), (1, 1)]
      for (y, x) in ds:
        c+= bakudanda(i+y,j+x)
      fp += str(c)
    else:
      fp += '#'
  print(fp)

Submission status: Accepted

Solution code:
def bakudanda (S,y,x):
  if y < 0 or y >= len(S):
    return 0
  if x < 0 or x >= len(S[y]):
    return 0
  if S[y][x] == '#':
    return 1
  else:
    return 0
H,W = map(int,input().split())
s=[]
for i in range(H):
  s.append(input())
  
for i in range(len(s)):
  a = s[i]
  fp = ''
  for j in range(len(a)):
    if a[j] == '.':
      c = 0
      ds = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0,1), (1, -1), (1, 0), (1, 1)]
      for (y, x) in ds:
        c+= bakudanda(s,i+y,j+x)
      fp += str(c)
    else:
      fp += '#'
  print(fp)

Submission status: Accepted

Solution code:
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):
        num_mine = 0
        if l[i][j] != "#":
            if 0<i and 0<j and l[i-1][j-1] == "#":
                num_mine += 1
            if 0<i and l[i-1][j] == "#":
                num_mine += 1
            if 0<i and j<w-1 and l[i-1][j+1] == "#":
                num_mine += 1
            if 0<j and l[i][j-1] == "#":
                num_mine += 1
            if j<w-1 and l[i][j+1] == "#":
                num_mine += 1
            if i<h-1 and 0<j and l[i+1][j-1]== "#":
                num_mine += 1
            if i<h-1 and l[i+1][j] == "#":
                num_mine += 1
            if i<h-1 and j<w-1 and l[i+1][j+1] == "#":
                num_mine += 1
            l[i][j] = str(num_mine) 
for i in range(h):
    print(''.join(l[i]))