Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

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

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

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

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

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

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

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

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

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

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

Sample Input 1

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

Sample Output 1

11211
1#2#1
11211

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

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

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

Sample Input 2

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

Sample Output 2

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

It is possible that there is no empty square.  

Sample Input 3

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

Sample Output 3

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



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


Sample output:
11211
1#2#1
11211

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=list(input()for _ in range(h))
dx=[0,1,0,-1,1,1,-1,-1]
dy=[1,0,-1,0,1,-1,1,-1]
for i in range(h):
	for j in range(w):
		if s[i][j]=='.':
			c=0
			for r in range(8):
				x=i+dx[r]
				y=j+dy[r]
				c+=0<=x<h and 0<=y<w and s[x][y]=='#'
			print(c,end='')
		else:print('#',end='')
	print()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
for i in range(h):
    ans = []
    for j in range(w):
        bomb = 0
        if s[i][j] == ".":
            for k in [0, -1, 1]:
                for n in [0, -1, 1]:
                    if i + k < h and j + n < w:
                        if i + k < 0 or j + n < 0:
                            continue
                        if s[i + k][j + n] == "#":
                            bomb += 1
            ans.append(str(bomb))
        else:
            ans.append("#")
    print("".join(ans))

Submission status: Accepted

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

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

#print(s)

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

for i in range(h):
    k = ''
    for j in range(w):
       
        k += s[i][j]
    print(k)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s_list = [input() for i in range(h)]
ans_list = []
for i in range(h):
    ans_line = []
    for j in range(w):
        ans_line.append('#')
    for j in range(w):
        count = 0
        if s_list[i][j] == '#':
            continue
        for dy in range(-1, 2):
            for dx in range(-1, 2):
                if dy == 0 and dx == 0:
                    continue
                ny = dy + i
                nx = dx + j
                if ny < 0 or nx < 0 or ny >= h or nx >= w:
                    continue
                if s_list[ny][nx] == '#':
                    count += 1
        ans_line[j] = count
    ans_list.append(ans_line)

for ans_y in ans_list:
    for ans_x in ans_y:
        print(ans_x, end='')
    print()

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())


H,W = inpl()
map = []
map.append(['.']*(W+2))
for y in range(H):
	map.append(['.'] + list(input()) + ['.'])
map.append(['.']*(W+2))

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

def check(x,y):
	global map
	tmp  = 0
	for yy in range(y-1,y+2):
		for xx in range(x-1,x+2):
			if map[yy][xx] == '#':
				tmp += 1
	return str(tmp)

for y in range(1,H+1):
	for x in range(1,W+1):
		if map[y][x] == '#':
			ans[y-1][x-1] = '#'
		else:
			ans[y-1][x-1] = check(x,y)

for an in ans:
	print(''.join(an))

Submission status: Accepted

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

ans = ['#' * W for i in range(H)]

for i in range(H):
    for j in range(W):
        if mp[i][j] == '#':
            continue
        
        cnt = 0
        for dy in range(-1, 2):
            for dx in range(-1, 2):
                if dx == 0 and dy == 0:
                    continue
                nx = j + dx
                ny = i + dy
                if nx < 0 or ny < 0 or nx >= W or ny >= H:
                    continue
                if mp[ny][nx] == '#':
                    cnt = cnt + 1
            
            ans[i] = ans[i][:j] + str(cnt) + ans[i][j+1:]
            
for i in range(H):
    print (str(ans[i]))

Submission status: Accepted

Solution code:
hw = list(map(int, input().split()))
s = list()
v = list()

for i in range(hw[0]):
    s.append(input())

for j in range(hw[0]):
    for k in range(hw[1]):
        if(s[j][k] != '#'):
            count = 0
            if(j > 0 and k > 0 and s[j-1][k-1] == '#'):
                count += 1
            if(j > 0 and s[j-1][k] == '#'):
                count += 1
            if(j > 0 and k < hw[1]-1 and s[j-1][k+1] == '#'):
                count += 1
            if(k > 0 and s[j][k-1] == '#'):
                count += 1
            if(k < hw[1]-1 and s[j][k+1] == '#'):
                count += 1
            if(j < hw[0]-1 and k > 0 and s[j+1][k-1] == '#'):
                count += 1
            if(j < hw[0]-1 and s[j+1][k] == '#'):
                count += 1
            if(j < hw[0]-1 and k < hw[1]-1 and s[j+1][k+1] == '#'):
                count += 1

            v.append(count)
            
        else:
            v.append(-1)

for l in range(len(v)):
    if(v[l] == -1):
        print("#", end = "")
    else:
        print(v[l], end = "")
        
    if((l+1) % hw[1] == 0):
        print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s_list = []
for i in range(H):
    s_list.append("."+ input()+ ".")
s_list.append("."*(W+2))
s_list.insert(0,"."*(W+2))

result = []
for i in range(1, H+1):
    line = []
    for j in range(1, W+1):
        if s_list[i][j]=="#":
            line.append("#")
            continue
        count = 0
        count += 0 if s_list[i][j-1] == "." else 1
        count += 0 if s_list[i+1][j-1] == "." else 1
        count += 0 if s_list[i+1][j] == "." else 1
        count += 0 if s_list[i+1][j+1] == "." else 1
        count += 0 if s_list[i][j+1] == "." else 1
        count += 0 if s_list[i-1][j+1] == "." else 1
        count += 0 if s_list[i-1][j] == "." else 1
        count += 0 if s_list[i-1][j-1] == "." else 1
        line.append(str(count))
    result.append(line)
for i in range(H):
    print("".join(result[i]))

Submission status: Accepted

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

def check_map(i, j):
    count = 0
    a1=j+1
    a2=j-1
    b1=i+1
    b2=i-1
    if(a2>=0):
        if(map[i][a2]=="#"):
            count+=1
    if(a1<W):
        if(map[i][a1]=="#"):
            count+=1
    if(b2>=0):
        if(map[b2][j]=="#"):
            count+=1
    if(b1<H):
        if(map[b1][j]=="#"):
            count+=1

    if(a2>=0 and b2>=0):
        if(map[b2][a2] == "#"):
            count+=1
    if(a1<W and b2>=0):
        if(map[b2][a1] == "#"):
            count+=1
    if(a1<W and b1<H):
        if(map[b1][a1] == "#"):
            count+=1
    if(a2>=0 and b1<H):
        if(map[b1][a2] == "#"):
            count+=1
    return str(count)

for i in range(H):
    for j in range(W):
        if(map[i][j] == "."):
            map[i][j] = check_map(i, j)

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

Submission status: Accepted

Solution code:
from itertools import product
H, W = map(int, input().split())
S = [['.'] * (W + 2)] + [list('.' + input() + '.') for _ in range(H)] + [['.'] * (W + 2)]
dis = [1, 1, 1, 0, 0, -1, -1, -1]
djs = [-1, 0, 1, -1, 1, -1, 0, 1]
for i, j in product(range(1, H + 1), range(1, W + 1)):
    if S[i][j] == '.':
        around = [S[i + di][j + dj] for di, dj in zip(dis, djs)]
        S[i][j] = str(around.count('#'))

for li in S[1: H + 1]:
    print(''.join(li[1: W + 1]))

Submission status: Accepted

Solution code:
def number(B, i, j):
    bob = 0
    if i-1 != -1:
        for k in range(max(0,j-1),min(W-1,j+1)+1):
            if B[i-1][k] == '#':
                bob += 1
    if j-1 != -1:
        if B[i][j-1] == '#':
            bob += 1
    if j+1 != W:
        if B[i][j+1] == '#':
            bob += 1
    if i+1 != H:
        for k in range(max(0,j-1),min(W-1,j+1)+1):
            if B[i+1][k] == '#':
                bob += 1
    return bob

H, W = map( int, input().split())
B = [input() for _ in range(H)]
S = ['' for _ in range(H)]
for i in range(H):
    for j in range(W):
        if B[i][j] == '#':
            S[i] += '#'
        else:
            S[i] += str(number(B,i,j))
for i in range(H):
    print(S[i])

Submission status: Accepted

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

def main():
    h, w = map(int, input().split())
    mine_list = []
    for _ in range(h):
        mine_list.append(input())
    mine_list_cp = mine_list[:]
    for i in range(h):
        mine_list[i] = '-' + mine_list[i] + '-'
    mine_list.insert(0, '-' * (w+2))
    mine_list.append('-' * (w+2))
    
    for i in range(1, h+1):
        for j in range(1, w+1):
            if mine_list[i][j] != '#':
                cnt = 0
                for k in range(-1, 2):
                    for l in range(-1, 2):
                        if mine_list[i+k][j+l] == '#':
                            cnt += 1
                a = list(mine_list_cp[i-1])
                a[j-1] = str(cnt)
                mine_list_cp[i - 1] = ''.join(a)
        
    for i in mine_list_cp:
        print(i)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def f(i,j,h,w,l):
    c=0
    for m in [-1,0,1]:
        for n in [-1,0,1]:
            if not m and not n:continue
            if 0<=i+m<h and 0<=j+n<w and l[i+m][j+n]=="#":
                c+=1
    return c

h,w=map(int,input().split())
l=[input() for _ in range(h)]
for i in range(h):
    for j in range(w):
        if l[i][j]==".":
            print(f(i,j,h,w,l),end="")
        else:
            print(l[i][j],end="")
    print("")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[input() for _ in range(h)]
ans=[['' for i in range(w)] for j in range(h)]
for i,l in enumerate(s):
    for j,c in enumerate(l):
        if c=='#':
            ans[i][j]='#'
        else:
            cnt = 0
            if i!=0:
                cnt += s[i-1][j]=='#'
            if j!=0:
                cnt += s[i][j-1]=='#'
            if i!=h-1:
                cnt += s[i+1][j]=='#'
            if j!=w-1:
                cnt += s[i][j+1]=='#'
            if i!=0 and j!=0:
                cnt += s[i-1][j-1]=='#'
            if i!=0 and j!=w-1:
                cnt += s[i-1][j+1]=='#'
            if i!=h-1 and j!=0:
                cnt += s[i+1][j-1]=='#'
            if i!=h-1 and j!=w-1:
                cnt += s[i+1][j+1]=='#'
            ans[i][j]=str(cnt)
for l in ans:
    print(''.join(l))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
m = [list(input()) for _ in range(H)]
x = [1, 0, -1, 0, 1, -1, -1, 1]
y = [0, 1, 0, -1, 1, 1, -1, -1]

for i in range(H):
    for j in range(W):
        if(m[i][j] == "#"):
            continue
        count = 0
        for d in range(8):
            nx = j + x[d]
            ny = i + y[d]
            if((nx < 0 or W <= nx) or (ny < 0 or H <= ny)):
                continue
            if(m[ny][nx] == "#"):
                count +=1
        m[i][j]=str(count)
for i in range(H):
    print(''.join(m[i]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
 
x = ["."*(W+2)]
for k in range(H):
    x.append("."+input()+".")
x.append("."*(W+2))

ans =[]
for l in range(1,H+1):
    b =[]
    for k in range(1,W+1):
        if x[l][k]==".":
            sum=0
            aa=x[l+1][k-1:k+2].count("#")+x[l][k-1:k+2].count("#")+x[l-1][k-1:k+2].count("#")
            b.append(aa)
        else:
            b.append('#')
    
    ans.append(b)
for i in ans:
  print(''.join(map(str, i)))

Submission status: Accepted

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

S=[]
fence='.'*W
S.append(fence)
for i in range(H):
    S.append('.'+input()+'.')
S.append(fence)

number_of_bombs=[]
for i in range(H+2):
    number_of_bombs.append([0*i for i in range(W+2)])

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=='#':
            number_of_bombs[i][j]=1000000


for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=='#':
            number_of_bombs[i][j-1]+=1
            number_of_bombs[i][j+1]+=1
            for k in range(j-1, j+2):
                number_of_bombs[i-1][k]+=1
                number_of_bombs[i+1][k]+=1

for i in range(1,H+1):
    line=''
    for j in range(1,W+1):
        if number_of_bombs[i][j]>=1000000:
            line+='#'
        else:
            line+=str(number_of_bombs[i][j])
    print(line)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
diff = [-1, 0, 1]
for i in range(H):
	for j in range(W):
		count = 0
		for x in diff:
			for y in diff:
				if 0 <= i + x < H and 0 <= j + y < W and S[i+x][j+y] == '#':
					count += 1
		if S[i][j] == '.':
			S[i][j] = count
for s  in S:
	print(''.join(map(str, s)))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input())for i 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 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:
h,w=map(int,input().split())
l=[]
d=[-1,0,1]
for i in range(h):
    a=list(input())
    l.append(a)
for i in range(h):
    for j in range(w):
        cnt=0
        for x in d:
            for y in d:
                if 0<=i+x<h and 0<=j+y<w:
                    if l[i+x][j+y]=="#":
                        cnt+=1
        if l[i][j]==".": l[i][j]=cnt
for i in l:
    print("".join(map(str,i)))

Submission status: Accepted

Solution code:
n=input().split()
row=int(n[0])
column=int(n[1])
pounce=[]
for i in range(row):
    lone=input()
    pounce.append([lone[k] for k in range(column)])
for i in range(row):
    for j in range (column):
        count=0
        if pounce[i][j]!='#':
            for z in [i-1,i,i+1]:
                for y in [j-1,j,j+1]:
                    if y>-1 and y<column and z>-1 and z<row:
                        if pounce[z][y]=='#':
                            count+=1
            pounce[i][j]=count
for i in range(row):
    for j in range(column):
        print(pounce[i][j],end='')
    print("")

Submission status: Accepted

Solution code:
H,W=[int(x) for x in str(input()).split()]
R=[]
A=[[0 for i in range(W+2)]]
for i in range(H):
  b=str(input())
  c=[0]
  d=[]
  for j in range(W): 
    
    d.append(b[j])
    if b[j]=='.':
      c.append(0)
    else:
      c.append(1)

  c.append(0)
  A.append(c)
  R.append(d)
A.append([0 for i in range(W+2)])

for i in range(0,H):
  for j in range(0+W):
    if R[i][j]=='.':

      R[i][j]=(A[i][j]+A[i][j+1]+A[i][j+2]+A[i+1][j]+A[i+1][j+2]+A[i+2][j]+A[i+2][j+1]+A[i+2][j+2])

for i in R:
  s=''
  for j in range(W):
    s+=str(i[j])
  print(s)

Submission status: Accepted

Solution code:
hw=input().split()
h=int(hw[0])
w=int(hw[1])
mx=[1,1,0,-1,-1,-1,0,1]
my=[0,1,1,1,0,-1,-1,-1]
grid=[]
for t in range(h):
	row=list(input())
	grid.append(row)
for i in range(w):
	for j in range(h):
		if grid[j][i]==".":
			ctr=0
			for k in range(8):
				adx=i+mx[k]
				ady=j+my[k]
				if adx>=0 and adx<w and ady>=0 and ady<h:
					if grid[ady][adx]=="#":
						ctr+=1
			grid[j][i]=str(ctr)
for l in range(h):
    print(''.join(grid[l]))

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
grid = [[0]*w for _ in range(h)]
for i in range(h):
  s = input()
  for j in range(len(s)):
    if s[j]=="#":
      grid[i][j]="#"
      for dx in range(-1, 2):
        for dy in range(-1, 2):
          if dx==0 and dy==0:
            continue
          nx, ny = j+dx, i+dy
          if 0<=nx<w and 0<=ny<h and grid[ny][nx]!="#":
            grid[ny][nx]+=1
for i in range(h):
  print("".join(map(str, grid[i])))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
src = [input() for i in range(H)]
dxy = [(0,1),(1,0),(0,-1),(-1,0),(-1,1),(1,1),(1,-1),(-1,-1)]
ans = [[] for i in range(H)]
for i in range(H):
    for j in range(W):
        if src[i][j] == '#':
            ans[i].append('#')
            continue
        bomb = 0
        for dx,dy in dxy:
            x,y = dx+j,dy+i
            if not 0 <= x < W: continue
            if not 0 <= y < H: continue
            if src[y][x] == '#':
                bomb += 1
        ans[i].append(str(bomb))
for row in ans:
    print(*row,sep='')

Submission status: Accepted

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

for i in range(1, H+1):
    for j in range(1, W+1):
        x = []
        if S[i][j] == '.':
            #周囲を全探索
            for di in range(-1, 2):
                for dj in range(-1, 2):
                    x.append(S[i+di][j+dj])
            print(x.count('#'), end="")
        else:
            print('#', end="")
    print("")

Submission status: Accepted

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

for i in range(1, H+1):
    for j in range(1, W+1):
        x = []
        if S[i][j] == '.':
            #周囲を全探索
            for di in range(-1, 2):
                for dj in range(-1, 2):
                    x.append(S[i+di][j+dj])
            print(x.count("#"), end="")
        else:
            print('#', end="")
    print("\n")

Submission status: Accepted

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

for i in range(1, H+1):
    for j in range(1, W+1):
        x = []
        if S[i][j] == '.':
            x.append(S[i-1][j-1])
            x.append(S[i-1][j])
            x.append(S[i-1][j+1])
            x.append(S[i][j-1])
            x.append(S[i][j])
            x.append(S[i][j+1])
            x.append(S[i+1][j-1])
            x.append(S[i+1][j])
            x.append(S[i+1][j+1])
            print(x.count("#"), end="")
        else:
            print('#', end="")
    print("\n")

Submission status: Accepted

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

S=[]

for i in range(H):
    tmp=list(input())
    S.append(tmp)

ope=[-1,0,1]

for i in range(H):
    ans=[]
    for j in range(W):
        if S[i][j]=="#":
            ans=ans+["#"]
        else:
            c=0
            for k in ope:
                for l in ope:
                    y=i+k
                    x=j+l
                    if x>-1 and x<W and y>-1 and y<H:
                        #print(y,x)
                        if S[y][x]=="#":
                            c+=1
            ans=ans+list(str(c))
    ans2="".join(ans)
    print(ans2)

Submission status: Accepted

Solution code:
from itertools import product
H, W = map(int, input().split())
a = []
for i in range(H): a.append(list(input()))
for i in range(H):
    for j in range(W):
        b = 0
        if a[i][j] == '#':continue
        for k,l in product([-1,0,1], repeat=2):
            if k == l == 0:continue
            h = i + k
            w = j + l
            if h < 0 or h >= H: continue
            if w < 0 or w >= W: continue
            if a[h][w] == '#':b+=1
        a[i][j] = b

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

Submission status: Accepted

Solution code:
H,W=[int(i) for i in input().split()]
A=[]
for i in range(H):
    s=input()
    S=list(s)
    A.append(S)
for i in range(H):
    for j in range(W):
        if A[i][j]!="#":            
            s=0
            for k in range(-1,2):
                for l in range(-1,2):
                    if k==0 and l==0:
                        continue
                    if i+k>=0 and i+k<=H-1 and j+l>=0 and j+l<=W-1:
                        if A[i+k][j+l]=="#":
                            s+=1
            A[i][j]=str(s)
for i in range(H):
    for j in range(W):
        print(A[i][j],end="")
    print()

Submission status: Accepted

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

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

# 8方向を見に行く用の配列を準備しておく
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]

# 各マスを周回
for i in range(H):
    for j in range(W):
        # 今いるマスが#なら何もしない
        if HW[i][j] == "#":
            continue
        # 8方向について#を数えていく
        cnt = 0
        for k in range(8):
            # 各方向にずらした位置を保持
            ni = i + dy[k]
            nj = j + dx[k]
            # index範囲外の時も何もしない
            if ni < 0 or ni >= H:
                continue
            if nj < 0 or nj >= W:
                continue
            
            if HW[ni][nj] == "#":
                cnt += 1
        HW[i][j] = cnt
# 1行ずつ文字列に変換して出力
for i in range(H):
    print("".join(map(str, (HW[i]))))

Submission status: Accepted

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

for i in range(H):
    ans = ""
    for j in range(W):
        cnt = 0
        if s[i][j] == "#":
            ans += "#"
            continue
        if i > 0 and j > 0 and s[i-1][j-1] == "#":
            cnt += 1
        if i > 0 and s[i-1][j] == "#":
            cnt += 1
        if i > 0 and j < W-1 and s[i-1][j+1] == "#":
            cnt += 1
        if j > 0 and s[i][j-1] == "#":
            cnt += 1
        if j < W-1 and s[i][j+1] == "#":
            cnt += 1
        if i < H-1 and j > 0 and s[i+1][j-1] =="#":
            cnt += 1
        if i < H-1 and s[i+1][j] =="#":
            cnt += 1
        if i < H-1 and j < W-1 and s[i+1][j+1] =="#":
            cnt += 1
        ans += str(cnt)
    print(ans)

Submission status: Accepted

Solution code:
from itertools import product

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

T = [[0 for _ in range(W)] for _ in range(H)]
for i, j in product(range(H), range(W)):
    if S[i][j] == '.':
        continue
    for di, dj in product([-1, 0, 1], [-1, 0, 1]):
        if di == 0 and dj == 0:
            T[i][j] = '#'
        else:
            if 0 <= i + di < H and 0 <= j + dj < W:
                if T[i + di][j + dj] != '#':
                    T[i + di][j + dj] += 1
for i in range(H):
    print(''.join([str(x) for x in T[i]]))

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S= [input().strip() for i in range(H)]
ans=[[0]*W for i in range(H)]
diff=[(-1, -1),(0, -1),(1, -1),
     (-1, 0),           (1, 0),
     (-1, 1),(0, 1),(1, 1),]
for i in range(H):
    for j in range(W):
        if S[i][j]!="#":
            ct=0
            for dh,dw in diff:
                if 0<=i+dh<H and 0<=j+dw<W:
                    ct+=S[i+dh][j+dw]=="#"
            ans[i][j]=str(ct)
        else:
            ans[i][j]="#"
print("\n".join(["".join(x)for x in ans]))

Submission status: Accepted

Solution code:
H,W=map(int,raw_input().split())
s=[raw_input() for r in xrange(H)]
# s[h][w]
dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,1,1,1,0,-1,-1,-1]
for r in xrange(H):
  buf = []
  for c in xrange(W):
    if s[r][c] == '#':
      buf.append('#')
    else:
      cnt = 0
      for i in xrange(8):
        ri = r + dx[i]
        ci = c + dy[i]
        if (0 <= ri < H) and (0 <= ci < W):
          if s[ri][ci] == '#':
            cnt += 1
      buf.append(str(cnt))
  print ''.join(buf)

Submission status: Accepted

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

h, w = map(int, input().split())
S = ['.'*(w+2)] + [('.' + input() + '.') for _ in range(h)] + ['.'*(w+2)]
T = []

directions = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
for i in range(1, h+1):
    string = ''
    for j in range(1, w+1):
        count = 0
        if S[i][j] == '.':
            for k in directions:
                if S[i+k[0]][j+k[1]] == '#':
                    count += 1
            string += str(count)
        else:
            string += '#'
    T.append(string)

for t in T:
    print(t)

Submission status: Accepted

Solution code:
# input()はint型で直接入力できないのか？
height, width = input().split()
# print(height, width)

# boardに各行の要素を代入する
# boardの初期化
board = []
# boardに各行の文字列をリスト化したリストを代入する
for i in range(int(height)):
    s = list(input())
    board.append(s)

# タプルはイミュータブルなので変更できない
# x軸y軸方向の座標　右下左上斜め右上斜め右下斜め左下斜め左上
dx = (1, 0, -1, 0, 1, -1, -1, 1)
dy = (0, 1, 0, -1, 1, 1, -1, -1)

for i in range(int(height)):
    for j in range(int(width)):
        if board[i][j] == '#':
            continue

        num = 0
        # dx,dyをfor文で回して'.'周りの爆弾の個数を調べる
        for d in range(8):
            ni = i + dy[d]
            nj = j + dx[d]
            # 大事な分岐
            if ni < 0 or int(height) <= ni:
                continue
            if nj < 0 or int(width) <= nj:
                continue
            if board[ni][nj] == '#':
                num += 1

        board[i][j] = str(num)

for i in range(int(height)):
    print(''.join(board[i]))

Submission status: Accepted

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

for i in range(h):
    row = ''
    for j in range(w):
        cnt = 0
        if s[i][j] == '#':
            row += '#'
            continue
        if i > 0 and j > 0 and s[i-1][j-1] == '#':
            cnt += 1
        if i > 0 and s[i-1][j] == '#':
            cnt += 1
        if i > 0 and j < w - 1 and s[i-1][j+1] == '#':
            cnt += 1
        if j > 0 and s[i][j-1] == '#':
            cnt += 1
        if j < w - 1 and s[i][j+1] == '#':
            cnt += 1
        if i < h - 1 and j > 0 and s[i+1][j-1] == '#':
            cnt += 1
        if i < h - 1 and s[i+1][j] == '#':
            cnt += 1
        if  i < h - 1 and j < w - 1 and s[i+1][j+1] == '#':
            cnt += 1
        row += str(cnt)
    print(row)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [[[] for i in range(W + 2)] for j in range(H + 2)]
for i in range(H):
    S[i + 1][1: W + 1] = list(input()[:])
for i in range(H):
    for j in range(W):
        if S[i + 1][j + 1] == '.':
            print([S[i][j], S[i][j+1], S[i][j+2], S[i+1][j], S[i+1][j+2],\
                    S[i+2][j], S[i+2][j+1], S[i+2][j+2]].count('#'), end = '')
        else:
            print('#', end = '')
    print('')

Submission status: Accepted

Solution code:
from itertools import product
H, W = map(int, input().split())
S = [["."] * (W + 2)]
for _ in range(H):
    s = ["."] + list(input()) + ["."]
    S.append(s)
S.append(["."] * (W + 2))

ans = []
deltas = list(product((0, 1, -1), (0, 1, -1)))
del deltas[0]


def count(S, i, j):
    if i <= 0 or i >= H + 1 or j <= 0 or j >= W + 1:
        return "error"
    c = 0
    for d_i, d_j in deltas:
        if S[i + d_i][j + d_j] == "#":
            c += 1
    return c


def print_grid(G):
    for i in range(len(G)):
        print("".join(G[i]))
    return


for i in range(1, H + 1):
    ans_row = []
    for j in range(1, W + 1):
        if S[i][j] == "#":
            ans_row.append("#")
        else:
            ans_row.append(str(count(S, i, j)))
    ans.append(ans_row)

# print_grid(S)
print_grid(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split());S=[input()for _ in[0]*H];print('\n'.join(''.join([str(sum(s[max(0,w-1):min(W,w+2)].count('#')for s in S[max(0,h-1):min(H,h+2)])),'#'][S[h][w]=='#']for w in range(W))for h in range(H)))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input()for _ in[0]*H]
cells = [[0]*(W+2)for _ in[0]*(H+2)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            for k in range(3):
                for l in range(3):
                    if cells[i+k][j+l] != '#':
                        cells[i+k][j+l] += 1
            cells[i+1][j+1] = '#'
print('\n'.join(''.join(map(str,row[1:W+1]))for row in cells[1:H+1]))