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:
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(list(input()))
if h == 1 and w == 1:
    print(S[0][0])
if h == 1 and w == 2:
    if S == [['.','#']]:
        print('1#')
    if S == [['#','#']]:
        print('##')
    if S == [['#','.']]:
        print('#1')
    if S == [['.','.']]:
        print('..')
if h == 2 and w == 1:
    if S == [['.'],['.']]:
        print('.')
        print('.')
    if S == [['#'],['.']]:
        print('#')
        print('1')
    if S == [['.'],['#']]:
        print('1')
        print('#')
    if S == [['#'],['#']]:
        print('#')
        print('#')
if h >= 2 and w >= 2:
    for k in range(h):
        for j in range(w):
            if S[k][j] == '.':
                if k == 0 and j == 0:
                    count = 0
                    if S[0][1] == '#':
                        count += 1 
                    count += S[1][0:2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and 1 <= j <= w-2:
                    count = 0
                    count += S[0][j-1:j+2].count('#')
                    count += S[1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and j == w-1:
                    count = 0
                    if S[0][j-1] == '#':
                        count += 1
                    count += S[1][j-1:].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    if S[k][1] == '#':
                        count += 1 
                    count += S[k+1][0:2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    count += S[k+1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    count += S[k+1][j-1:].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    count += S[k][1].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    S[k][j] = str(count)
    for p in S:
        print(''.join(p))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(list(input()))
if h == 1 and w == 2:
    if S == [['.','#']]:
        print('1#')
    if S == [['#','#']]:
        print('##')
    if S == [['#','.']]:
        print('#1')
    if S == [['.','.']]:
        print('..')
if h == 2 and w == 1:
    if S == [['.'],['.']]:
        print('.')
        print('.')
    if S == [['#'],['.']]:
        print('#')
        print('1')
    if S == [['.'],['#']]:
        print('1')
        print('#')
    if S == [['#'],['#']]:
        print('#')
        print('#')
if h >= 2 and w >= 2:
    for k in range(h):
        for j in range(w):
            if S[k][j] == '.':
                if k == 0 and j == 0:
                    count = 0
                    if S[0][1] == '#':
                        count += 1 
                    count += S[1][0:2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and 1 <= j <= w-2:
                    count = 0
                    count += S[0][j-1:j+2].count('#')
                    count += S[1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and j == w-1:
                    count = 0
                    if S[0][j-1] == '#':
                        count += 1
                    count += S[1][j-1:].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    if S[k][1] == '#':
                        count += 1 
                    count += S[k+1][0:2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    count += S[k+1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    count += S[k+1][j-1:].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    count += S[k][1].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    S[k][j] = str(count)
    for p in S:
        print(''.join(p))

Submission status: Wrong Answer

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

h, w = map(int, input().split())
s_list = [list(input()) for _ in range(h)]
ans_list = [[0] * w for _ in range(h)]
print(s_list)
print(ans_list)

for i in range(h):
    for j in range(w):
        if s_list[i][j] == "#":
            ans_list[i][j] = "#"
        else:
            if (i!=0) and (j!=0):
                if (s_list[i-1][j-1] == "#"):
                    ans_list[i][j] += 1
            if (i!=0):
                if (s_list[i-1][j] == "#"):
                    ans_list[i][j] += 1
            if (i!=0) and (j!=w-1):
                if (s_list[i-1][j+1] == "#"):
                    ans_list[i][j] += 1
            if (j!=0):
                if (s_list[i][j-1] == "#"):
                    ans_list[i][j] += 1
            if (j!=w-1):
                if (s_list[i][j+1] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=0):
                if (s_list[i+1][j-1] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1):
                if (s_list[i+1][j] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=w-1):
                if (s_list[i+1][j+1] == "#"):
                    ans_list[i][j] += 1

for i in ans_list:
    print(''.join(map(str, i)))

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc075/tasks/abc075_b?lang=ja
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        count = 0
        if s[i][j] == "#":
            pass
        else:
            if i > 0 and j > 0 and s[i-1][j-1] == "#": #左上
                count +=1
            if i > 0 and s[i-1][j]=="#": #上
                count +=1
            if i > 0 and j <W-1 and s[i-1][j+1] =="#": #右上
                count +=1  
            if j < W-1 and s[i][j+1] =="#": #右
                count +=1
            if i < H-1 and j<W-1 and s[i+1][j+1] =="#": #右下
                count +=1
            if i < H-1 and s[i+1][j] =="#": #下
                count +=1
            if i<H-1 and j<0 and s[i+1][j-1] =="#":  #左下
                count +=1
            if j > 0 and s[i][j-1] == "#":   #左
                count +=1
            s[i][j] = str(count)         
for i in range(H):
    print("".join(s[i]))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
Ws=W+2
Hs=H+2
root= [[0] * Ws for i in range(Hs)]
c=0
for i in range(H):
    I =""
    I = input()
    print(I)
    for j in range(W):
        print("youso is ",I[j])
        k = I[j]
        root[i+1][j+1]=k
root2= [[0] * W for i in range(H)]

for i in range(H):
    
    for j in range(W):
        if root[i+1][j+1] == ".":
            c=0
            if root[i+1+1][j+1+1]=="#":
                c+=1
            if root[i+1+1][j+1]=="#":
                c+=1
            if root[i+1+1][j-1+1]=="#":
                c+=1
            if root[i+1-1][j+1+1]=="#":
                c+=1
            if root[i+1-1][j+1]=="#":
                c+=1
            if root[i+1-1][j-1+1]=="#":
                c+=1
            if root[i+1+1][j+1+1]=="#":
                c+=1
            if root[i+1+1][j+1-1]=="#":
                c+=1
              

            root2[i][j]=c
        else:root2[i][j]="#"
        
for i in range(H):
    print(root[H])

Submission status: Wrong Answer

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

list1 = []
moziretu = ""

for i in range(h):
    s = input()
    moziretu = list(s.replace(".", "0").replace(".", "9"))
    moziretu = [int(c) if c.isdecimal() else c for c in moziretu]
    list1.append(moziretu)
    
for i in range(h):
    for j in range(w):
        if list1[i][j] == "#":
            try:
                list1[i-1][j-1] += 1
            except:
                pass
            try:
                list1[i-1][j] += 1
            except:
                pass
            try:
                list1[i-1][j+1] += 1
            except:
                pass
            try:
                list1[i][j-1] += 1
            except:
                pass            
            try:
                list1[i][j+1] += 1
            except:
                pass            
            try:
                list1[i+1][j-1] += 1
            except:
                pass            
            try:
                list1[i+1][j] += 1
            except:
                pass            
            try:
                list1[i+1][j+1] += 1
            except:
                pass
            
for i in range(h):
    for j in range(w):
        try:
            list1[i][j] = str(list1[i][j])
        except:
            pass

for i in range(h):
    print("".join(list1[i]))

Submission status: Wrong Answer

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

ans = []
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j] == "#":
      ans.append("#")
    else:
      c = [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.append(c.count("#"))
      
print(ans)
for i in range(h):
  print("".join(list(map(str, ans[i*w:(i+1)*w]))))

Submission status: Wrong Answer

Solution code:
H,W = [int(x) for x in input().split()]
S = ['.'*(W+2)]+['.'+input()+'.' for _ in range(H)]+['.'*(W+2)]
ans = []
for i in range(1,H+1):
    s = ''
    for j in range(1,W+1):
        cnt = 0
        li = [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]
        for x in li:
            if(S[i+x[0]][j+x[1]]=='#'):
                cnt += 1
        if(S[i][j]=='#'):
            s+='#'
        elif(cnt==0):
            s+='.'
        else:
            s+=str(cnt)

    ans.append(s)
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
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] == ".":
            cnt = 0
            for di in [-1,0,1]:
                for dj in [-1,0,1]:
                    if di == 0 and dj == 0:
                        continue
                    ni = i + di; nj = j + dj
                    if not(0 <= ni <= H-1 and 0 <= nj <= W-1):
                        continue
                    if S[ni][nj] == "#":
                        cnt += 1
            S[i][j] = cnt

for line in S:
    print(*line)

Submission status: Wrong Answer

Solution code:
import numpy as np
H, W = map(int, input().split())
m = np.zeros((H,W))
bomb = []
for h in range(H):
  row = list(str(input()))
  for w in range(W):
    if row[w] == "#":
      bomb.append((h, w))

for h, w in bomb:
  if h!=0 and w!=0:
    m[h-1, w-1] += 1
  if h!=0:
    m[h-1, w] += 1
  if h!=0 and w!=W-1:
    m[h-1, w+1] += 1
    
  if w!=0:
    m[h, w-1] += 1
  if w!=W-1:
    m[h, w+1] += 1
  
  if h!=H-1 and w!=0:
    m[h+1, w-1] += 1
  if h!=H-1:
    m[h+1, w] += 1
  if h!=H-1 and w!=W-1:
    m[h+1, w+1] += 1

for h in range(H):
  for w in range(W):
    if (h, w) in bomb:
      print("#", end="")
    else:
      print(m[h, w], end="")
  print("", end="\n")

Submission status: Wrong Answer

Solution code:
tmp = (input().split(" "))
H = int(tmp[0])
W = int(tmp[1])

datas = [input() for i in range(0, H, 1)]

array = []

for data in datas:
  tmp = [x for x in list(data)]
  array.append(tmp)
  
for i in range(0, H, 1):
  for j in range(0, W, 1):
    
    cnt = 0
    
    #空きマスでない場合
    if array[i][j] == "#":
      continue
      
    else:

      for l in range(-1, 2, 1):
        for m in range(-1, 2, 1):
            
          no1 = i + l
          no2 = j + m
          
          if l == no1 and m == no2:
            continue
          
            
          try:
            
            if no1 < 0 or no2 < 0:
              continue
              
            else:
              if array[no1][no2] == "#":
                cnt += 1
              else:
                pass
            
          except:
            pass
          
      array[i][j] = str(cnt)
      
for x in (array):
  print("".join(x))

Submission status: Wrong Answer

Solution code:
from collections import deque

h,w = map(int,input().split())
hw = [list(input()) for _ in range(h)]

for i in range(h):
  for j in range(w):
    if hw[i][j] == ".":
      cnt = 0
      for x in [-1, 0, 1]:
        for y in [-1, 0, 1]:
          try:
            if hw[i+y][j+x]=="#":
              cnt += 1
          except:
            None
      hw[i][j]=cnt

for line in hw:
  print("".join(list(map(str, line))))

Submission status: Wrong Answer

Solution code:

def read_input():
    h, w = map(int, input().split())
    slist = []
    for i in range(h):
        slist.append(input().strip())

    return h, w, slist

# i, jの周辺に1を足す
def sumup(board, i, j):
    def notbomb(x):
        return x != '#'

    up = i - 1 >= 0
    down = i + 1 < len(board)
    left = j - 1 >= 0
    right = j + 1 < len(board[0])

    if up:
        if notbomb(board[i - 1][j]):
            board[i - 1][j] += 1
        if left:
            if notbomb(board[i - 1][j - 1]):
                board[i - 1][j - 1] += 1
        if right:
            if notbomb(board[i - 1][j + 1]):
                board[i - 1][j + 1] += 1
    if left:
        if notbomb(board[i][j - 1]):
            board[i][j - 1] += 1
    if right:
        if notbomb(board[i][j + 1]):
           board[i][j + 1] += 1
    if down:
        if notbomb(board[i + 1][j]):
            board[i + 1][j] += 1
        if left:
            if notbomb(board[i + 1][j - 1]):
                board[i + 1][j - 1] += 1
        if right:
            if notbomb(board[i + 1][j + 1]):
                board[i + 1][j + 1] += 1
    return board

def submit():
    h, w, slist = read_input()

    board = []
    for s in slist:
        board.append([0 if c == '.' else c for c in s])

    for i in range(h):
        for j in range(w):
            if board[i][j] == '#':
                board = sumup(board, i, j)

    print(board)


if __name__ == '__main__':
    submit()

Submission status: Wrong Answer

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    h, w = f()
    s = [input() for i in range(h)]
    d = [[0]*w for i in range(h)]
    print(s)

    for i in range(h):
        for j in range(w):
            if s[i][j] == '.':
                cnt = 0
                for x in range(-1,2):
                    for y in range(-1,2):
                        if i+x in range(h) and j+y in range(w):
                            if s[i+x][j+y] == '#':
                                cnt+=1
                d[i][j] = str(cnt)
    
    for i in range(h):
        for j in range(w):
            if s[i][j] == '.':
                print(d[i][j], end = '' if j!=w-1 else '\n')
            else:
                print(s[i][j], end = '' if j!=w-1 else '\n')




solve()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc075/tasks/abc075_b
hw_list = list(map(int, input().split()))
h_max = hw_list[0]
w_max = hw_list[1]

s_list = []

for i in range(h_max):
    work_list = input()
    s_list.append(work_list)
answer_list = []

for h in range(h_max):
    work_list = s_list[h]
    work_answer = []
    for w, target in enumerate(work_list):
        if target == '.':
            tonari_list = [(w-1, h-1), (w, h-1), (w+1, h-1),
                           (w-1, h), (w+1, h),
                           (w-1, h+1), (w, h+1), (w+1, h+1)]
            count = 0
            for x, y in tonari_list:
                if 0 <= x < w_max and 0 <= y < h_max:
                    print('{}, {}'.format(x, y))
                    if s_list[y][x] == '#':
                        count += 1
                else:
                    continue
            work_answer.append(str(count))
        else:
            work_answer.append('#')
    answer_list.append(work_answer)

for a_list in answer_list:
    print(''.join(a_list))

Submission status: Wrong Answer

Solution code:
import copy

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for dir in range(8):
                try:
                    cnt += (S[i + dy[dir]][j + dx[dir]] == "#")
                except:
                    pass
            ans[i][j] = str(cnt)
for h in range(H):
    print("".join(ans[h]))

Submission status: Wrong Answer

Solution code:
import copy

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for dir in range(8):
                try:
                    cnt += (S[i + dx[dir]][j + dy[dir]] == "#")
                except:
                    pass
            ans[i][j] = str(cnt)
for h in range(H):
    print("".join(ans[h]))

Submission status: Wrong Answer

Solution code:
import copy

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for dir in range(8):
                try:
                    cnt += (S[i + dx[dir]][j + dy[dir]] == "#")
                except:
                    pass
            ans[i][j] = str(cnt)
for h in range(H):
    print("".join(ans[h]))

Submission status: Wrong Answer

Solution code:
import copy

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for dir in range(8):
                try:
                    cnt += (S[i + dx[dir]][j + dy[dir]] == "#")
                except:
                    pass
            ans[i][j] = cnt
print(ans)

Submission status: Wrong Answer

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

def count(ch):
  return 1 if ch == '#' else 0

for i in range(1,H+1):
  for j in range(W):
    if mine[i][j] == '#':
      continue
    else:
      mine[i][j] = count(mine[i-1][j-1])+count(mine[i][j-1])+count(mine[i+1][j-1])+count(mine[i-1][j])+count(mine[i][j])+count(mine[i+1][j])+count(mine[i-1][j+1])+count(mine[i][j+1])+count(mine[i+1][j+1])
for i in range(1,H+1):
  for j in range(1,W+1):
    print(mine[i][j],end='')
  print()

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
grid = []
for i in range(h):
    grid.append(input())
def check(i,j):
    try :
        return grid[i][j] == '#'
    except:
        return 0
def cnt(i,j):
    c = 0
    for d in [1,-1]:
        c += check(i+d,j)
        c += check(i,j+d)
    c += check(i+1,j+1)
    c += check(i+1,j-1)
    c += check(i-1,j-1)
    c += check(i-1,j+1)
    return c
g2 = []
for i in range(h):
    an = ""
    for j in range(w):
        if grid[i][j] == '#':
            an += '#'
        else:
            an += str(cnt(i,j))
    g2.append(an)
for i in g2:
    print(i)

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
#get hight and weidth 
glid = [0] * h

h_index = [-1, 0, 1, -1, 1, -1, 0, 1]
w_index = [-1, -1, -1, 0, 0, 1, 1, 1]

for i in range(h):
	glid[i] = list(input())
# get glids 

for j in range(h):
	for k in range(w):
		if glid[j][k] == "#":
			continue
		else:
			cnt = 0
			for n in range(8):
				if 0 <= j + h_index[n] and  j + h_index[n] <= h -1 and k + w_index[n] and  k + w_index[n] <= w -1:
					if glid[ j + h_index[ n ] ][ k + w_index[ n ]] == "#" :
						cnt += 1
			glid[ j ][ k ] = str(cnt)

for h_print in range (h):
	for w_print in range (w):
		print(glid[h_print][w_print], end="")
	print()

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
m=[[0for _ in range(w+2)]for _ in range(h+2)]
for y in range(h):
  s=input()
  for x in range(w):
    if s[x]=="#":
      for i in [0,1,2]:
        for j in [0,1,2]:
          m[y+j][x+i]+=1
      m[y+1][x+1]=9
for y in range(1,h+1):
  ans=""
  for x in range(1,w+1):
    if m[y][x]>9:
      ans+="#"
    else:
      ans+=str(m[y][x])
  print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
m=[[0for _ in range(w+2)]for _ in range(h+2)]
for y in range(h):
  s=input()
  for x in range(w):
    if s[x]=="#":
      for i in [-1,0,1]:
        for j in [-1,0,1]:
          m[y+j][x+i]+=1
      m[y+1][x+1]=9
for y in range(1,h+1):
  ans=""
  for x in range(1,w+1):
    if m[y][x]>9:
      ans+="#"
    else:
      ans+=str(m[y][x])
  print(ans)

Submission status: Wrong Answer

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

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

for i in range(h):
    for j in range(w):
        if masu[i][j] == ".":
            count = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    xx = i + dx
                    yy = j + dy
                    if 0 <= xx and xx < w and 0 <= yy and yy < h:
                        if masu[yy][xx] == "#":
                            count += 1
            masu[i][j] = count
for i in range(h):
    print("".join(map(str, masu[i])))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
s = []
def calc(i,j,k,l):
    if k==0 and l==0:
        return 0
    elif i+k==-1 or j+l==-1 or i+k==h or j+l==w:
        return 0
    elif s[i+k][j+l] == '#':
        count[i][j] = count[i][j] + 1
for i in range(h):
    s += [input()]
count = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        for k in range(3):
            for l in range(3):
                calc(i,j,k-1,l-1)
for i in range(h):
    s_tmp=''
    for j in range(w):
        if s[i][j]=='#':
            s_tmp += '#'
        else:
            s_tmp += str(count[i][j])
    s[i]=s_tmp
print(s)

Submission status: Wrong Answer

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

if h == 1 and w == 1:
    print(s[0])

elif h == 1:
    ans = ''
    for i in range(w):
        if s[0][i]=='#':
            ans += '#'
        elif i != 0 and i != w-1:
            ans += str(s[0][i-1:i+2].count('#'))
        elif i == 0:
            ans += str(s[0][1].count('#'))
        elif i == w-1:
            ans += str(s[0][i-1].count('#'))
    print(ans)

elif w == 1:
    for i in range(h):
        if s[i]=='#':
            print('#')
        elif i != 0 and i != h-1:
            print(str(s[i-1:i+2].count('#')))
        elif i == 0:
            print(str(s[1].count('#')))
        elif i == h-1:
            print(str(s[i-1].count('#')))

else:
    for i in range(h):
        ans = ''
        for j in range(w):
            if s[i][j] == '#':
                ans += '#'
            else:
                if i != 0 and j!= 0 and i != h-1 and j!= w-1:
                    up = s[i-1][j-1:j+2].count('#')
                    parallel = s[i][j-1:j+2].count('#')
                    down = s[i+1][j-1:j+2].count('#')
                    ans += str(up+parallel+down)
                elif i==0 and j==0:
                    parallel = s[0][1].count('#')
                    down = s[1][0:2].count('#')
                    ans += str(parallel+down)
                elif i==0 and j==w-1:
                    parallel = s[0][w-2:].count('#')
                    down = s[1][w-2:].count('#')
                    ans += str(parallel+down)
                elif i==h-1 and j==0:
                    up = s[i-1][:2].count('#')
                    parallel = s[i][:2].count('#')
                    ans += str(up+parallel)
                elif i==h-1 and j==w-1:
                    up = s[i-1][w-2:].count('#')
                    parallel = s[i][w-2:].count('#')
                    ans += str(up+parallel)
                elif i==0:
                    parallel = s[0][j-1:j+2].count('#')
                    down = s[1][j-1:j+2].count('#')
                    ans += str(parallel+down)
                elif j==0:
                    up = s[i-1][:2].count('#')
                    parallel = s[i][:2].count('#')
                    down = s[i+1][:2].count('#')
                    ans += str(up+parallel+down)
                elif i==h-1:
                    up = s[i-1][j-1:j+2].count('#')
                    parallel = s[i][j-1:j+2].count('#')
                    ans += str(up+parallel)
                else:
                    up = s[i-1][w-2:].count('#')
                    parallel = s[i][w-2:].count('#')
                    down = s[i+1][w-2:].count('#')
                    ans += str(up+parallel+down)
        print(ans)

Submission status: Wrong Answer

Solution code:
h,w=[int(i) for i in input().split()]
s=[]
for i in range(h):
    s.append([j for j in input()])
for i in range(h):
    text=""
    for j in range(w):
        if s[i][j]!="#":
            cnt=0
            try:
                if s[i-1][j-1 if j-1>=0 else 100]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i-1][j]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i-1][j+1]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i][j-1 if j-1>=0 else 100]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i][j+1]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j-1 if j-1>=0 else 100]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j+1]=="#":
                    cnt+=1
            except:
                pass
            text+=str(cnt)
        else:
            text+="#"
    print(text)

Submission status: Wrong Answer

Solution code:
H, W = map(int,input().split())
S = [list(input()) for i in range(H)] #リストを二重にする
#print(S)
for i in range(H): #すべてのマスを探索する
  for j in range(W):
    if S[i][j] == ".": #行列の中の点(なんもない地帯)を0に変換する
      S[i][j] = 0
#print(S)
for i in range(H): #すべてのマスを探索する
  for j in range(W):
    if S[i][j] == "#": #爆弾マスに当たった場合以下の処理が起きてそれ以外は何もおきない
       for k in range(-1, 2): #爆弾マスの横軸の行3マス分
          for l in range(-1, 2): #爆弾マスの縦軸の列3マス分
            ni = i+k 
            nj = j+l #爆弾マスの詳しい座標情報
            if (0 <= ni < H) and (0 <= nj < W) and \
               (S[ni][nj] != '#') and (S[ni][nj] != '.'): 
                #在りもしないマスで無いかの条件式
                #そのマスが数字であるかの条件式
              S[ni][nj] += 1
print(S)
for i in range(H): #リストの文字列を連結・結合
    print(''.join(map(str, S[i]))) #二重リストであることに注意、改行すると余計ややこしくなる

Submission status: Wrong Answer

Solution code:
H, W = [int(_) for _ in input().split()]
cbd = [list(input()) for _ in range(H)]
for i in range(H): 
    for j in range(W):
        if cbd[i][j] == ".":
            c = 0
            for iw in range(i-1,i+2):
                for jw in range(j-1,j+2):
                    if iw < 0 or H <= iw or jw < 0 or W <= jw:
                        continue
                    elif cbd[iw][jw] == "#":
                        c += 1
            cbd[i][j] = str(c)
print(*cbd,sep="\n")