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:
# -*- coding:utf-8 -*-
H,W=map(int,input().split())
boxes=list()
pad=[0]*(W+2)
boxes.insert(0,pad)
for i in range(H):
  x=input()
  box=list()
  box.append(0)
  for j in range(W):
    if x[j]=='.':
      box.append(0)
    elif x[j]=='#':
      box.append(1)
  box.append(0) 
  boxes.insert(i+1,box)
boxes.insert(H+1,pad)
answers=list()
padd=[0]*W
for n in range(H):
  answer=list()
  for k in range(W):
    count=0
    if boxes[n][k]==1:
      count+=1
    if boxes[n][k+1]==1:
      count+=1
    if boxes[n][k+2]==1:
      count+=1
    if boxes[n+1][k]==1:
      count+=1
    if boxes[n+1][k+2]==1:
      count+=1
    if boxes[n+2][k]==1:
      count+=1
    if boxes[n+2][k+1]==1:
      count+=1
    if boxes[n+2][k+2]==1:
      count+=1
    if count==0:
      count='#'
    else:
      count=str(count)
    answer.insert(k,count)
  answers.insert(n,answer)
for v in range(H):
  answe=''.join(answers[v])
  print(answe)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
H,W=map(int,input().split())
boxes=list()
pad=[0]*(W+2)
boxes.insert(0,pad)
for i in range(H):
  x=input()
  box=list()
  box.append(0)
  for j in range(W):
    if x[j]=='.':
      box.append(0)
    elif x[j]=='#':
      box.append(1)
  box.append(0) 
  boxes.insert(i+1,box)
boxes.insert(H+1,pad)
print(boxes)
answers=list()
padd=[0]*W
for n in range(H):
  answer=list()
  for k in range(W):
    count=0
    if boxes[n][k]==1:
      count+=1
    if boxes[n][k+1]==1:
      count+=1
    if boxes[n][k+2]==1:
      count+=1
    if boxes[n+1][k]==1:
      count+=1
    if boxes[n+1][k+2]==1:
      count+=1
    if boxes[n+2][k]==1:
      count+=1
    if boxes[n+2][k+1]==1:
      count+=1
    if boxes[n+2][k+2]==1:
      count+=1
    if count==0:
      count='#'
    else:
      count=str(count)
    answer.insert(k,count)
  answers.insert(n,answer)
for v in range(H):
  answe=''.join(answers[v])
  print(answe)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
s.append(input())
c = [[0] * w for i in range(h)]
for i in range(h-1):
    s.append(input())
    for j in range(w):
        p=[]
        if i ==0 and j==0:
            p=s[i][j:j+2]+s[i+1][j:j+2]
        elif i==0 and j>0:
            p=s[i][j-1:j+2]+s[i+1][j-1:j+2]
        elif i>0 and j==0:
            p = s[i-1][j:j+2]+s[i][j:j+2]+s[i+1][j:j+2]
        else:
            p = s[i-1][j-1:j+2]+s[i][j-1:j+2]+s[i+1][j-1:j+2]
        if s[i][j] == '#':
            c[i][j] = '#'
        else:
            c[i][j] = p.count('#')

for j in range(w):
    p=[]
    i=h-1
    if j==0:
        p = s[i-1][j:j+2]+s[i][j:j+2]
    else:
        p = s[i-1][j-1:j+2]+s[i][j-1:j+2]
    if s[i][j] == '#':
        c[i][j] = '#'
    else:
        c[i][j] = p.count('#')
        
for i in range(h):
  for j in range(w):
    print(c[i][j],end="")
  print('',sep='\n')

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
s.append(input())
c = [[0] * w for i in range(h)]
for i in range(h-1):
    s.append(input())
    for j in range(w):
        p=[]
        if i ==0 and j==0:
            p=s[i][j:j+2]+s[i+1][j:j+2]
        elif i==0 and j>0:
            p=s[i][j-1:j+2]+s[i+1][j-1:j+2]
        elif i>0 and j==0:
            p = s[i-1][j:j+2]+s[i][j:j+2]+s[i+1][j:j+2]
        else:
            p = s[i-1][j-1:j+2]+s[i][j-1:j+2]+s[i+1][j-1:j+2]
        if s[i][j] == '#':
            c[i][j] = '#'
        else:
            c[i][j] = p.count('#')
        ##print(i,j,p,c[i][j] )

for j in range(w):
    p=[]
    i=h-1
    if j==0:
        p = s[i-1][j:j+2]+s[i][j:j+2]
    else:
        p = s[i-1][j-1:j+2]+s[i][j-1:j+2]
    if s[i][j] == '#':
        c[i][j] = '#'
    else:
        c[i][j] = p.count('#')
    ##print(i,j,p,c[i][j] )
        
for i in range(h):
  for j in range(w):
    print(c[i][j],end="")
  print('',sep='\n')

Submission status: Wrong Answer

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

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

t = [['']*w for i in range(h)]
print(t.count)

dx = [0, 0, 1, -1, 1, 1, -1, -1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
for y in range(h):
    for x in range(w):
        if s[y][x] == '#':
            t[y][x] = '#'
        else:
            count = 0
            for i in range(8):
                if x + dx[i] < 0 or x + dx[i] >= w or y + dy[i] < 0 or y + dy[i] >= h:
                    continue
                nx = x + dx[i]
                ny = y + dy[i]
                if s[ny][nx] == '#':
                    count += 1
            t[y][x] = str(count)

for i in range(len(t)):
    print(''.join(t[i]))

Submission status: Wrong Answer

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

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

mapping = {'.':0, '#':1}

for i in range(H):
    for j in range(W):
        if Ss[i][j] == '#':
            ans[i][j] = '#'
            continue
        if i>0 and j>0:
            ans[i][j] += mapping[Ss[i-1][j-1]]
        if j>0:
            ans[i][j] += mapping[Ss[i+0][j-1]]
        if i<H-1 and j>0:
            ans[i][j] += mapping[Ss[i+1][j-1]]
        if i>0:
            ans[i][j] += mapping[Ss[i-1][j+0]]
        if i<H-1:
            ans[i][j] += mapping[Ss[i+1][j+0]]
        if i<H-1 and j<W-1:
            ans[i][j] += mapping[Ss[i+1][j+1]]
        if j<W-1:
            ans[i][j] += mapping[Ss[i+0][j+1]]
        if i>0 and j<W-1:
            ans[i][j] += mapping[Ss[i-1][j+1]]
        
for a in ans:
    print(*a)

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
m = []
for y in range(h):
  s = input()
  l = []
  for i in s:
    if i == '#':
      l.append(1)
    else:
      l.append(0)
  m.append(l)

print(m)
for y in range(h):
  s = ''
  for x in range(w):
    bomb = 0
    for i,j in ((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)):
      if y+j < 0 or x+i < 0 or x+i >= w or y+j >= h:
        continue
      if m[y+j][x+i] == 1:
        bomb += 1
    if m[y][x] == 1:
      bomb = '#'
    s += str(bomb)
  print(s)

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
S = [input() for _ in range(h)]
for r in range(h):
    for c in range(w):
        count = 0
        if S[r][c] == '#':
            print('#', end='')
        else:
            count += S[max(0,r-1)][max(0,c-1):min(c+2,w)].count('#')
            count += S[r][max(0,c-1):min(c+2,w)].count('#')
            count += S[min(r+1,h-1)][max(0,c-1):min(c+2,w)].count('#')
            print(count, end='')
    print()

Submission status: Wrong Answer

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

init = []
for i in range(h):
    # x = input()
    init.append([])
    for j in range(w):
        init[i].append(0)

for i in range(h):
    for j in range(w):
        if mass[i][j] != "#":
            # up
            if i + 1 < h and mass[i + 1][j] == "#":
                init[i][j] += 1
                # down
            if i - 1 >= 0 and mass[i - 1][j] == "#":
                init[i][j] += 1
                # right
            if j + 1 < w and mass[i][j + 1] == "#":
                init[i][j] += 1
                # left
            if j - 1 >= 0 and mass[i][j - 1] == "#":
                init[i][j] += 1
            if j + 1 < w and i + 1 < h and mass[i + 1][j + 1] == "#":
                init[i][j] += 1

            if i - 1 >= 0 and j - 1 >= 0 and mass[i - 1][j - 1] == "#":
                init[i][j] += 1

            if i + 1 < h and j - 1 >= 0 and mass[i + 1][j - 1] == "#":
                init[i][j] += 1

            if i - 1 >= 0 and j + 1 < w and mass[i - 1][j + 1] == "#":
                init[i][j] += 1
        else:
            init[i][j] = "#"
for i in range(h):
    for j in range(w):
        print(init[i][j])

Submission status: Wrong Answer

Solution code:
#インポート
import sys

#入力用
def ILI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def ISI(): return map(int, sys.stdin.readline().rstrip().split())
def II(): return int(sys.stdin.readline().rstrip())
def ISS(): return sys.stdin.readline().rstrip().split()
def IS(): return sys.stdin.readline().rstrip()


ans = []
dx = [-1,  0,  1, -1, 1, -1, 0, 1]
dy = [-1, -1, -1,  0, 0,  1, 1, 1]
H, W = ISI()
l = [list(IS()) for i in range(H)]
for i in range(H):
    for j in range(W):
        if l[i][j] != "#":
            cnt = 0
            for d in range(8):
                ni = i + dy[d]
                nj = j + dx[d]
                if 0 > ni or ni >= H: continue
                if 0 > nj or nj >= W: continue
                if l[ni][nj] == "#":
                    cnt += 1
            l[i][j] = cnt
for i in range(H):
    print(*l[i])

Submission status: Wrong Answer

Solution code:
H, W=map (int, input().split(' '))
z={}

for i in range(H):
    z[i]=str(input())



for h in range(H):
    for w in range(W):
        p=0
        for dy in [-1, 0, 1]:
            y=h+dy
            if h + dy < 0 or h + dy >= H:
                continue
            for dx in [-1, 0, 1]:
                x=w+dx
                if x < 0 or x >= W:
                    continue
                if x == w and y ==h:
                    continue
                
                if z[y][x:x+1] == "#":
                    p+=1
        if p==0:
            print('#', end="")
        else:
            print(p,end="")
    print("\n", end="")

Submission status: Wrong Answer

Solution code:
from itertools import product
#%%
def resolve():
    h,w=map(int,input().split())
    A=[list('.'+input()+'.') for _ in range(h)]
    A=[['.']*(w+2)]+A+[['.']*(w+2)]

    for i,j in product(range(1,h+1),range(1,w+1)):
        if A[i][j]=='.':
            A[i][j]=(A[i-1][j-1]=='#')+(A[i-1][j]=='#')+(A[i-1][j+1]=='#')
            +(A[i][j-1]=='#')+(A[i][j+1]=='#')
            +(A[i+1][j-1]=='#')+(A[i+1][j]=='#')+(A[i+1][j+1]=='#')

    for i in range(1,h+1):
        print(*A[i],sep='')
resolve()

Submission status: Wrong Answer

Solution code:
def eight_loc(i,j):
    return [(i-1,j-1),(i,j-1),(i+1,j-1),(i-1,j),(i+1,j),(i-1,j+1),(i,j+1),(i+1,j+1)]

h,w = map(int,input().split())
s = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            loc = eight_loc(i,j)
            bomb_num = 0
            for x,y in loc:
                if x > 0 and y > 0 and x < h and y < w:
                    if s[x][y] == "#":
                        bomb_num += 1
            s[i][j] = str(bomb_num)
for a in s:
    print(''.join(a))

Submission status: Wrong Answer

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

after = []
for i in range(h):
    row = ''
    for j in range(w):
        tmp = ''
        if before[i][j] == '.':
            for i2 in (i-1, i, i+1):
                for j2 in (j-1, j, j+1):
                    if i2 == i and j2 == j:
                        continue
                    else:
                        try:
                            tmp += before[i2][j2]
                        except IndexError:
                            tmp += '.'
            row += str(tmp.count('#'))
        else:
            row += '#'
    after.append(row)

for row in after:
    print(row)

Submission status: Wrong Answer

Solution code:
#全日本汚いコード選手権Pythonの部
H, W = map(int, input().split())
S = []
for i in range(H):
  S.append(list(input()))
L = [[0 for w in range(W)] for h in range(H)]

if H == 1 and W == 1:
  if S[0][0] == ".":
    L[0][0] = 0

elif H == 1:
  if S[0][1]=="#":
    L[0][0]+=1
  if S[0][W-2]=="#":
    L[0][W-1]+=1
  for w in range(1,W-1):
    if S[0][w-1]=="#":
      L[0][w]+=1
    if S[0][w+1]=="#":
      L[0][w]+=1

elif W == 1:
  if S[1][0]=="#":
    L[0][0]+=1
  if S[H-2][0]=="#":
    L[H-1][0]+=1
  for h in range(1,H-1):
    if [h-1][0]=="#":
      L[h][0]+=1
    if S[h+1][0]=="#":
      L[h][0]+=1
  
  
else:

  if S[0][1]=="#":
    L[0][0]+=1
  if S[1][0]=="#":
    L[0][0]+=1
  if S[1][1]=="#":
    L[0][0]+=1
  if S[H-1][1]=="#":
    L[H-1][0]+=1
  if S[H-2][0]=="#":
    L[H-1][0]+=1
  if S[H-2][1]=="#":
    L[H-1][0]+=1
  if S[0][W-2]=="#":
    L[0][W-1]+=1
  if S[1][W-1]=="#":
    L[0][W-1]+=1
  if S[1][W-2]=="#":
    L[0][W-1]+=1
  if S[H-1][W-2]=="#":
    L[H-1][W-1]+=1
  if S[H-2][W-1]=="#":
    L[H-1][W-1]+=1
  if S[H-2][W-2]=="#":
    L[H-1][W-1]+=1

  for w in range(1,W-1):
    if S[0][w-1]=="#":
      L[0][w]+=1
    if S[0][w+1]=="#":
      L[0][w]+=1
    if S[1][w-1]=="#":
      L[0][w]+=1
    if S[1][w]=="#":
      L[0][w]+=1
    if S[1][w+1]=="#":
      L[0][w]+=1
    if S[H-1][w-1]=="#":
      L[H-1][w]+=1
    if S[H-1][w+1]=="#":
      L[H-1][w]+=1
    if S[H-2][w-1]=="#":
      L[H-1][w]+=1
    if S[H-2][w]=="#":
      L[H-1][w]+=1
    if S[H-2][w+1]=="#":
      L[H-1][w]+=1

  for h in range(1,H-1):
    if S[h-1][0]=="#":
      L[h][0]+=1
    if S[h+1][0]=="#":
      L[h][0]+=1
    if S[h-1][1]=="#":
      L[h][0]+=1
    if S[h][1]=="#":
      L[h][0]+=1
    if S[h+1][1]=="#":
      L[h][0]+=1
    if S[h-1][W-1]=="#":
      L[h][W-1]+=1
    if S[h+1][W-1]=="#":
      L[h][W-1]+=1
    if S[h-1][W-2]=="#":
      L[h][W-1]+=1
    if S[h][W-2]=="#":
      L[h][W-1]+=1
    if S[h+1][W-2]=="#":
      L[h][W-1]+=1

  for w in range(1,W-1):
    for h in range(1,H-1):
      if S[h-1][w-1]=="#":
        L[h][w]+=1
      if S[h-1][w]=="#":
        L[h][w]+=1
      if S[h-1][w+1]=="#":
        L[h][w]+=1
      if S[h][w-1]=="#":
        L[h][w]+=1
      if S[h][w+1]=="#":
        L[h][w]+=1
      if S[h+1][w-1]=="#":
        L[h][w]+=1
      if S[h+1][w]=="#":
        L[h][w]+=1
      if S[h+1][w+1]=="#":
        L[h][w]+=1

C = [["" for w in range(W)] for h in range(H)]

for w in range(W):
  for h in range(H):
    if S[h][w]==".":
      C[h][w]=str(L[h][w])
    else:
      C[h][w]="#"

o = ""
for c in C:
  o = ""
  for cc in c:
    o += cc
  print(o)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]
ans=[[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=="#":
      ans[i-1][j-1]="#"
      print(ans)
    else:
      count=(S[i-1][j-1]=="#")+(S[i-1][j]=="#")+(S[i-1][j+1]=="#")+(S[i][j-1]=="#")+(S[i][j+1]=="#")+(S[i+1][j-1]=="#")+(S[i+1][j]=="#")+(S[i+1][j+1]=="#")
      ans[i-1][j-1]=count
for i in range(h):
  print(*ans[i],sep="")

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
count = 0
X = []
p = []
X.append(["@"]*(w+2))
for _ in range(h):
  p = []
  x = input()
  for i in range(w):
    p.append(x[i])
  X.append(["@"]+p+["@"])  
X.append(["@"]*(w+2))
for i in range(1,h+1):
  for j in range(1,w+1):
    count=0
    if X[i][j]==".":
      if X[i-1][j-1]=="#":count+=1
      if X[i-1][j]=="#":count+=1
      if X[i-1][j+1]=="#":count+=1
      if X[i][j-1]=="#":count+=1
      if X[i][j+1]=="#":count+=1
      if X[i+1][j-1]=="#":count+=1
      if X[i+1][j]=="#":count+=1
      if X[i+1][j+1]=="#":count+=1
      count=str(count)
      X[i][j]=count
for k in range(1,h+1):
  print(*X[k][1:w+1])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]
ans=[[0]*w]*h
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=="#":
      ans[i-1][j-1]="#"
    else:
      count=(S[i-1][j-1]=="#")+(S[i-1][j]=="#")+(S[i-1][j+1]=="#")+(S[i][j-1]=="#")+(S[i][j+1]=="#")+(S[i+1][j-1]=="#")+(S[i+1][j]=="#")+(S[i+1][j+1]=="#")
      ans[i-1][j-1]=count
for i in range(h):
  print(*ans[i],sep="")

Submission status: Wrong Answer

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

d = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
for i in range(H):
    for j in range(W):
        print(i, j)
        if S[i][j] == '.':
            cnt = 0
            for r in range(8):
                y = i + d[r][0]
                x = j + d[r][1]
                if 0 <= x < W and 0 <= y < H and S[y][x] == '#':
                        cnt = cnt + 1
            S[i][j] = str(cnt)

for i in range(H):
    print(''.join(S[i]))

Submission status: Wrong Answer

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

for i in range(h):
    for j in range(m):
        if s[i][j] == "#":
            ans[i].append("#")
        else:
            count = 0
            s1=[[i-1,j],[i+1,j],[i,j-1],[i,j+1]]
            s2=[[i-1,j+1],[i+1,j+1],[i+1,j-1],[i-1,j-1]]
            for temp in s1 :
                i2=temp[0];j2=temp[1]
                print(i2,j2)
                if i2<0 or h-1<i2 or j2<0 or m-1<j2:
                    continue

                if s[i2][j2]=="#":
                    count+=1

            for temp in s2 :
                i2=temp[0];j2=temp[1]
                if i2<0 or h-1<i2 or j2 <0 or m-1<j2 :
                    continue

                if s[i2][j2]=="#":
                    count+=1
            ans[i].append(count)

for i in range(h):
    print(*ans[i],sep="")

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
	H, W = map(int, input().split())
	S = [list(input()) for i in range(H)]

	SS = [[0 for i in range(W+2)] for j in range(H+2)]

	for i in range(H):
		for j in range(W):
			if S[i][j] == '#':
				# pass
				SS[i+1][j+1] = 1
	print(SS)
	answer = S.copy()

	for i in range(H):
		for j in range(W):
			if answer[i][j] ==  '.':
				answer[i][j] = str(SS[i][j] + SS[i+1][j] + SS[i+2][j] + SS[i][j+1] + SS[i+2][j+1] + SS[i][j+2] + SS[i+1][j+2] + SS[i+2][j+2])

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

Submission status: Wrong Answer

Solution code:
import math
import sys
from itertools import accumulate
from functools import reduce
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

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

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

sys.setrecursionlimit(10000)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())
    = [list(list(sys.stdin.readline().rstrip())) for i in range(h)] #文字列を分解して二次元配列入力

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    h,w = map(int, sys.stdin.readline().split())
    s = [list(list(sys.stdin.readline().rstrip())) for i in range(h)]
    for i in range(h):
        for j in range(w):
            c=0
            if s[i][j] == "#":
                pass
            else:
                if 0<i and 0<j and s[i-1][j-1] == "#":#左上
                    c +=1
                if 0<j and s[i][j-1] == "#":#左
                    c +=1
                if 0<i and s[i-1][j] == "#":
                    c +=1
                if i<h-1 and j<w-1 and s[i+1][j+1] == "#":
                    c +=1
                if j<w-1 and s[i][j+1] == "#":
                    c +=1
                if i<h-1 and s[i+1][j] == "#":
                    c +=1
                if 0<i and j<w-1 and s[i-1][j+1] == "#":
                    c +=1
                if 0<j and i<h-1 and s[i+1][j-1] == "#":
                    c +=1
                s[i][j] = c
    for i in range(h):
        print(*s[i])

Submission status: Wrong Answer

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

bombMap=[]
result=[]
while i<H:
  tempLine=input()
  tempAray=[]
  tempResult=[]
  j=0
  
  while j<len(tempLine):
    tempAray.append(tempLine[j])
    tempResult.append(0)
    j=j+1
  bombMap.append(tempAray)
  result.append(tempResult)
  
  i=i+1

i=0
while i<H:
  j=0
  while j<W:
    if bombMap[i][j]=="#":
      result[i][j]="#"
      y=i-1
      while y< i+2:
        x=j-1
        while x<j+2:
          if y<H and x<W:
            if bombMap[y][x]!="#":
              #print(bombMap[y][x])
              result[y][x]=result[y][x]+1
              #print(result[y][x])
          else:
            break
          x=x+1        
        y=y+1
    
    j=j+1  
  i=i+1
  
  
#print(bombMap)
#print(result)

i=0
while i<H:
  j=0
  temp=""
  while j<W:
    temp=temp+str(result[i][j])
    j=j+1
  print(temp)
  i=i+1

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
matrix = [[x for x in input()] for y in range(h)]

def check_surround(matrix, x, y):
    if matrix[x][y] == '#':
        return '#'
    else:
        tmp = []
        tmp += check_index(matrix, x-1, y-1)
        tmp += check_index(matrix, x, y-1)
        tmp += check_index(matrix, x+1, y-1)
        tmp += check_index(matrix, x-1, y)
        tmp += check_index(matrix, x, y)
        tmp += check_index(matrix, x+1, y)
        tmp += check_index(matrix, x-1, y+1)
        tmp += check_index(matrix, x, y+1)
        tmp += check_index(matrix, x+1, y+1)
        return str(str(tmp).count('#'))

def check_index(matrix, x, y):
    try:
        return matrix[x][y]
    except:
        return str(0)

for ri in range(h):
    s = ''
    for ni in range(w):
        s += check_surround(matrix, ri, ni)
    print(s)

Submission status: Wrong Answer

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

for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j] != "#":
            if i>0:
                if S[i-1][j] == "#":
                    cnt += 1
                if j>0 and S[i-1][j-1] == "#":
                    cnt += 1
                if j<W-1 and S[i-1][j+1] == "#":
                    cnt += 1
            if i<H-1:
                if S[i+1][j] == "#":
                    cnt += 1
                if j>0 and S[i+1][j-1] == "#":
                    cnt += 1
                if j<W-1 and S[i+1][j+1] == "#":
                    print("here")
                    cnt += 1
            if j>0 and S[i][j-1] == "#":
                cnt += 1
            if j<W-1 and S[i][j+1] == "#":
                cnt += 1
            S[i][j] = str(cnt)
for i in range(H):
    print("".join(S[i]))

Submission status: Wrong Answer

Solution code:
def iread():
    return int(input())


def sread():
    return input()


def aread_int():
    tmp = input().split()
    ret = [int(i) for i in tmp]
    return ret


def aread_str():
    return input().split()


def check(arg):
    if arg == '#':
        return 1
    else:
        return 0

def format_print(matrix, colum, row):
    for i in range(colum):
        for j in range(row):
            print(matrix[i][j], end="")
        print("")

if __name__ == '__main__':
    h, w = map(int, input().split())
    matrix = []
    ans = []
    for _ in range(h):
        ans.append([0] * w)
        
    for _ in range(h):
        matrix.append(list(input()))

    for i in range(h):
        for j in range(w):
            if check(matrix[i][j]) > 0:
                ans[i][j] = '#'
            else:
                cnt = 0
                if i > 0 and i < h - 1:
                    cnt += check(matrix[i - 1][j])
                    cnt += check(matrix[i + 1][j])
                    if j > 0:
                        cnt += check(matrix[i - 1][j - 1])
                        cnt += check(matrix[i][j - 1])
                        cnt += check(matrix[i + 1][j - 1])
                    if j < w - 1:
                        cnt += check(matrix[i - 1][j + 1])
                        cnt += check(matrix[i][j + 1])
                        cnt += check(matrix[i + 1][j + 1])
                elif i > 0:
                    cnt += check(matrix[i - 1][j])
                    if j > 0:
                        cnt += check(matrix[i - 1][j - 1])
                        cnt += check(matrix[i][j - 1])
                    if j < w - 1:
                        cnt += check(matrix[i - 1][j + 1])
                        cnt += check(matrix[i][j + 1])
                elif i < h - 1:
                    cnt += check(matrix[i + 1][j])
                    if j > 0:
                        cnt += check(matrix[i][j - 1])
                        cnt += check(matrix[i + 1][j - 1])
                    if j < w - 1:
                        cnt += check(matrix[i][j + 1])
                        cnt += check(matrix[i + 1][j + 1])

                ans[i][j] = cnt
    format_print(ans, h, w)

Submission status: Wrong Answer

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

if W ==1:
    if H ==2:
        if S[0]==".":
            A[0]=str(S[1].count("#"))
        if S[1]==".":
            A[1]=str(S[0].count("#"))
    else:
        for i in range(H):
            if S[i][0]==".":
                if i ==0:
                    A[0][0]=str(S[1][0].count("#"))
                elif i ==H-1:
                    A[H-1][0]=str(S[H-2][0].count("#")+S[H-1][0].count("#"))
                else:
                    A[i][0]=str(S[i-1][0].count("#")+S[i][0].count("#")+S[i+1][0].count("#"))
    for i in range(H):
        print("".join(A[i]))
    exit()

for i in range(W):
    for j in range(H):
        if S[j][i]==".":
            if i ==0 and j ==0:
                A[j][i] =str(S[0][0:2].count("#")+S[1][0:2].count("#"))
            elif i ==W-1 and j ==0:
                A[j][i] =str(S[0][W-2:W].count("#")+S[1][W-2:W].count("#"))
            elif i ==0 and j ==H-1:
                A[j][i] =str(S[H-2][0:2].count("#")+S[H-1][0:2].count("#"))
            elif i ==W-1 and j ==H-1:
                A[j][i] =str(S[H-2][W-2:W].count("#")+S[H-1][W-2:W].count("#"))
            elif i ==0:
                A[j][i] =str(S[j-1][0:2].count("#")+S[j][0:2].count("#")+S[j+1][0:2].count("#"))
            elif i ==W-1:
                A[j][i] =str(S[j-1][W-2:W].count("#")+S[j][W-2:W].count("#")+S[j+1][W-2:W].count("#"))
            elif j ==0:
                A[j][i] =str(S[0][i-1:i+2].count("#")+S[1][i-1:i+2].count("#"))
            elif j ==H-1:
                A[j][i] =str(S[H-2][i-1:i+2].count("#")+S[H-1][i-1:i+2].count("#"))
            else:
                A[j][i] =str(S[j-1][i-1:i+2].count("#")+S[j][i-1:i+2].count("#")+S[j+1][i-1:i+2].count("#"))
           
                
for i in range(H):
    print("".join(A[i]))

Submission status: Wrong Answer

Solution code:
import numpy as np

#周りの'#'をカウントする関数
def countsherp(S_pad,i,j):
    count = 0
    for k in range(-1,2):
        for l in range(-1,2):
            if S_pad[i+k][j+l]=='#':
                count += 1
    return count

#入力
h, w = map(int,input().split()) 
S = np.array([list(input()) for i in range(h)])

#変数の初期化
w_pad = np.array([['p' for i in range(w)]])
h_pad = 'p'
S_pad = np.empty((0,w),int)

#padding処理(2次元配列の周りを'.'で囲む)
S_pad = np.append(S_pad,w_pad,axis=0)
S_pad = np.append(S_pad,S,axis=0)
S_pad = np.append(S_pad,w_pad,axis=0)
S_pad = np.insert(S_pad,0,h_pad,axis=1)
S_pad = np.insert(S_pad,S_pad.shape[1],h_pad,axis=1)

#'.'を数字に変換
for i in range(1,S_pad.shape[0]-1):
    for j in range(1,S_pad.shape[1]-1):
        count = 0
        if S_pad[i][j] == '.':
            S_pad[i][j] = countsherp(S_pad,i,j)
print(S_pad[1:-1,1:-1])

Submission status: Wrong Answer

Solution code:
# ABC75, B - minesweeper
h, w = [int(el) for el in input().split(' ')]
S = [list(input()) for i in range(h)]


for i in range(h):
    for j in range(w):
        if S[i][j] == '.':
            count = 0
            for dx in [-1, 0, 1]:
                for dy in [-1, 0, 1]:
                    try:
                        if S[i + dx][j + dy] == "#":
                            count += 1
                    except:
                        pass
            S[i][j] = str(count)


[print(''.join(S[i])) for i in range(h)]

Submission status: Wrong Answer

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

S = []
for h in range(h):
  S.append(input())

for i in range(h):
  ss = ''
  for j in range(w):
    if S[i][j] == '#':
      ss += '#'
    else:
      counter = 0
      for k in [-1, 0, 1]:
        for l in [-1, 0, 1]:
          if (k, l) == (0, 0):
            continue
          elif i+k < 0 or i+k > h-1 or j+l < 0 or j+l > w-1:
            continue
          else:
            if S[i+k][j+l] == '#':
              counter += 1
      ss += str(counter)
    print(ss)