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: Wrong Answer

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

def check(S,H,W):

    for h in range(H):
        for w in range(W):
            if S[h][w] != "#":
                if h !=0 :
                    if S[h-1][w] == "#":
                        S[h][w] +=1
                if h !=H-1 :
                    if S[h+1][w] == "#":
                        S[h][w] +=1
                if w !=0 :
                    if S[h][w-1] == "#":
                        S[h][w] +=1
                if w !=W-1 :
                    if S[h][w+1] == "#":
                        S[h][w] +=1
#右斜め
                if h !=H-1  and w !=W-1:                    
                    if S[h+1][w+1] == "#":
                        S[h][w] +=1
                if h != 0  and w !=W-1:                    
                    if S[h-1][w+1] == "#":
                        S[h][w] +=1
#左斜め下　左斜め上
                if h !=H-1  and w !=0:                    
                    if S[h+1][w-1] == "#":
                        S[h][w] +=1

                if h != 0  and w !=0:                    
                    if S[h-1][w-1] == "#":
                        S[h][w] +=1

    for a in S:
        print(*a)

def convert(S,H,W):
    
    for h in range(H):
        for w in range(W):
            if S[h][w]==".":
                S[h][w] = 0
    return S




S,H,W = nyu()
S =convert(S,H,W)
check(S,H,W)

Submission status: Wrong Answer

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

for i in range(h):
    for j in range(w):
        cnt = 0
        if map[i][j] == ".":
            for dx, dy in d:
                nx = j + dx
                ny = i + dy
                if 0 <= nx < w and 0 <= ny < h and map[ny][nx] == "#":
                    cnt += 1
        if map[i][j] != "#":
            map[i][j] = cnt

for i in range(h):
    print(*map[i])

Submission status: Wrong Answer

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

for i in range(H):
  for j in range(W):
    if list[i][j] == '#':
      try:
        ans[i][j] = ''
        ans[i][j] = '#'
      except:
        pass
      try:
        ans[i-1][j-1] += 1
      except:
        pass
      try:
        ans[i-1][j] += 1
      except:
        pass
      try:
        ans[i-1][j+1] += 1
      except:
        pass
      try:
        ans[i][j-1] += 1
      except:
        pass
      try:
        ans[i][j+1] += 1
      except:
        pass
      try:
        ans[i+1][j-1] += 1
      except:
        pass
      try:
        ans[i+1][j] += 1
      except:
        pass
      try:
        ans[i+1][j+1] += 1
      except:
        pass

for i in range(H):
  line = ''
  for j in range(W):
    line += str(ans[i][j])
  print(line)

Submission status: Wrong Answer

Solution code:
n,m= map(int, input().split())
a=[list(input()) for i in range(n)]
b=0
for i in range(n):
    for j in range(m):
        if a[i][j]==".":
            if n-1>=i+1 and a[i+1][j-1]=='#': #左下
                b+=1
            if a[i][j-1]=='#': #左
                b+=1
            if i-1>=0 and j-1>=0 and a[i-1][j-1]=='#': #左上
                b+=1
            if a[i-1][j]=='#': #上
                b+=1
            if m-1>=j+1 and a[i-1][j+1]=='#': #右上
                b+=1
            if m-1>=j+1 and a[i][j+1]=='#': #右
                b+=1
            if n-1>=i+1 and m-1>=j+1 and a[i+1][j+1]=='#': #右下
                b+=1
            if n-1>=i+1 and a[i+1][j]=='#': #下
                b+=1
            a[i][j]=b
            b=0
        print(a[i][j], end="")
    print()

Submission status: Wrong Answer

Solution code:
import numpy as np

def check_mass(i,j):
  c=0
  if i>0:
    if j>0:
      if mass[i-1][j-1]=="#":
        c+=1
    if mass[i-1][j]=="#":
        c+=1
    if j<W-1:
      if mass[i-1][j+1]=="#":
        c+=1
  if True:
      if mass[i][j-1]=="#":
        c+=1
      if mass[i][j]=="#":
          c+=1
      if j<W-1:
        if mass[i][j+1]=="#":
          c+=1
  if i<H-1:
      if mass[i+1][j-1]=="#":
        c+=1
      if mass[i+1][j]=="#":
          c+=1
      if j<W-1:
        if mass[i+1][j+1]=="#":
          c+=1
  return c

H,W= list(map(int, input().split()))
mass= []
for i in range(H):
  s = list(input()) # stringのままだと、replaceが出来ないのでlistに変換
  mass.append(s)

# print(mass)

for i in range(H):
  for j in range(W):
    if mass[i][j] == ".":
      c=check_mass(i,j)
      mass[i][j] = str(c)

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

Submission status: Wrong Answer

Solution code:
import numpy as np

def check_mass(i,j):
  c=0
  if i>0:
    if j>0:
      if mass[i-1][j-1]=="#":
        c+=1
    if mass[i-1][j]=="#":
        c+=1
    if j<W-1:
      if mass[i-1][j+1]=="#":
        c+=1
  if True:
      if mass[i][j-1]=="#":
        c+=1
      if mass[i][j]=="#":
          c+=1
      if j<W-1:
        if mass[i][j+1]=="#":
          c+=1
  if i<H-1:
      if mass[i+1][j-1]=="#":
        c+=1
      if mass[i+1][j]=="#":
          c+=1
      if j<W-1:
        if mass[i+1][j+1]=="#":
          c+=1
  return c

H,W= list(map(int, input().split()))
mass= []
for i in range(H):
  s = list(input()) # stringのままだと、replaceが出来ないのでlistに変換
  mass.append(s)

print(mass)

for i in range(H):
  for j in range(W):
    if mass[i][j] == ".":
      c=check_mass(i,j)
      mass[i][j] = str(c)

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

Submission status: Wrong Answer

Solution code:
import itertools

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

op =[]
for i in itertools.product([-1, 0, 1], repeat=2):
    op.append(i)


for i, s1 in enumerate(S):
    for j, s2 in enumerate(s1):
        if(s2 == '.'):
            counter = 0
            for o in op:
                print(i,j,o)
                if(i == 0 and o[0] == -1):
                    continue
                elif(i == H-1 and o[0] == 1):
                    continue
                elif(j == 0 and o[1] == -1):
                    continue
                elif(j == W-1 and o[1] == 1):
                    continue
                else:
                    if(S[i + o[0]][j + o[1]] == "#"):
                        counter += 1
            ans[i][j] = str(counter)

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

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
l =[]
q =[]
n=[]
l.append("."*(w+2))
for i in range(h):
  l.append("."+ input()+".")
l.append("."*(w+2))
for i in range(1,h+1):
  for t in range(1,w+1):
    if l[i][t]==".":
      q.append(l[i][t-1])
      q.append(l[i][t+1])
      q.append(l[i-1][t-1])
      q.append(l[i-1][t])
      q.append(l[i-1][t+1])
      q.append(l[i+1][t-1])
      q.append(l[i+1][t])
      q.append(l[i+1][t+1])
      z = q.count("#")
      n.append(z)
      q = []
    else:
      n.append("#")
for t in range(0,h):
  c = n[w*t:w*(t+1)]
  c = map(str,c)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
l =[]
q =[]
n=[]
l.append("."*(w+2))
for i in range(h):
  l.append("."+ input()+".")
l.append("."*(w+2))
for i in range(1,h+1):
  for t in range(1,w+1):
    q.append(l[i][t-1])
    q.append(l[i][t+1])
    q.append(l[i-1][t-1])
    q.append(l[i-1][t])
    q.append(l[i-1][t+1])
    q.append(l[i+1][t-1])
    q.append(l[i+1][t])
    q.append(l[i+1][t+1])
    z = q.count("#")
    if z ==0:
      z ="#"
    n.append(z)
    q = []
for t in range(0,h):
  c = n[w*t:w*(t+1)]
  c = map(str,c)
  print(''.join(c))

Submission status: Wrong Answer

Solution code:
h, w = [int(x) for x in input().split()]
matrix = [list(input()) for _ in range(h)]
surrounding_distances = (-1, 0, 1)


def count_bombs(i, j):
    for da in surrounding_distances:
        for db in surrounding_distances:
            if da == db == 0:
                # Not surrounidings.
                continue
            try:
                if matrix[i + da][j + db] == '#':
                    yield True
            except IndexError:
                # The dolphin stand on the edge.
                continue


for i in range(h):
    for j in range(w):
        if matrix[i][j] == '.':
            matrix[i][j] = str(sum(count_bombs(i, j)))

[print(''.join(row)) for row in matrix]

Submission status: Wrong Answer

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

for h in range(H):
    for w in range(W):
        count=0
        if S[h][w]=='#':
            continue
        # left up
        try:
            if w==0:
                pass
            elif S[h-1][w-1]=='#':
                count+=1
        except:
            pass
        # Left
        try:
            if w==0:
                pass
            elif S[h][w-1]=='#':
                count+=1
        except:
            pass

        # Left down
        try:
            if w==0:
                pass
            elif S[h+1][w-1]=='#':
                count+=1
        except:
            pass

        # Up
        try:
            if S[h-1][w]=='#':
                count+=1
        except:
            pass

        # Right up
        try:
            if S[h-1][w+1]=='#':
                count+=1
        except:
            pass

        # Right
        try:
            if w==W-1:
                pass
            elif S[h][w+1]=='#':
                count+=1
        except:
            pass

        # Right down
        try:
            if S[h+1][w+1]=='#':
                count+=1
        except:
            pass

        # Down
        try:
            if S[h+1][w]=='#':
                count+=1
        except:
            pass
        S[h][w]=str(count)

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

Submission status: Wrong Answer

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

for h in range(H):
    for w in range(W):
        count=0
        if S[h][w]=='#':
            continue
        # left up
        try:
            if S[h-1][w-1]=='#':
                count+=1
        except:
            pass
        # Left
        try:
            if S[h][w-1]=='#':
                count+=1
        except:
            pass

        # Left down
        try:
            if S[h+1][w-1]=='#':
                count+=1
        except:
            pass

        # Up
        try:
            if S[h-1][w]=='#':
                count+=1
        except:
            pass

        # Right up
        try:
            if S[h-1][w+1]=='#':
                count+=1
        except:
            pass

        # Right
        try:
            if S[h][w+1]=='#':
                count+=1
        except:
            pass

        # Right down
        try:
            if S[h+1][w+1]=='#':
                count+=1
        except:
            pass

        # Down
        try:
            if S[h+1][w]=='#':
                count+=1
        except:
            pass
        S[h][w]=str(count)

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

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split(' '))
masu = [input() for i in range(H)]
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, -1, -1, -1, 0, 1, 1, 1]
ans = []
for i in range(H):
    a = []
    for j in range(W):
        if (masu[i][j] == '#'):
            a.append('#')
            continue
        cnt = 0
        for d in range(len(dx)):
            x = i + dx[d]
            y = j + dy[d]
            if (x < 0 or H <= x):
                continue
            if (y < 0 or W <= y):
                continue
            if (masu[x][y] == '#'):
                cnt += 1
        a.append(str(cnt))
    ans.append(a)
for b in ans:
    print(b)

Submission status: Wrong Answer

Solution code:
tate, yoko = map(int,input().split())
map_list = []
for t in range(tate):
    map_list.append(list(input()))

new_map_list = [[0] * yoko for i in range(tate)]

try:
    for t in range(tate):
        for y in range(yoko):
            if map_list[t][y] == '#':
                for t_2 in range(t-1, t+2):
                    for y_2 in range(y-1, y+2):
                        new_map_list[t_2][y_2] += 1
except IndexError:
    pass

for t in range(tate):
    for y in range(yoko):
        if map_list[t][y] == '#':
            new_map_list[t][y] = '#'

for i in new_map_list:
    for j in i:
        print(j, end='')
    print()

Submission status: Wrong Answer

Solution code:
def masucnt(masu,i,j):
  s="."
  try:
    s=masu[i][j]
  except:
    s=""
  return 1 if s=="#" else 0

h,w=map(int,input().split())
masu=[""]*h

for i in range(h):
  masu[i]=input().strip()

for i in range(h):
  for j in range(len(masu[i])):
    n=0
    s=masu[i][j]
    if s=="#":
      if j==(len(masu[i])-1):
        print("#")
      else:
        print("#",end="")
    else:
      n+=masucnt(masu,i-1,j-1)
      n+=masucnt(masu,i-1,j)
      n+=masucnt(masu,i-1,j+1)
      n+=masucnt(masu,i,j-1)
      n+=masucnt(masu,i,j+1)
      n+=masucnt(masu,i+1,j-1)
      n+=masucnt(masu,i+1,j)
      n+=masucnt(masu,i+1,j+1)
      if j==(len(masu[i])-1):
        print(n)
      else:
        print(n,end="")

Submission status: Wrong Answer

Solution code:
def masucnt(masu,i,j):
  s="."
  try:
    s=masu[i][j]
  except:
    s=""
  return 1 if s=="#" else 0

h,w=map(int,input().split())
masu=[""]*h

for i in range(h):
  masu[i]=input().strip()

for i in range(h):
  for j in range(len(masu[i])):
    n=0
    s=masu[i][j]
    if s=="#":
      print("#",end="")
    else:
      n+=masucnt(masu,i-1,j-1)
      n+=masucnt(masu,i-1,j)
      n+=masucnt(masu,i-1,j+1)
      n+=masucnt(masu,i,j-1)
      n+=masucnt(masu,i,j+1)
      n+=masucnt(masu,i+1,j-1)
      n+=masucnt(masu,i+1,j)
      n+=masucnt(masu,i+1,j+1)
      print(n,end="")
  print("")

Submission status: Wrong Answer

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())
s = []
for i in range(h):
  s.append(list(input()))
  

for y in range(h):
  for x in range(w):
    
    if s[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 s[ny][nx] == '#':
        cnt += 1
        
      s[y][x] = str(cnt)
      
      
for y in range(h):
  print(''.join(s[y]))

Submission status: Wrong Answer

Solution code:
x,y = [int(i) for i in input().split()]
ans = []
for i in range(x):
	ans.append(list(input()))
for i in range(x):
	for j in range(y):
		if ans[i][j] == '.':
			d = 0
			for k in range(i-1,i+2):
				for l in range(j-1,j+2):
					if 0 <= k and k < x and 0 <= l and l < y:
						if ans[k][l] =='#':
							d += 1
							ans[i][j]=str(d)
for i in ans:
	print(''.join(i))

Submission status: Wrong Answer

Solution code:
# 相対的な座標
dy = [-1, -1, -1, 0, 0, 1, 1, 1]
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
field = []
H, W = map(int, input().split())
for _ in range(H):
    field.append(list(input()))
for y in range(H):
    for x in range(W):
        if field[y][x] == '#':
            continue
        count = 0
        for k in range(8): # 8方向
            nx = x + dx[k]
            ny = y + dy[k]
            if nx < 0 or W <= nx or ny < 0 or H <= ny: # 枠外
                continue
            if field[ny][nx] == '#':
                count += 1
        field[y][x] = str(count)
for h in range(H):
    print(field[h])

Submission status: Wrong Answer

Solution code:
# 下，右，上，左
dy = [1, 0, -1, 0]
dx = [0, 1, 0, -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] == "#":

            cnt = 0

            for k in range(4):

                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

            if cnt == 0:
                print("No")
                exit()

print("Yes")

Submission status: Wrong Answer

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 + dy[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: Wrong Answer

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 + dy[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: Wrong Answer

Solution code:
import math
import string


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


def nCr(n, r):
    return math.factorial(n)//math.factorial(n-r)*math.factorial(r)


h, w = map(int, input().split())
boad = [None]*h
for i in range(h):
    boad[i] = list(input())
# print(boad)

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 boad[i][j] == '.':
            cnt = 0
            for k in range(8):
                x = i+dx[k]
                y = j+dy[k]
                if (x >= 0 and x < h) and (y >= 0 and y > w) and (boad[x][y] == '#'):
                    cnt += 1
                boad[i][j] = str(cnt)
for i in range(h):
    print(''.join(boad[i]))

Submission status: Wrong Answer

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

for i in range(w):
    for j in range(h):
        if s[j][i] == ".":
            c = 0
            for p in range(-1, 2):
                for q in range(-1, 2):
                    if j+p < 0:
                        x = 0
                    elif j+p >= h:
                        x = h-1
                    else:
                        x = j+p
                    
                    if i+q < 0:
                        y = 0
                    elif i+q >= w:
                        y = w-1
                    else:
                        y = i+q
                    if s[x][y]=="#":
                        c += 1
            s[j][i] = str(c)
for i in s:
    print("".join(i))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[input() for i in range(n)]
ans=[[0 for i in range(m)]for c in l] 
for i in range(n):
  for d in range(m):
    if l[i][d]==".":
      ret=0
      for a,b in[[1,1],[1,0],[0,1],[-1,-1],[-1,0],[0,-1],[1,-1],[-1,1]] :
        try:
          if l[i+a][d+b]=="#":ret+=1
        except IndexError:ret+=0
      ans[i][d]=str(ret)
    else:ans[i][d]="#"
for i in range(n):
  s="".join(ans[i])
  print(s)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[input() for i in range(n)]
ans=[[0 for i in range(m)]for c in l] 
for i in range(n):
  for d in range(m):
    if l[i][d]==".":
      ret=0
      for a,b in[[1,1],[1,0],[0,1],[-1,-1],[-1,0],[0,-1],[1,-1],[-1,1]] :
        try:
          if l[i+a][d+b]=="#":ret+=1
        except IndexError:ret+=0
        ans[i][d]=str(ret)
    else:ans[i][d]="#"
for i in range(n):
  s="".join(ans[i])
  print(s)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[input() for i in range(n)]
ans=[[0 for i in range(m)]for c in l] 
for i in range(n):
  for d in range(m):
    if l[i][d]==".":
      ret=0
      for a,b in[[1,1],[1,0],[0,1],[-1,-1],[-1,0],[0,-1],[1,-1],[-1,1]] :
        try:
          if l[i+a][d+b]=="#":ret+=1
        except IndexError:continue
        ans[i][d]=str(ret)
    else:ans[i][d]="#"
for i in range(n):
  s="".join(ans[i])
  print(s)

Submission status: Wrong Answer

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


mx = [0, 1, 1, 1, 0, -1, -1, -1]
my = [1, 1, 0, -1, -1, -1, 0, 1]
for hi in range(h):
    for wi in range(w):
        if s[hi][wi] == '#':
            print('#', end="")
            continue
        cnt = 0
        for mi in range(8):
            nx = wi + mx[mi]
            ny = hi + my[mi]
            if (nx >= w) or (ny >= h):
                continue
            if s[ny][nx] == '.':
                continue
            cnt += 1
        print(cnt, end="")
    print("")

Submission status: Wrong Answer

Solution code:
import math
import string


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


def nCr(n, r):
    return math.factorial(n)//math.factorial(n-r)*math.factorial(r)


h, w = map(int, input().split())
a = [None]*h
for i in range(h):
    a[i] = input()
# print(a)

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

for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            print('#', end="")
        else:
            count = 0
            for k in range(8):
                x = i+dx[k]  # (i,j)の位置からk移動させたときのx座標
                y = j+dy[k]  # (i,j)の位置からk移動させたときのy座標
                if x < 0 or x <= h or y < 0 or y >= w:
                    # x,yが盤面の中に入っていて'#'のとき
                    continue
                if a[x][y] == '#':
                    count += 1
            print(count, end="")
    print('\n', end="")

Submission status: Wrong Answer

Solution code:
H, W = (int(i) for i in input().split(" "))
status = []
for i in range(H):
    status.append([k for k in input()])
    
new_status = [["#" for k in range(W)] for i in range(H)]

for i in range(H):
    for k in range(W):
        if status[i][k] == ".":
            count = 0
            try:
                if status[i - 1][k - 1] == "#":
                    count += 1
            except:
                pass
            try:
                if status[i - 1][k] == "#":
                    count += 1
            except:
                pass
            try:
                if status[i - 1][k + 1] == "#":
                    count += 1
            except:
                pass
            try:
                if status[i][k - 1] == "#":
                    count += 1
            except:
                pass
            try:
                if status[i][k + 1] == "#":
                    count += 1
            except:
                pass
            try:
                if status[i + 1][k - 1] == "#":
                    count += 1
            except:
                pass
            try:
                if status[i + 1][k] == "#":
                    count += 1
            except:
                pass
            try:
                if status[i + 1][k + 1] == "#":
                    count += 1
            except:
                pass
    
            new_status[i][k] = str(count)
      
for i in new_status:
    print("".join(i))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
lis=[["."]*(W+2)]
for i in range(H):
    lis+=[["."]+list(input())+["."]]
lis += [["."]*(W+2)]
print(lis)
for h in range(1, H+1):
    for w in range(1, W+1):
        x=0
        if lis[h][w] == ".":
            if lis[h+1][w-1] == "#":
                x += 1
            if lis[h+1][w] == "#":
                x += 1
            if lis[h+1][w+1] == "#":
                x += 1
            if lis[h][w-1] == "#":
                x += 1
            if lis[h][w+1] == "#":
                x += 1
            if lis[h-1][w-1] == "#":
                x += 1
            if lis[h-1][w] == "#":
                x += 1
            if lis[h-1][w+1] == "#":
                x += 1
            lis[h][w] = x
lis=lis[1:-1]
for i in lis:
    print(*i[1:-1], sep="")

Submission status: Wrong Answer

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

dxy = [(-1,1),(-1,0),(-1,-1),(0,1),(0,-1),(1,1),(1,0),(1,-1)]
for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            continue
        for dx, dy in dxy:
            x = i + dx
            y = j + dy
            if x < 0 or x >= H:
                continue
            if y <= 0 or y >= W:
                continue
            if S[x][y] == '#':
                continue
            answer[x][y] += 1
#print(answer)
for i in answer:
    print(*i,sep='')

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
a = ["." for i in range(w+2)]
dot_map = [a]
for _ in range(h):
    l = list(input())
    l.insert(0, ".")
    l.append(".")
    dot_map.append(l)
dot_map.append(a)

for i in range(1, h+1):
    for j in range(1,w+1):
        if dot_map[i][j] == ".":
            counter = 0
            if dot_map[i-1][j-1] == "#":
                counter += 1
            if dot_map[i-1][j] == "#":
                counter += 1
            if dot_map[i-1][j+1] == "#":
                counter += 1
            if dot_map[i][j-1] == "#":
                counter += 1
            if dot_map[i][j+1] == "#":
                counter += 1
            if dot_map[i+1][j-1] == "#":
                counter += 1
            if dot_map[i+1][j] == "#":
                counter += 1
            if dot_map[i+1][j+1] == "#":
                counter += 1
            dot_map[i][j] = counter

for i in range(1, h+1):
    ans = dot_map[i]
    print(*ans[1:w+1])

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
lis = [list(input()) for i 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):
        count = 0
        if lis[i][j] != "#":
            for k in range(8):
                try:
                    if lis[i + dx[k]][j + dy[k]] == "#":
                        count += 1
                except:
                    pass
            lis[i][j] = str(count)
for ans in lis:
    print("".join(ans))

Submission status: Wrong Answer

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

def checknum(x,y):
    count = 0
    for i in range(-1,2):
        for j in range(-1,2):
            nx = x+i
            ny = y+j
            if 0 <= nx < h and 0 <= ny < w and l[nx][ny] == "#":
                count += 1
    return count

for i in range(h):
    for j in range(w):
        if l[i][j] == ".":
            l[i][j] = checknum(i,j)
for i in range(h):
    for j in range(w):
        print(l[i][j],end="")
    print()

Submission status: Wrong Answer

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

ans = []


def calc(S,i,j):
    n=0
    if (i-1)>=0:
        if S[i-1][j]=="#":
            n += 1
        if (j-1)>=0:
            if S[i-1][j-1]=="#":
                n += 1
        if (j+1)<W:
            if S[i-1][j+1]=="#":
                n += 1
    if (j-1)>=0:
        if S[i][j-1]=="#":
            n += 1
        if (i+1)<H:
            if S[i+1][j-1]=="#":
                n += 1
    if (i+1)<H:
        if S[i+1][j] == "#":
            n += 1
        if (j+1)<W:
            if S[i+1][j+1]=="#":
                n += 1
    if (j+1)<W:
        if S[i][j+1]=="#":
            n += 1
    return n

for i in range(H):
    s = ""
    c = [0]*W
    for j in range(W):
        if S[i][j]==".":
            n = calc(S,i,j)
            c[j]=n
    for k in range(W):
        if c[k]==0:
            s = s+"#"
        else:
            s = s+str(c[k])
    ans.append(s)

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
l=[["."]*(b+2)]+[["."]+list(input())+["."] for _ in range(a)]+[["."]*(b+2)]
for i in range(1,a+1,1):
  for j in range(1,b+1,1):
    if l[i][j]!="#":
      l[i][j]=str(sum(l[x][y]=="#" for x in [i-1,i,i+1] for y in [j-1,j,j+1]))
for i in range(1,a+1,1):
  print("".join(l[i][1:-2]))

Submission status: Wrong Answer

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    dxs=[-1,0,1]
    dys=[-1,0,1]
    h,w=INTM()
    s=['.'*(w+2)]
    ans=[[0]*(w+2) for i in range(h+2)]
    for i in range(h):
        st='.'+STR()+'.'
        s.append(st)
    s.append('.'*(w+2))
    #print(s)
    #print(ans)
    for i in range(1,h+1):
        for j in range(1,w+1):
            if s[i][j]=='#':
                ans[i][j]='#'
                for dx in dxs:
                    for dy in dys:
                        if s[i+dx][j+dy]=='.':
                            #print(ans)
                            ans[i+dx][j+dy]=int(ans[i+dx][j+dy]+1)
    for i in range(1,h+1):
        print(*ans[i][1:w],sep='')
                
        
if __name__ == '__main__':
    do()