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:
#!/usr/bin/env python3
# input
h, w = map(int, input().split())
s = [input() for _ in range(h)]

def sweep(i, j, s):
    global h; global w
    ans = 0
    # inner 
    if 0 < i < h-1:
        if 0 < j < w-1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    # boundary
    if i == 0:
        if j == 0:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        else:
            sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j], s[i+1][j+1], s[i][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    elif i == h-1:
        if j == 0:
            sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        else:
            sweeps = [s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1], s[i][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    else:
        if j == 0:
            sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1], s[i+1][j+1], s[i+1][j]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1], s[i+1][j-1], s[i+1][j]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    return ans

def sweep1line(j, s):
    global w
    ans = 0
    if j == 0:
        if s[0][j+1] == '#':
            ans += 1
    elif j == w-1:
        if s[0][j-1] == '#':
            ans += 1
    else:
        sweeps = [s[0][j-1], s[0][j+1]]
        for sw in sweeps:
            if sw == '#':
                ans += 1
    return ans

# calc
ans = [['' for _ in range(w)] for _ in range(h)]

if h != 1:
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans[i][j] = '#'
            if s[i][j] == '.':
                ans[i][j] = str(sweep(i, j, s))
else:
    for j in range(w):
        if s[0][j] == '#':
            ans[0][j] = '#'
        if s[0][j] == '.':
            ans[0][j] = str(sweep1line(j, s))

# output
for i in range(h):
    print(''.join(ans[i]))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# input
h, w = map(int, input().split())
s = [input() for _ in range(h)]

def sweep(i, j, s):
    global h; global w
    ans = 0
    # inner
    if 0 < i < h-1:
        if 0 < j < w-1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
    # boundary
    if i == 0:
        if j == 0:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        else:
            sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j], s[i+1][j+1], s[i][j+1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
    elif i == h-1:
        if j == 0:
            sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        else:
            sweeps = [s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1], s[i][j+1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
    else:
        if j == 0:
            sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1], s[i+1][j+1], s[i+1][j]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1], s[i+1][j-1], s[i+1][j]]
            for s in sweeps:
                if s == '#':
                    ans += 1
    return ans

ans = [['' 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 s[i][j] == '.':
            ans[i][j] = str(sweep(i, j, s))
# output
for i in range(h):
    print(''.join(ans[i]))

Submission status: Runtime Error

Solution code:
import copy
H,W=map(int,input().split())
S=[0]*W
for i in range(H):
    S[i]=list(input()+".")
S+=[["."]*(W+1)]
S2=[["#"]*W]*H

for i in range(H):
    for j in range(W):
        if S[i][j]=="#":
            S2[i][j]="#"
        else:
            if i==0:
                im=H
            else:
                im=i-1
            if j==0:
                jm=W
            else:
                jm=j-1

            if i==H-1:
                ip=H
            else:
                ip=i+1

            if j==W-1:
                jp=W
            else:
                jp=j+1
            cnt=[S[im][jm],S[im][j],S[im][jp],S[i][jm],S[i][jp],S[ip][jm],S[ip][j],S[ip][jp]]
            S2[i][j]=cnt.count('#')

print(S2)

Submission status: Runtime Error

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

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

S_ans = []
for row in S:
    S_ans.append(list(row))
#print(S) #['.....', '.#.#.', '.....']
#print(S[1][1]) # #と表示される．二次元配列のように扱える！
count = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            if i == j == 0:
                if S[i][j+1] == "#":
                    count += 1
                if S[i+1][j] == "#":
                    count += 1
                if S[i+1][j+1] == "#":
                    count += 1
            elif i==0 and 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
            elif i == (H-1) and j == 0:
                if S[i][j+1] == "#":
                    count += 1
                if S[i-1][j] == "#":
                    count += 1
                if S[i-1][j+1] == "#":
                    count += 1
            elif i == (H-1) and 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
            elif i == (H-1):
                if S[i][j+1] == "#":
                    count += 1
                if S[i-1][j+1] == "#":
                    count += 1
                if S[i-1][j] == "#":
                    count += 1
                if S[i-1][j-1] == "#":
                    count += 1
                if S[i][j-1] == "#":
                    count += 1
            elif j == (W-1):
                if S[i-1][j] == "#":
                    count += 1
                if S[i-1][j-1] == "#":
                    count += 1
                if S[i][j-1] == "#":
                    count += 1
                if S[i+1][j-1] == "#":
                    count += 1
                if S[i+1][j] == "#":
                    count += 1
            else: #端じゃない所
                if S[i][j+1] == "#":
                    count += 1
                if S[i+1][j] == "#":
                    count += 1
                if S[i+1][j+1] == "#":
                    count += 1
                if S[i+1][j-1] == "#":
                    count += 1
                if S[i][j-1] == "#":
                    count += 1
                if S[i-1][j-1] == "#":
                    count += 1
                if S[i-1][j] == "#":
                    count += 1
                if S[i-1][j+1] == "#":
                    count += 1
            S_ans[i][j]=str(count)
            count = 0
                
        # if S[i][j] == "#":
        #     S[i][j+1]
        #     S[i+1][j]
        #     S[i+1][j+1]
for row in S_ans:
    print(''.join(list(map(str,row))))

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 s[i][j] == '.':
            count = 0
            for dx, dy in (
                (-1,1), (0,1), (1,1),
                (-1,0), (0,0), (1,0),
                (-1,-1),(0,-1),(1,-1)
            ):
                now_x, now_y = i+dx, j+dy
                if s[now_x][now_y] == '#':
                    if 0 <= now_x and 0 <= now_y and now_x <= H-1 and now_y <= W-1:
                        count += 1
            s[i][j] = str(count)

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

Submission status: Runtime Error

Solution code:
l, x, y, s, d = list(map(int, input().split()))

if d >= s:
    a, b = d-s, l-(d-s)
else:
    a, b = l-(s-d), s-d

if x >= y:
    print(a/(x+y))
else:
    print(min(a/(x+y), b/(y-x)))

Submission status: Runtime Error

Solution code:
# coding: utf-8
import re
import math
from collections import defaultdict
import itertools
from copy import deepcopy
import random
from heapq import heappop,heappush
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)
###################################################

def main():
    H,W=map(int,input().split())
    S=[]
    for a in range(H):
        S.append(input())
    ans=["a"*W]*H
    for h in range(H):
        for w in range(W):
            c=S[h][w]
            if c=="#":
                ans[h][w]="#"
            else:
                count=0
                for a in range(-1,2):
                    for b in range(-1,2):
                        x=w+b
                        y=h+a
                        if 0<=y and y<H and 0<=x and x<W:
                            if ans[y][x]=="#":
                                count+=1
                ans[h][w]=str(count)
    for a in range(H):
        print(ans[a])
main()

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
src = [input() for i in range(H)]
ans = []
for row in src:
    ans.append(list(row))
dxy=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for i in range(H):
    for j in range(W):
        c=0
        if src[i][j]=="#": continue
        for dx,dy in dxy:
            if j+dx<0 or j+dx>W-1 or i+dy<0 or i+dy>H-1: continue
            if src[j+dx][i+dy]=="#":
                c+=1
        ans[i][j]=c
for row in ans:
    print("".join(list(map(str,row))))

Submission status: Runtime Error

Solution code:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const long long INF = 1LL << 60;
const int MOD = 1e9 + 7;

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

bool isGuMojiretsu(string S){
	int n = S.size();
	if(n %2 == 0){
		rep(i,n/2){
			if(S[i]!=S[n/2+i]){
				return false;
			}
		}
		return true;
	}else{
		return false;
	}
}

int main() {
	int H,W;
	cin >> H >> W;

	std::vector<string> S(H);
	S.insert(S.begin(),string(W+2,'.'));
	S.push_back(string(W+2,'.'));
	
	rep(i,H){
		cin >> S.at(i+1);
		S.at(i+1) = "."+S.at(i+1)+".";
	}

	int cnt = 0;
	rep2(i,1,H+1){
		rep2(j,1,W+1){
			cnt = 0;
			rep2(qq,-1,1+1){
				rep2(pp,-1,1+1){
					cnt += (S.at(i+pp)[j+qq]=='#' && !(pp==qq && pp == 0))?1:0;
				}
			}
			S.at(i)[j] = (S.at(i)[j]=='#')?'#':cnt+'0';
		}
		cout << S.at(i).substr(1,W) << endl;
	}
	return 0;

}

Submission status: Runtime Error

Solution code:
import numpy as np
H,W = map(int,input().split())
table = [["."]*(W+2),]
for _ in range(H):
    table.append([".",]+list(input())+[".",])
table.append(["."]*(W+2))
table = np.array(table)
ans = []
#print(table)
for h in range(H):
    for w in range(W):
        mawari = table[h:h+3,w:w+3]
        if mawari.shape != (3,3):
            continue
        #print(mawari)
        if mawari[1,1] == ".":
            ans.append(np.count_nonzero(mawari=="#"))
        else:
            ans.append("#")
ans = np.array(ans).reshape(H,W)
#print(ans)
for i in ans:
    print("".join(i))

Submission status: Runtime Error

Solution code:
import numpy as np
H,W = map(int,input().split())
table = [["."]*(W+2),]
for _ in range(H):
    table.append([".",]+list(input())+[".",])
table.append(["."]*(W+2))
table = np.array(table)
ans = []
#print(table)
for h in range(H):
    for w in range(W):
        mawari = table[h:h+3,w:w+3]
        #print(mawari)
        if mawari[1,1] == ".":
            ans.append(np.count_nonzero(mawari=="#"))
        else:
            ans.append("#")
ans = np.array(ans).reshape(H,W)
#print(ans)
for i in ans:
    print("".join(i))

Submission status: Runtime Error

Solution code:
H = int(input())
W = int(input())
name ={}
count = 0
for i in range(H):
  name[i] = input()
  for j in range(W):
    if name[i][j] ==".":
      count = 0
      for k in range(i-1,i+2):
        for l in range(j-1,j+2):
          if name[k][l] == "#":
            count += 1
      name[i][j] = count
for i in range(H):
  print(name[i])

Submission status: Runtime Error

Solution code:
dx=[1,1,1,0,0,-1,-1,-1]
dy=[-1,0,1,-1,1,-1,0,1]
n,m=map(int,input().split())
s=list()
for i in range(n):
	s.append(input())
ans=[[0 for i in range(n+5)]for i in range(m+5)]
for i in range(n):
	for j in range(m):
		if s[i][j]=='#':
			ans[i][j]='#';
		else:
			t=0
			for k in range(8):
				xx=i+dx[k]
				yy=j+dy[k]
				if xx>=0 and yy>=0 and xx<n and yy<m:
					if s[xx][yy]=='#':
						t+=1
			ans[i][j]=t

for i in range(n):
	for j in range(m):
		print(ans[i][j],end='')
	print('')

Submission status: Runtime Error

Solution code:
dx=[1,1,1,0,0,-1,-1,-1]
dy=[-1,0,1,-1,1,-1,0,1]
n,m=map(int,input().split())
s=list()
for i in range(n):
	s.append(input())
ans=[[0 for i in range(n+5)]for i in range(m+5)]
for i in range(n):
	for j in range(m):
		if s[i][j]=='#':
			ans[i][j]='#';
		else:
			t=0
			for k in range(8):
				xx=i+dx[k]
				yy=j+dy[k]
				if xx>=0 and yy>=0 and xx<n and yy<m and s[xx][yy]=='#':
					t+=1
			ans[i][j]=t

for i in range(n):
	for j in range(m):
		print(ans[i][j],end='')
	print('')

Submission status: Runtime Error

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

for i in range(H):
  for j in range(W):
    if s[i][j] == '#':
      continue
    cnt = 0
    for n in range(8):
      ni = i + dx[n]
      nj = j + dy[n]
      if ni < 0 or H <= ni or nj < 0 or W <= nj:
        continue
      if S[ni][nj] == '#':
        cnt += 1
    S[i][j] = str(cnt)


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

Submission status: Runtime Error

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

for i in range(H):
  for j in range(W):
    if s[i][j] == '#':
      continue
    cnt = 0
    for n in range(8):
      ni = i + dx[n]
      nj = j + dy[n]
      if ni < 0 or H <= ni or nj < 0 or W <= nj:
        continue
      if s[ni][nj] == '#':
        cnt += 1
    S[i][j] = str(cnt)


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

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  for j in range(W):
    if s[i][j] == '#':
      continue
    cnt = 0
    for n in range(8):
      ni = i + dx[n]
      nj = j + dy[n]
      if ni < 0 or H <= ni or nj < 0 or W <= nj:
        continue
      if s[ni][nj] == '#':
        cnt += 1
    S[i][j] = str(cnt)


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

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      cnt = 0
      for n in range(8):
        ni = i + dx[n]
        nj = j + dy[n]
        if ni < 0 or H <= ni or nj < 0 or W <= nj:
          continue
        if s[ni][nj] == '#':
          cnt += 1
      S[i][j] = str(cnt)

print(S)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      cnt = 0
      for n in range(8):
        ni = i + dx[n]
        nj = j + dy[n]
        if ni < 0 or ni >= H:
          continue
        if nj < 0 or nj >= W:
          continue
        if s[ni][nj] == '#':
          cnt += 1
      S[i][j] = str(cnt)

print(S)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [list(map(int, input().split())) for i in range(H)]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      cnt = 0
      for n in range(8):
        ni = i + dx[n]
        nj = j + dy[n]
        if ni < 0 or ni >= H:
          continue
        if nj < 0 or nj >= W:
          continue
        if s[ni][nj] == '#':
          cnt += 1
      S[i][j] = str(cnt)

print(S)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [list(map(int, input().split())) for i in range(H)]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      cnt = 0
      for n in range(8):
        ni = i + dx[n]
        nj = j + dy[n]
        if ni < 0 or ni >= H:
          continue
        if nj < 0 or nj >= W:
          continue
        if s[ni][nj] == '#':
          cnt += 1
      S[i][j] = cnt

print(S)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [list(map(int, input().split())) for i in range(H)]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      cnt = 0
      for n in range(8):
        ni = i + dx
        nj = j + dy
        if ni < 0 or ni >= H:
          continue
        if nj < 0 or nj >= W:
          continue
        if s[ni][nj] == '#':
          cnt += 1
      S[i][j] = cnt

print(S)

Submission status: Runtime Error

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

for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      cnt = 0
      if S[i-1][j-1] == '#':
        cnt += 1
      if S[i-1][j] == '#':
        cnt += 1
      if S[i-1][j+1] == '#':
        cnt += 1
      if S[i][j-1] == '#':
        cnt += 1
      if S[i][j+1] == '#':
        cnt += 1
      if S[i+1][j-1] == '#':
        cnt += 1
      if S[i+1][j] == '#':
        cnt += 1
      if S[i+1][j+1] == '#':
        cnt += 1
      S[i][j] = cnt
    
    
print(S)

Submission status: Runtime Error

Solution code:
# ABC075B

import numpy as np

h, w = map(int, input().split())
board = sp.reshape(np.array(np.repeat("@", (h+2)*(w+2))), (h+2, w+2))
for i in range(h):
    s = input()
    board[i+1, 1:w+1] = np.array(list(s))
for i in range(1, h+1):
    count = np.zeros(w, dtype=str)
    for j in range(1, w+1):
        if board[i,j] == '.':
            count[j-1] = str(\
                (board[i-1, j-1] == "#") + (board[i-1, j] == "#") + (board[i-1, j+1] == "#") +\
                (board[i  , j-1] == "#") + (board[i  , j] == "#") + (board[i  , j+1] == "#") +\
                (board[i+1, j-1] == "#") + (board[i+1, j] == "#") + (board[i+1, j+1] == "#"))
        elif board[i, j] == "#":
            count[j-1] = "#"
    print(''.join(count))

Submission status: Runtime Error

Solution code:
# ABC075B

import numpy as np

h, w = map(int, input().split())
board = sp.reshape(np.array(sp.repeat("@", (h+2)*(w+2))), (h+2, w+2))
for i in range(h):
    s = input()
    board[i+1, 1:w+1] = np.array(list(s))
for i in range(1, h+1):
    count = np.zeros(w, dtype=str)
    for j in range(1, w+1):
        if board[i,j] == '.':
            count[j-1] = str(\
                (board[i-1, j-1] == "#") + (board[i-1, j] == "#") + (board[i-1, j+1] == "#") +\
                (board[i  , j-1] == "#") + (board[i  , j] == "#") + (board[i  , j+1] == "#") +\
                (board[i+1, j-1] == "#") + (board[i+1, j] == "#") + (board[i+1, j+1] == "#"))
        elif board[i, j] == "#":
            count[j-1] = "#"
    print(''.join(count))

Submission status: Runtime Error

Solution code:
# ABC075B

import numpy as np

h, w = map(int, input().split())
board = sp.reshape(np.array(sp.repeat("@", (h+2)*(w+2))), (h+2, w+2))
for i in range(h):
    s = input()
    board[i+1, 1:w+1] = np.array(list(s))
for i in range(1, h+1):
    count = sp.zeros(w, dtype=str)
    for j in range(1, w+1):
        if board[i,j] == '.':
            count[j-1] = str(\
                (board[i-1, j-1] == "#") + (board[i-1, j] == "#") + (board[i-1, j+1] == "#") +\
                (board[i  , j-1] == "#") + (board[i  , j] == "#") + (board[i  , j+1] == "#") +\
                (board[i+1, j-1] == "#") + (board[i+1, j] == "#") + (board[i+1, j+1] == "#"))
        elif board[i, j] == "#":
            count[j-1] = "#"
    print(''.join(count))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
newS = [['#' for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            c = max(0, j-1)
            d = min(W-1, j+1)
            if i == 0:
                cnt = S[i][c:d+1].count('#')
                cnt += S[i+1][c:d+1].count('#')
            elif i == H-1:
                cnt = S[i][c:d+1].count('#')
                cnt += S[i-1][c:d+1].count('#')
            else:
                cnt = S[i][c:d+1].count('#')
                cnt += S[i-1][c:d+1].count('#')
                cnt += S[i+1][c:d+1].count('#')
            newS[i][j] = str(cnt)
for i in newS:
    print(''.join(i))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
newS = [['#']*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            c = max(0, j-1)
            d = min(W-1, j+1)
            if i == 0:
                cnt = S[i][c:d+1].count('#')
                cnt += S[i+1][c:d+1].count('#')
            elif i == H-1:
                cnt = S[i][c:d+1].count('#')
                cnt += S[i-1][c:d+1].count('#')
            else:
                cnt = S[i][c:d+1].count('#')
                cnt += S[i-1][c:d+1].count('#')
                cnt += S[i+1][c:d+1].count('#')
            newS[i][j] = str(cnt)
for i in newS:
    print(''.join(i))

Submission status: Runtime Error

Solution code:
mine = []
h, w = map(int, input().split())
for i in range(h):
    mine.append(list(input()[:]))
ans = [[0 for i in range(w)] for j in range(w)]
for i in range(h):
    for j in range(w):
        if mine[i][j] == '#':
            if i != 0:
                ans[i-1][j] += 1
            if j != 0:
                ans[i][j-1] += 1
            if i != h-1:
                ans[i+1][j] += 1
            if j != w-1:
                ans[i][j+1] += 1
            if i != 0 and j != 0:
                ans[i-1][j-1] += 1
            if i != 0 and j != w-1:
                ans[i-1][j+1] += 1
            if i != h-1 and j != 0:
                ans[i+1][j-1] += 1
            if i != h-1 and j != w-1:
                ans[i+1][j+1] += 1

for i in range(h):
    for j in range(w):
        if mine[i][j] == "#":
            print("#", end="")
        else:
            print(ans[i][j], end="")
    print()

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[input() for _ in range(h)]
l=[[] 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('#')
                l[i].append(str(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('#')
                l[i].append(str(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]]
                k=l1j.count('#')
                l[i].append(str(k))
            else:l[i].append('#')
        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('#')
                l[i].append(str(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('#')
                l[i].append(str(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('#')
                l[i].append(str(lhjj))
            else:l[i].append('#')
        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('#')
                l[i].append(str(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('#')
                l[i].append(str(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('#')
                l[i].append(str(lijj))
            else:l[i].append('#')
for i in range(h):
    print("".join(l[i]))