Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.  

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).  

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.  

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.  

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310



Sample input:
3 5
.....
.#.#.
.....


Sample output:
11211
1#2#1
11211

Submission status: Accepted

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

result = [[0 for _ in range(W+1)]for _ in range(H+1)]
for i in range(H):
  for j in range(W):
    if(S[i][j] == '#'):
      result[i][j] = '#'
      if(result[i-1][j-1] != '#'):
        result[i-1][j-1] += 1
      if(result[i-1][j] != '#'):
        result[i-1][j] += 1
      if(result[i-1][j+1] != '#'):
        result[i-1][j+1] += 1
      if(result[i][j-1] != '#'):
        result[i][j-1] += 1
      if(result[i][j+1] != '#'):
        result[i][j+1] += 1
      if(result[i+1][j-1] != '#'):
        result[i+1][j-1] += 1
      if(result[i+1][j] != '#'):
        result[i+1][j] += 1
      if(result[i+1][j+1] != '#'):
        result[i+1][j+1] += 1

for r in result[:-1]:
  print(''.join(list(map(str,r[:W]))))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
mapdata=[]
for i in range(h):
  line=(str(input()))
  line2=list(line)
  mapdata.append(line2)
result=[]
k=[]
def get_bomb(i,j):
  count=0
  for x in (i-1,i,i+1):
    for y in (j-1,j,j+1):
      if 0<=x<=h-1 and 0<=y<=w-1:
        if mapdata[x][y]=='#':
          count+=1
  return count
for i in range(h):
  for j in range(w):
    if mapdata[i][j]=='.':
      k.append(str(get_bomb(i,j)))
    else:
      k.append('#')
  result.append(k)
  k=[]
for i in range(len(result)):
  result2=''.join(result[i])
  print(result2)

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

def swap(s,pos):
    s[pos]-=1
    s[pos+1]+=1
    print(s)
    if pos == n-2: return s
    if s[pos+2] == 0: swap(s,pos+1)
    return s


#n = ip()                              #入力整数1つ
h,w= (int(i) for i in input().split())      #入力整数横2つ
#n,x,y = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
a = []

for i in range(h):
    a.append(input())
adj = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        for x in range(-1, 2):
            for y in range(-1, 2):
                if x+y or x*y:
                    if 0 <= i + x < h and 0 <= j + y < w:
                        if a[i + x][j + y] == '#': adj[i][j]+= 1
for i in range(h):
    for j in range(w):
        if a[i][j] == "#": print("#",end='')
        else :print(adj[i][j],end='')
    print()

Submission status: Accepted

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

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

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

for y in range(H):
    for x in range(W):

        if field[y][x] == '#':
            continue

        cnt = 0

        for k in range(8):
            ny = y + dy[k]
            nx = x + dx[k]

            if ny < 0 or H <= ny\
                    or nx < 0 or W <= nx:
                continue

            if field[ny][nx] == '#':
                cnt += 1

        field[y][x] = str(cnt)

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

Submission status: Accepted

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

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

def count_bombs(i, j):
    search_target = []
    if i != 0:
        search_target += field[i-1][max(0,j-1):min(W,j+2)]
    if i != H-1:
        search_target += field[min(H-1,i+1)][max(0,j-1):min(W,j+2)]
    search_target += field[i][max(0,j-1):min(W,j+2)]
    bombs = str(sum(1 if x == '#' else 0 for x in search_target))
    return bombs


for i in range(H):
    for j in range(W):
        if field[i][j] == '.':
            field[i][j] = count_bombs(i, j)

for i in range(H):
    print("".join(field[i]), end='')

Submission status: Accepted

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

for y in range(h):
  for x in range(w):
    if s[y][x] =='.':
      cnt = 0
      for dx, dy in delta:
        if 0 <= x + dx < w and 0<= y + dy < h and s[y + dy][x + dx] == '#':
          cnt += 1
      s[y][x] = str(cnt)

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

Submission status: Accepted

Solution code:
import copy
y,x = map(int,input().split())
maps = []


def f(i,x,maps): # i>0 x >0 
    l = []
    if i > 0  and x > 0:
        try:
            l.append(maps[i-1][x-1])
        except:
            pass
        try:
            l.append(maps[i-1][x])
        except:
            pass
        try:
            l.append(maps[i-1][x+1])
        except:
            pass
        try:
            l.append(maps[i][x-1])        
        except:
            pass
        try:
            l.append(maps[i][x+1])        
        except:
            pass
        try:
            l.append(maps[i+1][x-1])        
        except:
            pass
        try:
            l.append(maps[i+1][x])   
        except:
            pass
        try:
            l.append(maps[i+1][x+1])        
        except:
            pass
    elif i > 0 and x == 0:
        try:
            l.append(maps[i-1][x])
        except:
            pass
        try:
            l.append(maps[i-1][x+1])    
        except:
            pass
        try:
            l.append(maps[i][x+1])        
        except:
            pass
        try:
            l.append(maps[i+1][x])   
        except:
            pass
        try:
            l.append(maps[i+1][x+1])
        except:
            pass
    elif i == 0 and x > 0:
        try:
            l.append(maps[i][x-1])       
        except:
            pass
        try:
            l.append(maps[i][x+1])
        except:
            pass
        try:
            l.append(maps[i+1][x-1])
        except:
            pass
        try:
            l.append(maps[i+1][x])   
        except:
            pass
        try:
            l.append(maps[i+1][x+1])
        except:
            pass
    elif i == 0 and x == 0:
        try:
            l.append(maps[i][x+1])    
        except:
            pass
        try:
            l.append(maps[i+1][x])           
        except:
            pass
        try:
            l.append(maps[i+1][x+1])        
        except:
            pass

    return l


for i in range(y):
    s = str(input())
    maps.append([s[i] for i in range(x)])
maps2 = copy.deepcopy(maps)
for i in range(y):
    for j in range(x):
        r = maps[i][j]
        ll = f(i,j,maps)

        if r == ".":
            k = ll.count("#")
            maps2[i][j] = str(k)

for i in range(y):
    print(''.join(maps2[i]))

Submission status: Accepted

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

delta = [[-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] == '.':
            cnt = 0
            for dx, dy in delta:
                if 0 <= x + dx < H and 0 <= y + dy < W:
                    if S[x+dx][y+dy] == '#':
                        cnt += 1
            S[x][y] = str(cnt)

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

Submission status: Accepted

Solution code:
def check(arr, x, y):
    d = ((0, -1), (0, 1), (1, -1), (1, 0), (1, 1), (-1, -1), (-1, 0), (-1, 1))

    if arr[x][y] == '#':
        return -1
    else:
        cnt = 0
        for dx, dy in d:
            try:
                if (x+dx) >=0 and (y+dy) >= 0:
                    if arr[x + dx][y + dy] == '#':
                        cnt += 1
            except:
                pass
    return cnt    

h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
ans = []
for x in range(h):
    for y in range(w):
        val = check(s, x, y)
        if val == -1:
            ans.append('#')
        else:
            ans.append(val)

for i in range(h*w):
    print(ans[i], end="")
    if (i+1)%w == 0:
        print()

Submission status: Accepted

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

# print(s)

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

for i in range(h):
    temp = []
    for j in range(w):
        count = 0
        if s[i][j] == '#':
            temp.append('#')
            continue
        for k in range(len(dx)):
            xx = j + dx[k]
            yy = i + dy[k]
            if (xx < 0) or (xx > w - 1) or (yy < 0) or (yy > h - 1):
                continue
            # print(xx, yy)
            if s[yy][xx] == '#':
                count += 1
        temp.append(str(count))
    t.append(temp)
for i in range(h):
    row = ''
    for x in t[i]:
        row += x
    print(row)

Submission status: Accepted

Solution code:
from sys import stdin
h, w = [int(x) for x in stdin.readline().rstrip().split()]
matrix = [stdin.readline().rstrip() for _ in range(h)]
for i in range(h):
    matrix[i] = [matrix[i][x:x+1] for x in range(len(matrix[i]))]
    matrix[i] = [0 if x == '.' else x for x in matrix[i]]
for i in range(h):
    for j in range(w):
        if matrix[i][j] == '#':
            for n in range(-1, 2):
                for m in range(-1, 2):
                    i_n = i + n
                    j_m = j + m
                    if h > i_n >= 0 and w > j_m >= 0 :
                    	if matrix[i_n][j_m] != '#':
                        	matrix[i_n][j_m] += 1
for i in range(h):
  for j in range(w):
  	print('{}'.format(matrix[i][j]), end = '')
  print('')

Submission status: Accepted

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

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if A[i][j] == '.':
            cnt = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if A[i + k][j + l] == '#':
                        cnt += 1
            A[i][j] = str(cnt)
            
for i in range(1, h + 1):
    print(''.join(A[i][1:w + 1]))

Submission status: Accepted

Solution code:
import sys
size=input()
size=size.split()
H=int(size[0])
W=int(size[1])
string_input=[]
mine_state=[]
no_state=[]
count_list=[[0 for i in range(W)] for j in range(H)]
dx=[1,0,-1,0,1,1,-1,-1]
dy=[0,1,0,-1,1,-1,1,-1]
for num in range(H):
    s=input()
    string_input.append(s)
for i in range(H):
    for j in range(W):
        if string_input[i][j]=='.':
            no_state.append((i,j))
        else:
            mine_state.append((i,j))
for i in range(H):
    for j in range(W):
        if (i,j) in no_state:
            count=0
            for d in range(8):
                if (i+dx[d],j+dy[d]) in  mine_state:
                    count+=1
                count_list[i][j]=count
        else:
            count_list[i][j]='#'
for i in range (H):
    for j in range(W):
        sys.stdout.write(str(count_list[i][j]))
    print()

Submission status: Accepted

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

# mapping

for line in range(1, H + 1):
    for row in range(1, W + 1):
        mapper = Smap[line - 1][row - 1]
        map[line][row] = mapper

for line in range(1, H + 1):
    for row in range(1, W + 1):

        bomb_counter = 0
        for counter_line in range(-1, 2):
            for counter_row in range(-1, 2):
                if counter_line == 0 and counter_row == 0:
                    pass
                elif map[counter_line + line][counter_row + row] == '#':
                    bomb_counter += 1
        if map[line][row] != '#':
            map[line][row] = str(bomb_counter)

for i in range(1, len(map)):
    print('.'.join(map[i]).replace('.', ''))

Submission status: Accepted

Solution code:
int_h, int_w = map(int, input().split())
l_h = list()
for i in range(int_h):
    l_h.append(input())

l_temp = list()
l_temp.append(list(map(int, "0" * (int_w + 2))))
for r in l_h:
    l_temp.append(list(map(lambda x: int(x) if x != "#" else x, "0" + r.replace(".", "0") + "0")))

l_temp.append(list(map(int, "0" * (int_w + 2))))


# print(l_temp)


for rownum, row in enumerate(l_temp):
    for colnum, col in enumerate(row):
        if col == "#":
            for c in [-1, 0, 1]:
                if l_temp[rownum - 1][colnum + c] != "#":
                    l_temp[rownum - 1][colnum + c] += 1
                if l_temp[rownum + 1][colnum + c] != "#":
                    l_temp[rownum + 1][colnum + c] += 1
            for c in [-1, 1]:
                if l_temp[rownum][colnum + c] != "#":
                    l_temp[rownum][colnum + c] += 1


for row in l_temp[1:-1]:
    print("".join(map(str,row[1:-1])))

Submission status: Accepted

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


for i in range(h):
  for j in range(w):
    count = 0 
    if s[i][j]==".":
      for k in [[0,1],[1,0],[-1,0],[0,-1],[1,1],[-1,-1],[1,-1],[-1,1]]:
        if 0<=k[0]+i<h and  0<=k[1]+j<w:
          if s[k[0]+i][k[1]+j]=="#":
            count = count +1
      s[i] = s[i][0:j]+str(count)+s[i][j+1:w]
      
for i in range(h):
  for j in range(w):
    print(s[i][j], end="")
  print()

Submission status: Accepted

Solution code:
[h,w] = [int(i) for i in input().split()]
s = []
for i in range(h):
    s.append(input())
ans1 = []
if h != 1 and w != 1:
    for i in range(h):
        ans = []
  #上下のエラーを排除
        if i != 0 and i != h-1:
            for j in range(w):
            #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j+1]+s[i-1][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i-1][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        elif i == 0:
            for j in range(w):
                #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        else:
            for j in range(w):
        #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j+1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i-1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        ans1.append(ans)
    for i in range(h):
        for j in range(w):
            print(ans1[i][j],end = "")
        print("")

elif h != 1 and w == 1:
    #hは非1でwが1
    ans = []
    for i in range(h):
        if i !=0 and i != h-1:
            if s[i][0] == ".":
                ans.append((s[i-1][0]+s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        elif i == 0:
            if s[i][0] == ".":
                ans.append((s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        else:
            if s[i][0] == ".":
                ans.append((s[i-1][0]).count("#"))
            else:
                ans.append(s[i][0])

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

elif h == 1 and w != 1:
    #hは1でwが非1
    ans = []
    for j in range(w):
        if j != 0 and j!= w-1:
            if s[0][j] == ".":
                ans.append((s[0][j-1]+s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        elif j == 0:
            if s[0][j] == ".":
                ans.append((s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        else:
            if s[0][j] == ".":
                ans.append((s[0][j-1]).count("#"))
            else:
                ans.append(s[0][j])
    for j in range(w):
        print(ans[j],end = "")
else:
    ans = []
    if s[0][0] == ".":
        ans.append(0)
    else:
        ans.append(s[0][0])
    print(ans[0])

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


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

def rangeCheck(row,col):
    return row >= 0 and row <= H-1 and col >= 0 and col <= W-1

diff_row = [-1,-1,-1,0,0,1,1,1]
diff_col = [-1,0,1,-1,1,-1,0,1]

table = [list(str(input())) for _ in range(H)]

for row in range(H):
    for col in range(W):
        if table[row][col] != '.':continue
        count = 0
        for i in range(8):
            adj_row = row+diff_row[i]
            adj_col = col+diff_col[i]
            if rangeCheck(adj_row,adj_col) == False:continue
            if table[adj_row][adj_col] == '#':count += 1

        table[row][col] = str(count)

for row in range(H):
    for col in range(W):
        print("%c"%(table[row][col]),end="")
    print("")

Submission status: Accepted

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

from itertools import product
def num(i,j):
    c=0
    for p,q in product([-1,0,1],repeat=2):
        if 0<=i+p<H and 0<=j+q<W and S[i+p][j+q]=='#':
            c+=1
    return c

for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            S[i][j]=str(num(i,j))
    print(''.join(S[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input().replace(".","0")) for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      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]!="#":
              s[i+k][j+l]=str(int(s[i+k][j+l])+1)
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())
L = []
blank = ['.' for _ in range(W+2)]
L.append(blank)

def cnt(x):
    if x == '#':
        return 1
    else:
        return 0

for _ in range(H):
    L.append(['.']+list(input())+['.'])
L.append(blank)

for i in range(1,H+1):
    for j in range(1,W+1):
        if L[i][j] == '#':
            print('#',end='')
        else:
            c = 0
            for x in [-1,0,1]:
                for y in [-1,0,1]:
                    if not (x==y==0):
                        c += cnt(L[i+x][j+y])
            print(c,end='')
    print()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
L = []
blank = ['.' for _ in range(W+2)]
L.append(blank)
def cnt(x):
    if x == '#':
        return 1
    else:
        return 0

for _ in range(H):
    L.append(['.']+list(input())+['.'])
L.append(blank)
for i in range(1,H+1):
    for j in range(1,W+1):
        if L[i][j] == '#':
            print('#',end='')
        else:
            c = cnt(L[i-1][j])+cnt(L[i][j-1])+cnt(L[i+1][j])+cnt(L[i][j+1])+cnt(L[i-1][j-1])+cnt(L[i+1][j-1])+cnt(L[i+1][j+1])+cnt(L[i-1][j+1])
            print(c,end='')
    print()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s0 = [list(input()) for _ in range(h)]
s = [["0"]*(w+2)]+[["0"]+s0[i]+["0"] for i in range(h)]+[["0"]*(w+2)]
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == ".":
            s[i][j] = "{0}".format([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]].count("#"))
for i in range(1, h+1):
    print(''.join(s[i][1:w+1]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
from collections import defaultdict
data=defaultdict(int)
for y in range(h):
    line=input()
    for x in range(w):
        if line[x]=='#':
            data[(y,x)]=-1
            for i in range(-1,2):
                for j in range(-1,2):
                    if data[(y+i,x+j)]!=-1:
                        data[(y+i,x+j)]+=1
for y in range(h):
    s=''
    for x in range(w):
        if data[(y,x)]==-1:s+='#'
        else:s+=str(data[(y,x)])
    print(s)

Submission status: Accepted

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

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


def is_bomb(x, y):
    return S[y][x] == "#"

def count_neibor(x, y):
    dxs = [1, -1, 0, 0, 1, 1, -1, -1]
    dys = [0, 0, 1, -1, 1, -1, 1, -1]
    count = 0
    for dx, dy in zip(dxs, dys):
        check_x = x + dx
        check_y = y + dy
        if 0 <= check_x < W and 0 <= check_y < H:
            count += 1 if S[check_y][check_x] == "#" else 0
    return count

result = []
for y in range(H):
    row = []
    for x in range(W):
        if is_bomb(x, y):
            row.append("#")
        else:
            row.append(str(count_neibor(x, y)))
    result.append(row)

for c in result:
    print(''.join(c))

Submission status: Accepted

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

# マップ作成
S.append("."*(W+2))
for i in range(H):
    S.append("."+input()+".")
S.append("."*(W+2))

# サーチング用マップ
search = [[0]*W for i in range(H)]

# 地雷位置確認探査機
dx = [1, 0, -1, 0, 1, 1, -1, -1]
dy = [0, 1, 0, -1, 1, -1, 1, -1]

# サーチ
for y in range(1, H+1):
    for x in range(1, W+1):
        count = 0

        for i in range(8):
                if S[y+dy[i]][x+dx[i]] == "#":
                    count +=1
                search[y-1][x-1] = count

                if S[y][x] == "#":
                    search[y-1][x-1] = "#"

# リストを外して表記
for i in range(H):
    row = list(map(str, search[i]))
    print(''.join(row))

Submission status: Accepted

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

for hh in range(h):
    for ww in range(w):
        if li[hh][ww] == '#':
            print('#', end='')
        else:
            w0 = max(ww - 1, 0)
            w1 = min(ww + 2, w)
            h0 = max(hh - 1, 0)
            h1 = min(hh + 2, h)
            cnt = sum(li[hx][w0: w1].count('#') for hx in range(h0, h1))
            print(cnt, end='')
    print('')

Submission status: Accepted

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

x = [0,1,1,1,0,-1,-1,-1]
y = [1,1,0,-1,-1,-1,0,1]

for i in range(h):
    for j in range(w):
        if a[i][j] == '.':
            a[i][j] = 0
            for k in range(8):
                if (0 <= i+x[k] <= h-1) and (0 <= j+y[k] <= w-1):
                    if a[i+x[k]][j+y[k]] == '#':
                        a[i][j] += 1
            a[i][j] = str(a[i][j])

for aa in a:
    print(''.join(aa))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
h,w = map(int, input().split())
r=[[0 for i in range(h+2)] for j in range(w+2)]
for i in range(0,h):
  s=input()
  for j in range(0,w):
    if s[j]=='#':
      r[j+1][i+1]=-1
      for y in range(-1,2):
        for x in range(-1,2):
          if x==0 and y==0:
            continue
          elif r[j+1+x][i+1+y]!=-1:
            r[j+1+x][i+1+y]+=1
for i in range(0,h):
  mz=""
  for j in range(0,w):
    if r[j+1][i+1]==-1:
      mz=mz+'#'
    else:
      mz=mz+str(r[j+1][i+1])
  print(mz)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
dx=[1,1,0,-1,-1,-1,0,1]
dy=[0,1,1,1,0,-1,-1,-1]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]==".":
            for k in range(8):
                x=i+dx[k]
                y=j+dy[k]
                if 0<=x and x<h and 0<=y and y<w and s[x][y]=="#":
                    cnt+=1
            s[i][j]=str(cnt)
for i in s:
    print(*i,sep="")

Submission status: Accepted

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

    # b[1][1] -> b[0][0], b[0][1], b[0][2], b[1][0], b[1][2], b[2][0], b[2][1], b[2][2]

    ans = []
    for i in range(h):
        e = []
        for j in range(w):
            e.append(0)
        ans.append(e)

    for i in range(h):
        for j in range(w):
            count = 0
            v = b[i][j]
            if v == "#":
                ans[i][j] = '#'
                continue

            for k in range(-1, 2):
                for l in range(-1, 2):
                    if i + k < 0 or i + k >= h:
                        pass
                    elif j + l < 0 or j + l >= w:
                        pass
                    else:
                        if b[i + k][j + l] == "#":
                            count += 1
            ans[i][j] = count

    for l1 in ans:
        for l2 in l1:
            print(l2, end="")
        print("")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(h):
    l = ''
    for j in range(w):
        if S[i][j] == '#':
            l += '#'
        else:
            l += str(sum([T[max(0, j-1):min(w, j+2)].count('#') for T in S[max(0, i-1):min(h, i+2)]]))
    print(l)

Submission status: Accepted

Solution code:
# ABC 075
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0 for i in range(n)]
def db(x): 
    global debug
    if debug: print(x)
debug = False
H,W = getIntList()
S = zeros(H+2)
S[0] = '.'*(W+2)
for i in range(H): S[i+1] = '.' + input() + '.'
S[H+1] = '.'*(W+2)
db(S)
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=='#': continue
        c = 0
        for k in range(3):
            if S[i-1][j+k-1]=='#': c += 1
        if S[i][j-1]=='#': c += 1
        if S[i][j+1]=='#': c += 1
        for k in range(3):
            if S[i+1][j+k-1]=='#': c += 1
        S[i] = S[i][:j] + str(c) + S[i][j+1:]
    db(S[i])
db(S)
for i in range(H):
    print(S[i+1][1:W+1])

Submission status: Accepted

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

for i in range(h):
    l = ''
    for j in range(w):
        if S[i][j] == '#':
            l += '#'
        else:
            l += str(sum([T[max(0, j-1) : min(w, j+2)].count('#') for T in S[max(0, i-1) : min(h, i+2)]]))
    print(l)

Submission status: Accepted

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

for i in range(h + 2):
    for j in range(w + 2):
        if A[i][j] == '.':
            A[i][j] = 0
            
for i in range(h + 2):
    for j in range(w + 2):
        if A[i][j] == '#':
            if A[i-1][j-1] != '#':
                A[i-1][j-1] += 1
            if A[i-1][j] != '#':
                A[i-1][j] += 1
            if A[i-1][j+1] != '#':
                A[i-1][j+1] += 1
            if A[i][j-1] != '#':
                A[i][j-1] += 1
            if A[i][j+1] != '#':
                A[i][j+1] += 1
            if A[i+1][j-1] != '#':
                A[i+1][j-1] += 1
            if A[i+1][j] != '#':
                A[i+1][j] += 1
            if A[i+1][j+1] != '#':
                A[i+1][j+1] += 1
                
for i in range(1, h + 1):
    print(''.join(map(str, A[i][1:w+1])))

Submission status: Accepted

Solution code:
import numpy as np
#from functools import reduce

def main():
    N, W = list(map(int, input().split()))
    bomb_map = np.zeros((N+2, W+2))
    for i in range(1,N+1):
        bomb_map[i,1:-1] = np.array(list(map(lambda x: 0 if x =='.' else 1, input())))

    new_map = bomb_map[:-2,:-2] + bomb_map[:-2,1:-1] + bomb_map[:-2,2:] \
                + bomb_map[1:-1,:-2] + bomb_map[1:-1,1:-1] + bomb_map[1:-1,2:] \
                    + bomb_map[2:,:-2] + bomb_map[2:,1:-1] + bomb_map[2:,2:]

    new_map = new_map * (bomb_map[1:-1,1:-1] * -2 + 1)

    result = [[str(int(i)) if i >= 0 else '#' for i in l] for l in new_map.tolist()]


    for l in result:
        print("".join(l))


if __name__ == '__main__':
    main()