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: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [[i for i in input()] for _ in range(h)]
count = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            if i == 0:
                if j == 0:
                    if s[i][j+1]=='#':
                        count+=1
                    if s[i+1][j]=='#':
                       count+=1
                    if s[i+1][j+1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                elif j == w-1:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i+1][j]=='#':
                        count+=1
                    if s[i+1][j-1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i+1][j-1+k]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
            elif i == h-1:
                if j == 0:
                    if s[i][j+1]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    if s[i-1][j+1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                elif j == w-1:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    if s[i-1][j-1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i-1][j-1+k]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
            else:
                if j==0:
                     if s[i+1][j]=='#':
                         count+=1
                     if s[i-1][j]=='#':
                         count+=1
                     for k in range(3):
                         if s[i-1+k][j+1]=='#':
                             count+=1
                     s[i][j]=str(count)
                     count = 0
                elif j==w-1:
                    if s[i+1][j]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    for k in range(3):
                        if s[i-1+k][j-1]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i+1][j-1+k]=='#':
                            count+=1
                        if s[i-1][j-1+k]=='#':
                            count+=1

                    s[i][j]=str(count)
                    count = 0
for i in range(h):
    print(''.join(s[i]))

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
S = [input().split() for i in range(H)]
for i in range(0,H):
    for j in range(0,W):
        if (S[i][j] == '.'):
            count = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if(0<=i+a<=H-1 and 0<=j+b<=W-1):
                        if(S[i+a][j+b] == '#'):
                            count += 1
            S[i][j] = count
    print(S[i])

Submission status: Runtime Error

Solution code:
H,W = map(int,input('H W=').split())
S = [list(map(int,input().split())) for i in range(H)]
for i in range(0,H):
    for j in range(0,W):
        if (S[i][j] == '.'):
            count = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if(0<=i+a<=H-1 and 0<=j+b<=W-1):
                        if(S[i+a][j+b] == '#'):
                            count += 1
            S[i][j] = count
    print(S[i])

Submission status: Runtime Error

Solution code:
H,W = map(int,input('H W=').split())
S = []
for i in range(H):
    s = list(map(int,input().strip().split()))
    S.append(s)
for i in range(0,H):
    for j in range(0,W):
        if (S[i][j] == '.'):
            count = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if(0<=i+a<=H-1 and 0<=j+b<=W-1):
                        if(S[i+a][j+b] == '#'):
                            count += 1
            S[i][j] = count
    print(S[i])

Submission status: Runtime Error

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

ans = board
dist = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]

for i in range(H):
    for j in range(W):
        if board[i][j] == '#':
            continue
        count = 0
        for x, y in dist:
            now_x = i + x
            now_y = j + y
            if now_x >= W or now_x < 0 or now_y >= H or now_y < 0:
                continue
            if board[now_x][now_y] == '#':
                count += 1
        ans[i][j] = str(count)

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

Submission status: Runtime Error

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

ans = board
dist = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]

for i in range(H):
    for j in range(W):
        if board[i][j] == '#':
            continue
        count = 0
        for x, y in dist:
            now_x = i + x
            now_y = j + y
            if now_x > W or now_x < 0 or now_y > H or now_y < 0:
                continue
            if board[now_x][now_y] == '#':
                count += 1
        ans[i][j] = str(count)

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

Submission status: Runtime Error

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

ans = board
dist = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]
count = 0

for i in range(H):
    for j in range(W):
        if board[i][j] == '#':
            continue
        for x, y in dist:
            now_x = i + x
            now_y = j + y
            if now_x > W or now_x < 0 or now_y > H or now_y < 0:
                continue
            if board[now_x][now_y] == '#':
                count += 1
        ans[i][j] = str(count)

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

Submission status: Runtime Error

Solution code:
import numpy as np

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

for i in range(H):
  for j in range(W):
    if S[i, j] == '.':
      S[i, j] = dict(zip(*np.unique(S[max(i-1, 0):i+2, max(j-1, 0):j+2], return_counts=True)))['#']
for s in S:
  print(''.join(s))

Submission status: Runtime Error

Solution code:
3 5
#####
#####
#####h, w = map(int, input().split())
field = [["" for _ in range(w)] for _ in range(h)]
for i in range(h):
    s = input()
    for j in range(w):
        field[i][j] = s[j]

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

def numbering(x, y):
    if field[y][x] == ".":
        c = 0
        for i in range(8):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx and nx < w and 0 <= ny and ny < h:
                if field[ny][nx] == "#":
                    c += 1
        field[y][x] = str(c)

for X in range(w):
    for Y in range(h):
        numbering(X, Y)

for Y in range(h):
    for X in range(w):
        print(field[Y][X], end = "")
    print()

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[input() for i in range(h)]
a=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    t=0
    if i==0:
      if j==0:
        if s[i][j+1]=="#":t+=1
        if s[i+1][j+1]=="#":t+=1
        if s[i+1][j]=="#":t+=1
      elif 0<j<w-1:
        if s[i][j+1]=="#":t+=1
        if s[i+1][j+1]=="#":t+=1
        if s[i+1][j]=="#":t+=1
        if s[i+1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1        
      elif j==w-1:
        if s[i+1][j]=="#":t+=1
        if s[i+1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1 
    elif 0<i<h-1:
      if j==0:
        if s[i-1][j]=="#":t+=1
        if s[i-1][j+1]=="#":t+=1
        if s[i][j+1]=="#":t+=1
        if s[i+1][j+1]=="#":t+=1
        if s[i+1][j]=="#":t+=1
      elif 0<j<w-1:
        if s[i][j+1]=="#":t+=1
        if s[i+1][j+1]=="#":t+=1
        if s[i+1][j]=="#":t+=1
        if s[i+1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1
        if s[i-1][j-1]=="#":t+=1
        if s[i-1][j]=="#":t+=1
        if s[i-1][j+1]=="#":t+=1
      elif j==w-1:
        if s[i+1][j]=="#":t+=1
        if s[i+1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1
        if s[i-1][j-1]=="#":t+=1
        if s[i-1][j]=="#":t+=1      
    elif i==h-1:
      if j==0:
        if s[i][j+1]=="#":t+=1
        if s[i-1][j+1]=="#":t+=1
        if s[i-1][j]=="#":t+=1
      elif 0<j<w-1:
        if s[i][j+1]=="#":t+=1
        if s[i-1][j+1]=="#":t+=1
        if s[i-1][j]=="#":t+=1
        if s[i-1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1        
      elif j==w-1:
        if s[i-1][j]=="#":t+=1
        if s[i-1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1
    if s[i][j]=="#":
      a[i][j]="#"
    else:
       a[i][j]=t
for aa in a:
  tt=""
  for aaa in aa:
    tt+=str(aaa)
  print(tt)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

/* Some Libraries */

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,w; cin>>h>>w;
    vector<string> s(h);
    rep(i,h) cin>>s[i];
    rep(i,h)rep(j,w){
        if (s[i][j]=='#') continue;
        int cnt=0;
        FOR(k,i-1,i+1)FOR(l,j-1,j+1){
            if (!(0<=k&&k<h&&0<=l&&l<w)) continue;
            if (s[k][l]=='#') cnt++;
        }
        s[i][j] = char(cnt+'0');
    }
    rep(i,h) cout<<s[i]<<"\n";
    return 0;
}

Submission status: Runtime Error

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


def bombcount(hi,wi):
  cnt=0
  for x in range(-1,2):
    for y in range(-1,2):
      
      if 0<=hi+x<h-1 and 0<=wi+y<w-1:
        if s[hi+x][wi+y]=="#":
          cnt+=1
  return cnt
      
for i in range(h):
  for j in range(w):
    
    if s[i][j]=="#":
      continue
    else:
      s[i][j]=bombcount(i,j)
      
for i in range(h):
  print("".join(s[i]))

Submission status: Runtime Error

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

ans = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            ans[i][j] = "#"
            if j-1 >= 0 and ans[i][j-1] != "#":
                ans[i][j-1] += 1
            if j-1 >= 0 and i-1 >= 0 and ans[i-1][j-1] != "#":
                ans[i-1][j-1] += 1
            if j-1 >= 0 and i+1 <= H-1 and ans[i+1][j-1] != "#":
                ans[i+1][j-1] += 1

            if i-1 >= 0 and ans[i-1][j] != "#":
                ans[i-1][j] += 1
            if i+1 <= H-1 and ans[i+1][j] != "#":
                ans[i+1][j] += 1
            
            if j+1 <= W-1 and ans[i][j+1] != "#":
                ans[i][j+1] += 1
            if j+1 <= W-1 and i-1 >= 0 and ans[i-1][j+1] != "#":
                ans[i-1][j+1] += 1
            if j+1 <= W-1 and i+1 <= H-1 and ans[i+1][j+1] != "#":
                ans[i+1][j+1] += 1                        

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

Submission status: Runtime Error

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 i==0:
            if j==0 and s[0][0]=='.':
                l1=[s[0][1],s[1][0],s[1][1]]
                l1l=l1.count('#')
                s[0][0]=l1l
            elif j==w-1 and s[0][w-1]=='.':
                r1=[s[0][w-2],s[1][w-2],s[1][w-1]]
                r1r=r1.count('#')
                s[0][w-1]=r1r
            elif j!=0 and j!=w-1 and s[0][j]=='.':
                l1j=[s[0][j-1],s[0][j+1],[s[1][j-1],s[1][j],s[1][j+1]]
                l1jj=l1j.count('#')
                s[0][j]==l1jj
            else:pass
        elif i==h-1:
            if j==0 and s[h-1][0]=='.':
                lh=[s[h-1][1],s[h-2][0],s[h-2][1]]
                lhl=lh.count('#')
                s[h-1][0]=lhl
            elif j==w-1 and s[h-1][w-1]=='.':
                rh=[s[h-1][w-2],s[h-2][w-2],s[h-2][w-1]]
                rhr=rh.count('#')
                s[h-1][w-1]=rhr
            elif j!=0 and j!=w-1 and s[h-1][j]=='.':
                lhj=[s[h-1][j-1],s[h-1][j+1],[s[h-2][j-1],s[h-2][j],s[h-2][j+1]]
                lhjj=lhj.count('#')
                s[0][j]==lhjj
            else:pass
        else:
            if j==0 and s[i][0]=='.':
                li=[s[i-1][0],s[i-1][1],s[i][1],s[i+1][1],s[i+1][0]]
                lil=li.count('#')
                s[i][0]=lil
            elif j==w-1 and s[i][w-1]=='.':
                ri=[s[i-1][w-1],s[i-1][w-2],s[i][w-1],s[i+1][w-2],s[i+1][w-1]]
                rir=ri.count('#')
                s[i][w-1]=rir
            elif j!=0 and j!=w-1 and s[i][j]=='.':
                lij=[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]]
                lijj=lij.count('#')
                s[i][j]==lijj
            else:pass
    print("".join(s[i]))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[list(input()) for _ in range(w)]
for i in range(h):
    for j in range(w):
        if i==0:
            if j==0 and s[0][0]=='.':
                l1=[s[0][1],s[1][0],s[1][1]]
                l1l=l1.count('#')
                s[0][0]=l1l
            elif j==w-1 and s[0][w-1]=='.':
                r1=[s[0][w-2],s[1][w-2],s[1][w-1]]
                r1r=r1.count('#')
                s[0][w-1]=r1r
            elif j!=0 and j!=w-1 and s[0][j]=='.':
                l1j=[s[0][j-1],s[0][j+1],[s[1][j-1],s[1][j],s[1][j+1]]
                l1jj=l1j.count('#')
                s[0][j]==l1jj
            else:pass
        elif i==h-1:
            if j==0 and s[h-1][0]=='.':
                lh=[s[h-1][1],s[h-2][0],s[h-2][1]]
                lhl=lh.count('#')
                s[h-1][0]=lhl
            elif j==w-1 and s[h-1][w-1]=='.':
                rh=[s[h-1][w-2],s[h-2][w-2],s[h-2][w-1]]
                rhr=rh.count('#')
                s[h-1][w-1]=rhr
            elif j!=0 and j!=w-1 and s[h-1][j]=='.':
                lhj=[s[h-1][j-1],s[h-1][j+1],[s[h-2][j-1],s[h-2][j],s[h-2][j+1]]
                lhjj=lhj.count('#')
                s[0][j]==lhjj
            else:pass
        else:
            if j==0 and s[i][0]=='.':
                li=[s[i-1][0],s[i-1][1],s[i][1],s[i+1][1],s[i+1][0]]
                lil=li.count('#')
                s[i][0]=lil
            elif j==w-1 and s[i][w-1]=='.':
                ri=[s[i-1][w-1],s[i-1][w-2],s[i][w-1],s[i+1][w-2],s[i+1][w-1]]
                rir=ri.count('#')
                s[i][w-1]=rir
            elif j!=0 and j!=w-1 and s[i][j]=='.':
                lij=[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]]
                lijj=lij.count('#')
                s[i][j]==lijj
            else:pass
    print("".join(s[i]))

Submission status: Runtime Error

Solution code:
from collections import deque
import heapq
import itertools
import math
from collections import Counter


h,w=map(int,input().split())
s=[]
neighbor=[-1,0,1]
for i in range(h):
    tmp=list(input())
    s.append(tmp)
ans=[[-1]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="#":
            ans[i][j]="#"
        elif i==0:
            if j==0:
                if s[0][1]=="#":
                    cnt+=1
                if s[1][1]=="#":
                    cnt+=1
                if s[1][0]=="#":
                    cnt+=1
                ans[0][0]=str(cnt)
            elif j==w-1:
                if s[0][w-2]=="#":
                    cnt+=1
                if s[1][w-2]=="#":
                    cnt+=1
                if s[1][w-1]=="#":
                    cnt+=1
                ans[0][w-1]=str(cnt)
            else:
                if s[0][j+1]=="#":
                    cnt+=1
                if s[0][j-1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[1][j+k]=="#":
                        cnt+=1
                ans[0][j]=str(cnt)
        elif i==h-1:
            if j==w-1:
                if s[h-2][w-1]=="#":
                    cnt+=1
                if s[h-2][w-2]=="#":
                    cnt+=1
                if s[h-1][w-2]=="#":
                    cnt+=1
                ans[h-1][w-1]=str(cnt)
            elif j==0:
                if s[h-1][1]=="#":
                    cnt+=1
                if s[h-2][1]=="#":
                    cnt+=1
                if s[h-1][0]=="#":
                    cnt+=1
                ans[h-1][0]=str(cnt)
            else:
                if s[h-1][j+1]=="#":
                    cnt+=1
                if s[h-1][j-1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[h-2][j+k]=="#":
                        cnt+=1
                ans[h-1][j]=str(cnt)
        elif j==0:
            if s[i+1][0]=="#":
                cnt+=1
            if s[i-1][0]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][1]=="#":
                    cnt+=1
            ans[i][0]=str(cnt)
        elif j==w-1:
            if s[i+1][w-1]=="#":
                cnt+=1
            if s[i-1][w-1]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][w-2]=="#":
                    cnt+=1
            ans[i][w-1]=str(cnt)
        else:
            for k in neighbor:
                if s[i+k][j-1]=="#":
                    cnt+=1
                if s[i+k][j+1]=="#":
                    cnt+=1
            if s[i+1][j]=="#":
                    cnt+=1
            if s[i-1][j]=="#":
                    cnt+=1
            ans[i][j]=str(cnt)
for i in range(h):
    answer=''.join(ans[i])
    print(answer)

Submission status: Runtime Error

Solution code:
from collections import deque
import heapq
import itertools
import math
from collections import Counter


h,w=map(int,input().split())
s=[]
neighbor=[-1,0,1]
for i in range(h):
    tmp=list(input())
    s.append(tmp)
ans=[[-1]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="#":
            ans[i][j]="#"
        elif i==0:
            if j==0:
                if s[0][1]=="#":
                    cnt+=1
                if s[1][1]=="#":
                    cnt+=1
                if s[1][0]=="#":
                    cnt+=1
                ans[0][0]=str(cnt)
            elif j==w-1:
                if s[0][w-2]=="#":
                    cnt+=1
                if s[1][w-2]=="#":
                    cnt+=1
                if s[1][w-1]=="#":
                    cnt+=1
                ans[0][w-1]=str(cnt)
            else:
                if s[0][j+1]=="#":
                    cnt+=1
                if s[0][j-1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[1][j+k]=="#":
                        cnt+=1
                ans[0][j]=str(cnt)
        elif i==h-1:
            if j==w-1:
                if s[h-2][w-1]=="#":
                    cnt+=1
                if s[h-2][w-2]=="#":
                    cnt+=1
                if s[h-1][w-2]=="#":
                    cnt+=1
                ans[h-1][w-1]=str(cnt)
            elif j==0:
                if s[h-1][1]=="#":
                    cnt+=1
                if s[h-2][1]=="#":
                    cnt+=1
                if s[h-1][0]=="#":
                    cnt+=1
                ans[h-1][0]=str(cnt)
            else:
                if s[h-1][j+1]=="#":
                    cnt+=1
                if s[h-1][j+1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[h-2][j+k]=="#":
                        cnt+=1
                ans[h-1][j]=str(cnt)
        elif j==0:
            if s[i+1][0]=="#":
                cnt+=1
            if s[i-1][0]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][1]=="#":
                    cnt+=1
            ans[i][0]=str(cnt)
        elif j==w-1:
            if s[i+1][w-1]=="#":
                cnt+=1
            if s[i-1][w-1]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][w-2]=="#":
                    cnt+=1
            ans[i][w-1]=str(cnt)
        else:
            for k in neighbor:
                if s[i+k][j-1]=="#":
                    cnt+=1
                if s[i+k][j+1]=="#":
                    cnt+=1
            if s[i+1][j]=="#":
                    cnt+=1
            if s[i-1][j]=="#":
                    cnt+=1
            ans[i][j]=str(cnt)
for i in range(h):
    answer=''.join(ans[i])
    print(answer)

Submission status: Runtime Error

Solution code:
from collections import deque
import heapq
import itertools
import math
from collections import Counter


h,w=map(int,input().split())
s=[]
neighbor=[-1,0,1]
for i in range(h):
    tmp=list(input())
    s.append(tmp)
ans=[[-1]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="#":
            ans[i][j]="#"
        elif i==0:
            if j==0:
                if s[0][1]=="#":
                    cnt+=1
                if s[1][1]=="#":
                    cnt+=1
                if s[1][0]=="#":
                    cnt+=1
                ans[0][0]=str(cnt)
            elif j==w-1:
                if s[0][w-2]=="#":
                    cnt+=1
                if s[1][w-2]=="#":
                    cnt+=1
                if s[1][w-1]=="#":
                    cnt+=1
                ans[0][w-1]=str(cnt)
            else:
                if s[0][j+1]=="#":
                    cnt+=1
                if s[0][j-1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[1][j+k]=="#":
                        cnt+=1
                ans[0][j]=str(cnt)
        elif i==h-1:
            if j==w-1:
                if s[h-2][w-1]=="#":
                    cnt+=1
                if s[h-2][w-2]=="#":
                    cnt+=1
                if s[h-1][w-2]=="#":
                    cnt+=1
                ans[h-1][w-1]=str(cnt)
            elif j==0:
                if s[h-1][1]=="#":
                    cnt+=1
                if s[h-2][1]=="#":
                    cnt+=1
                if s[h-1][0]=="#":
                    cnt+=1
                ans[h-1][0]=str(cnt)
            else:
                if s[h-1][j+1]=="#":
                    cnt+=1
                if s[h-1][j+1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[h-2][j+k]=="#":
                        cnt+=1
                ans[h-1][j]=str(cnt)
        elif j==0:
            if s[i+1][0]=="#":
                cnt+=1
            if s[i-1][0]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][1]=="#":
                    cnt+=1
            ans[i][0]=str(cnt)
        elif j==w-1:
            if s[i+1][w-1]=="#":
                cnt+=1
            if s[i-1][w-1]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][w-2]=="#":
                    cnt+=1
            ans[i][w-1]=str(cnt)
        else:
            for k in neighbor:
                if s[i+k][j-1]=="#":
                    cnt+=1
                if s[i+k][j+1]=="#":
                    cnt+=1
                if s[i+1][j]=="#":
                    cnt+=1
                if s[i-1][j]=="#":
                    cnt+=1
            ans[i][j]=str(cnt)
for i in range(h):
    answer=''.join(ans[i])
    print(answer)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]
ans = [[] for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == '#':
            ans[h - 1].append('#')
        else:
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == '#' for n in N]))

for h in range(H):
    print(''.join(ans[h]))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]
ans = [[] for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            ans[h - 1].append('#')
        else:
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == '#' for n in N]))

for h in range(H):
    print(''.join(ans[h]))

Submission status: Runtime Error

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

ans_list = copy.deepcopy(map_list)

# 高さのループ
for _h in range(h):
  # 幅のループ
  for _w in range(w):
    # カウント用変数
    counter = 0
    
    # 調査座標が"#"だったら回答に#を代入
    if map_list[_h][_w] == "#":
      counter = "#"

    # 最上段
    if _h == 0 and counter != "#":
      # 左端
      if _w == 0:
        if map_list[_h][_w+1] == "#":
          counter += 1
        if map_list[_h+1][_w] == "#":
          counter += 1
        if map_list[_h+1][_w+1] == "#":
          counter += 1
      # 右端
      elif _w == w - 1:
        if map_list[_h][_w-1] == "#":
          counter += 1
        if map_list[_h+1][_w-1] == "#":
          counter += 1
        if map_list[_h+1][_w] == "#":
          counter += 1
      # 両端以外
      else:
        if map_list[_h][_w-1] == "#":
          counter += 1
        if map_list[_h][_w+1] == "#":
          counter += 1
        if map_list[_h+1][_w-1] == "#":
          counter += 1
        if map_list[_h+1][_w] == "#":
          counter += 1
        if map_list[_h+1][_w+1] == "#":
          counter += 1
    # 最下段
    elif _h == h - 1 and counter != "#":
      # 左端
      if _w == 0:
        if map_list[_h-1][_w] == "#":
          counter += 1
        if map_list[_h-1][_w+1] == "#":
          counter += 1
        if map_list[_h][_w+1] == "#":
          counter += 1
      # 右端
      elif _w == w - 1:
        if map_list[_h-1][_w-1] == "#":
          counter += 1
        if map_list[_h-1][_w] == "#":
          counter += 1
        if map_list[_h][_w-1] == "#":
          counter += 1
      # 両端以外
      elif counter != "#":
        if map_list[_h-1][_w-1] == "#":
          counter += 1
        if map_list[_h-1][_w] == "#":
          counter += 1
        if map_list[_h-1][_w+1] == "#":
          counter += 1
        if map_list[_h][_w-1] == "#":
          counter += 1
        if map_list[_h][_w+1] == "#":
          counter += 1
    # 最上下段以外
    elif _h != 0 and _h != h - 1 and counter != "#":
      # 左端
      if _w == 0:
        if map_list[_h-1][_w] == "#":
          counter += 1
        if map_list[_h-1][_w+1] == "#":
          counter += 1
        if map_list[_h][_w+1] == "#":
          counter += 1
        if map_list[_h+1][_w] == "#":
          counter += 1
        if map_list[_h+1][_w+1] == "#":
          counter += 1
      # 右端
      elif _w == w - 1:
        if map_list[_h-1][_w-1] == "#":
          counter += 1
        if map_list[_h-1][_w] == "#":
          counter += 1
        if map_list[_h][_w-1] == "#":
          counter += 1
        if map_list[_h+1][_w-1] == "#":
          counter += 1
        if map_list[_h+1][_w] == "#":
          counter += 1
      # 両端以外
      else:
        if map_list[_h-1][_w-1] == "#":
          counter += 1
        if map_list[_h-1][_w] == "#":
          counter += 1
        if map_list[_h-1][_w+1] == "#":
          counter += 1
        if map_list[_h][_w-1] == "#":
          counter += 1
        if map_list[_h][_w+1] == "#":
          counter += 1
        if map_list[_h+1][_w-1] == "#":
          counter += 1
        if map_list[_h+1][_w] == "#":
          counter += 1
        if map_list[_h+1][_w+1] == "#":
          counter += 1
    ans_list[_h][_w] = counter
for i in ans_list:
  print("".join(list(map(str,i))))