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 = [list(input()) for _ in range(H)]

for i in range(H):
    for j in range(W):
        count = 0
        
        if i == 0 and H != 1:
            if j == 0:
                count = [S[i][j+1], S[i+1][j], S[i+1][j+1]].count('#')
            elif j == W - 1:
                count = [S[i][j-1], S[i+1][j], S[i+1][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1], S[i+1][j-1], S[i+1][j], S[i+1][j+1]].count('#')
        elif i == 0 and H == 1:
            if j == 0:
                count = [S[i][j+1]].count('#')
            elif j == W - 1:
                count = [S[i][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1]].count('#')

        elif i == H - 1:
            if j == 0:
                count = [S[i][j+1], S[i-1][j], S[i-1][j+1]].count('#')
            elif j == W - 1:
                count = [S[i-1][j-1], S[i-1][j], S[i][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1], S[i-1][j-1], S[i-1][j], S[i-1][j+1]].count('#')
        else:
            if j == 0:
                count = [S[i-1][j], S[i-1][j+1], S[i][j+1], S[i+1][j], S[i+1][j+1]].count('#')
            elif j == W - 1:
                count =[S[i-1][j-1], S[i-1][j], S[i][j-1], S[i+1][j-1], S[i+1][j]].count('#')
            else:
                count =[S[i-1][j-1], S[i-1][j], S[i-1][j+1], S[i][j-1], S[i][j+1], S[i+1][j-1], S[i+1][j], S[i+1][j+1]].count('#')
        if S[i][j] != '#':
            S[i][j] = str(count)
for s in S:
    print("".join(s))

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):
        count = 0
        
        if i == 0:
            if j == 0:
                count = [S[i][j+1], S[i+1][j], S[i+1][j+1]].count('#')
            elif j == W - 1:
                count = [S[i][j-1], S[i+1][j], S[i+1][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1], S[i+1][j-1], S[i+1][j], S[i+1][j+1]].count('#')

        elif i == H - 1:
            if j == 0:
                count = [S[i][j+1], S[i-1][j], S[i-1][j+1]].count('#')
            elif j == W - 1:
                count = [S[i-1][j-1], S[i-1][j], S[i][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1], S[i-1][j-1], S[i-1][j], S[i-1][j+1]].count('#')
        else:
            if j == 0:
                count = [S[i-1][j], S[i-1][j+1], S[i][j+1], S[i+1][j], S[i+1][j+1]].count('#')
            elif j == W - 1:
                count =[S[i-1][j-1], S[i-1][j], S[i][j-1], S[i+1][j-1], S[i+1][j]].count('#')
            else:
                count =[S[i-1][j-1], S[i-1][j], S[i-1][j+1], S[i][j-1], S[i][j+1], S[i+1][j-1], S[i+1][j], S[i+1][j+1]].count('#')
        if S[i][j] != '#':
            S[i][j] = str(count)
for s in S:
    print("".join(s))

Submission status: Runtime Error

Solution code:
def incre(a, i, j):
    if a[i][j] != '#':
        a[i][j] += 1


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

a = [[0]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            a[i][j] = '#'

for i in range(h):
    for j in range(w):
        if (s[i][j] == '#'):
            if i == 0:
                if j == 0:
                    incre(a, i, j+1)
                    incre(a, i+1, j)
                    incre(a, i+1, j+1)
                elif 0 < j < w - 1:
                    incre(a, i, j-1)
                    incre(a, i, j+1)
                    incre(a, i+1, j-1)
                    incre(a, i+1, j)
                    incre(a, i+1, j+1)

                elif j == w-1:
                    incre(a, i, j-1)
                    incre(a, i-1, j-1)
                    incre(a, i+1, j)
            elif 0 < i < h - 1:
                if j == 0:
                    incre(a, i-1, j)
                    incre(a, i-1, j+1)
                    incre(a, i, j+1)
                    incre(a, i+1, j)
                    incre(a, i+1, j+1)
                elif 0 < j < w - 1:
                    incre(a, i-1, j-1)
                    incre(a, i-1, j)
                    incre(a, i-1, j+1)
                    incre(a, i, j-1)
                    incre(a, i, j+1)
                    incre(a, i+1, j-1)
                    incre(a, i+1, j)
                    incre(a, i+1, j+1)
                elif j == w-1:
                    incre(a, i-1, j)
                    incre(a, i-1, j-1)
                    incre(a, i, j-1)
                    incre(a, i+1, j-1)
                    incre(a, i+1, j)
            elif i == h-1:
                if j == 0:
                    incre(a, i-1, j)
                    incre(a, i-1, j+1)
                    incre(a, i, j+1)
                elif 0 < j < w - 1:
                    incre(a, i-1, j-1)
                    incre(a, i-1, j)
                    incre(a, i-1, j+1)
                    incre(a, i, j-1)
                    incre(a, i, j+1)
                elif j == w-1:
                    incre(a, i-1, j-1)
                    incre(a, i-1, j)
                    incre(a, i, j-1)

[print(*i, sep="") for i in a]

Submission status: Runtime Error

Solution code:
import sys
import collections

size = sys.stdin.readline().split(' ')
S = int(size[0])
H = int(size[1])


data = sys.stdin.readlines()
for i in range(len(data)):
	data[i] = [j.replace('\n','') for j in data[i]]


if S == 1 and H == 1:
	if data[0][0] == ".":
		data[0][0] = "0"
elif S == 1 and H > 1:
	for j in range(H):
		if data[0][j] == "#":
			continue
		else:
			if j == 0:
				list = [data[0][j+1]]
				count = str(list.count("#"))
				data[0][j] = count
			elif j == (H-1):
				list = [data[0][j-1]]
				count = str(list.count("#"))
				data[0][j] = count
			else:
				list = [data[0][j-1], data[0][j+1]]
				count = str(list.count("#"))
				data[0][j] = count
elif S > 1 and H == 1:
	for i in range(S):
		if data[i][0] == "#":
			continue
		else:
			if i == 0:
				list = [data[i+1][0]]
				count = str(list.count("#"))
				data[i][0] = count
			elif i == (S-1):
				list = [data[i-1][0]]
				count = str(list.count("#"))
				data[i][0] = count	
			else:
				list = [data[i-1][0], data[i+1][0]]
				count = str(list.count("#"))
				data[0][j] = count					
else:
	for i in range(S):
		for j in range(H):
			if data[i][j] == "#":
				continue
			else:
				#上左
				if i == 0 and j == 0:
					list = [data[i+1][j], data[i+1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				#下右
				elif i == (S-1) and j == (H-1):
					list = [data[i-1][j], data[i-1][j-1], data[i][j-1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 上右
				elif i == 0 and j == (H-1):
					list = [data[i][j-1], data[i+1][j-1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 下左
				elif i == (S-1) and j == 0:
					list = [data[i-1][j], data[i-1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 左
				elif (1 <= i < (S-1)) and j == 0:
					list = [data[i-1][j], data[i-1][j+1], data[i][j+1], data[i+1][j+1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 右
				elif (1 <= i < (S-1)) and j == (H-1):
					list = [data[i-1][j], data[i-1][j-1], data[i][j-1], data[i+1][j-1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 上
				elif i == 0 and (1<= j < (H-1)):
					list = [data[i][j-1], data[i+1][j-1], data[i+1][j], data[i+1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 下
				elif i == (S-1) and (1<= j < (H-1)):
					list = [data[i][j-1], data[i-1][j-1], data[i-1][j], data[i-1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 真ん中
				else:
					list = [data[i][j-1], data[i-1][j-1],data[i-1][j],data[i-1][j+1],data[i][j+1],data[i+1][j+1],data[i+1][j],data[i+1][j-1]]
					count = str(list.count("#"))
					data[i][j] = count



for i in range(len(data)):
	ans = str(data[i]).replace('[', '').replace(']', '').replace(',', '').replace("'", '').replace(' ', '')
	print(ans)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0

    # 左と右
    if index % col != 0:# 0列でない
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:# 末列でない
        if newLine[index + 1] == "#":
            count += 1
    # 上と下
    if index > col: #首行でない
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row - 1):#末行でない
        if newLine[index + col] == "#":
            count += 1

    # 上の左と右 0 <= index + col + 1 < len(newLine) and
    if index > col and index % col != 0: #首行でない かつ　0列でない
        if newLine[index - col - 1] == "#":
            count += 1
    if index > col and index % col != col - 1: #首行でない かつ　末列でない
        if newLine[index - col + 1] == "#":
            count += 1
    # 下の左と右
    if index < col * (row - 1) and index % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if index < col * (row - 1) and index % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    # lines = ['6 5', '.##.#', '.####', '..##.', '..#..', '.....', '.....']
    #lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
    	lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0

    # 左と右
    if index % col != 0:# 0列でない
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:# 末列でない
        if newLine[index + 1] == "#":
            count += 1
    # 上と下
    if index >= col: #首行でない
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row - 1):#末行でない
        if newLine[index + col] == "#":
            count += 1

    # 上の左と右 0 <= index + col + 1 < len(newLine) and
    if index >= col and index % col != 0: #首行でない かつ　0列でない
        if newLine[index - col - 1] == "#":
            count += 1
    if index >= col and index % col != col - 1: #首行でない かつ　末列でない
        if newLine[index - col + 1] == "#":
            count += 1
    # 下の左と右
    if index < col * (row - 1) and index % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if index < col * (row - 1) and index % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    #lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    # lines = ['6 5', '.##.#', '.####', '..##.', '..#..', '.....', '.....']
    #lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0
    # 左と右
    if index % col != 0:
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:
        if newLine[index + 1] == "#":
            count += 1
    # 上と下
    if index > col:
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row - 1):
        if newLine[index + col] == "#":
            count += 1

    # 上の左と右 0 <= index + col + 1 < len(newLine) and
    if index > col and index % col != 0:
        if newLine[index - col - 1] == "#":
            count += 1
    if index > col and index % col != col - 1:
        if newLine[index - col + 1] == "#":
            count += 1
    # 下の左と右
    if index < col * (row - 1) and index % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if index < col * (row - 1) and index % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    # lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    # lines = ['6 5', '.##.#', '.####', '..##.', '..#..', '.....', '.....']
    # lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
   	for l in sys.stdin:
       lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0
    # 左と右
    if index % col != 0:
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:
        if newLine[index + 1] == "#":
            count += 1
    # 上と下
    if index > col:
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row - 1):
        if newLine[index + col] == "#":
            count += 1

    # 上の左と右
    if 0 <= index - col - 1 < len(newLine) and index > col and index % col != 0:
        if newLine[index - col - 1] == "#":
            count += 1
    if 0 <= index - col + 1 < len(newLine) and index > col and index % col != col - 1:
        if newLine[index - col + 1] == "#":
            count += 1
    # 下の左と右
    if 0 <= index + col - 1 < len(newLine) and index < col * (row - 1) and index % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if 0 <= index + col + 1 < len(newLine) and index < col * (row - 1) and index % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    # lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    # lines = ['6 5', '.##.#', '.####', '..##.', '..#..', '.....', '.....']
    # lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0
    # 左と右
    if index % col != 0:
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:
        if newLine[index + 1] == "#":
            count += 1
    # 上と下
    if index > col:
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row - 1):
        if newLine[index + col] == "#":
            count += 1
    # 上の左と右
    if 0 <= index - col - 1 < len(newLine) and index > col and index % col != 0:
        if newLine[index - col - 1] == "#":
            count += 1
    if 0 <= index - col + 1 < len(newLine) and index < col * (row - 1) and index % col != col - 1:
        if newLine[index - col + 1] == "#":
            count += 1
    # 下の左と右
    if 0 <= index + col - 1 < len(newLine) and index > col and index % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if 0 <= index + col + 1 < len(newLine) and index < col * (row - 1) and index % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    # lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    # lines = ['6 5', '.##.#', '.####', '..##.', '..#..', '.....', '.....']
    # lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0
    # 左と右
    if index % col != 0:
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:
        if newLine[index + 1] == "#":
            count += 1
    # 上と下
    if index > col:
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row - 1):
        if newLine[index + col] == "#":
            count += 1
    # 上の左と右
    if 0 <= index - col - 1 < len(newLine) and index - col % col != 0:
        if newLine[index - col - 1] == "#":
            count += 1
    if 0 <= index - col + 1 < len(newLine) and index - col % col != col - 1:
        if newLine[index - col + 1] == "#":
            count += 1
    # 下の左と右
    if 0 <= index + col - 1 < len(newLine) and index + col % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if 0 <= index + col + 1 < len(newLine) and index + col % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    # lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    # lines = ['6 5', '.##.#', '.####', '..##.', '..#..', '.....', '.....']
    # lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0

    if index % col != 0:
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:
        if newLine[index + 1] == "#":
            count += 1

    if index > col:
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row-1):
        if newLine[index + col] == "#":
            count += 1

    if 0 <= index - col - 1 < len(newLine) and index % col != 0:
        if newLine[index - col - 1] == "#":
            count += 1
    if 0 <= index - col + 1 < len(newLine) and index % col != col - 1:
        if newLine[index - col + 1] == "#":
            count += 1

    if 0 <= index + col - 1 < len(newLine) and index % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if 0 <= index + col + 1 < len(newLine) and index % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    # lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    #lines = ['6 5', '.##.#', '.####', '..##.', '..#..', '.....', '.....']
    #lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
      lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, row, col):
    count = 0

    if index % col != 0:
        if newLine[index - 1] == "#":
            count += 1
    if index % col != col - 1:
        if newLine[index + 1] == "#":
            count += 1

    if index > col:
        if newLine[index - col] == "#":
            count += 1
    if index < col * (row-1):
        if newLine[index + col] == "#":
            count += 1

    if 0 <= index - col - 1 < len(newLine) and index % col != 0:
        if newLine[index - col - 1] == "#":
            count += 1
    if 0 <= index - col + 1 < len(newLine) and index % col != col - 1:
        if newLine[index - col + 1] == "#":
            count += 1

    if 0 <= index + col - 1 < len(newLine) and index % col != 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if 0 <= index + col + 1 < len(newLine) and index % col != col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    # lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    #lines = ['6 5', '.##.#', '.####', '..##.', '..#..', '.....', '.....']
    # lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
       lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, value, col):
    count = 0
    if 0 <= index - 1 < len(newLine) and index % col > 0:
        if newLine[index - 1] == "#":
            count += 1
    if 0 <= index + 1 < len(newLine) and index % col < col - 1:
        if newLine[index + 1] == "#":
            count += 1

    if 0 <= index - col < len(newLine):
        if newLine[index - col] == "#":
            count += 1
    if 0 <= index + col < len(newLine):
        if newLine[index + col] == "#":
            count += 1

    if 0 <= index - col - 1 < len(newLine) and index % col > 0:
        if newLine[index - col - 1] == "#":
            count += 1
    if 0 <= index - col + 1 < len(newLine) and index % col < col - 1:
        if newLine[index - col + 1] == "#":
            count += 1

    if 0 <= index + col - 1 < len(newLine) and index % col > 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if 0 <= index + col + 1 < len(newLine) and index % col < col - 1:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, value, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    #lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    #lines = ['3 5', '###.#', '#####', '#.##.']
    #lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
       lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, value, col):
    count = 0
    if 0 <= index - 1 < len(newLine) and index % col >= 0:
        if newLine[index - 1] == "#":
            count += 1
    if 0 <= index + 1 < len(newLine) and index % col <= 5:
        if newLine[index + 1] == "#":
            count += 1

    if 0 <= index - col < len(newLine):
        if newLine[index - col] == "#":
            count += 1
    if 0 <= index + col < len(newLine):
        if newLine[index + col] == "#":
            count += 1

    if 0 <= index - col - 1 < len(newLine) and index % col >= 0:
        if newLine[index - col - 1] == "#":
            count += 1
    if 0 <= index - col + 1 < len(newLine) and index % col <= 5:
        if newLine[index - col + 1] == "#":
            count += 1

    if 0 <= index + col - 1 < len(newLine) and index % col >= 0:
        if newLine[index + col - 1] == "#":
            count += 1
    if 0 <= index + col + 1 < len(newLine) and index % col <= 5:
        if newLine[index + col + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, value, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    #lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    #lines = ['3 5', '#####', '#####', '#####']
    #lines = ['3 5', '.....', '.#.#.', '.....']

    lines = []
    for l in sys.stdin:
       lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, value, row, col):
    count = 0
    if 0 <= index - 1 < len(newLine) and index % row > 0:
        if newLine[index - 1] == "#":
            count += 1
    if 0 <= index + 1 < len(newLine) and index % row < 5:
        if newLine[index + 1] == "#":
            count += 1

    if 0 <= index - row < len(newLine):
        if newLine[index - row] == "#":
            count += 1
    if 0 <= index + row < len(newLine):
        if newLine[index + row] == "#":
            count += 1

    if 0 <= index - row - 1 < len(newLine) and index % row > 0:
        if newLine[index - row - 1] == "#":
            count += 1
    if 0 <= index - row + 1 < len(newLine) and index % row < 5:
        if newLine[index - row + 1] == "#":
            count += 1

    if 0 <= index + row - 1 < len(newLine) and index % row > 0:
        if newLine[index + row - 1] == "#":
            count += 1
    if 0 <= index + row + 1 < len(newLine) and index % row < 5:
        if newLine[index + row + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, value, row, col)
    for i in range(0, len(newLine), col):
        print("".join(newLine[i:i + col]))


if __name__ == '__main__':
    #lines = ['6 6', '#####.', '#.#.##', '####.#', '.#..#.', '#.##..', '#.#...']
    #lines = ['3 5', '#####', '#####', '#####']
    lines = []
    for l in sys.stdin:
       lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
import sys


def check(newLine, index, value, row, col):
    count = 0
    if 0 <= index - 1 < len(newLine) and index % row > 0:
        if newLine[index - 1] == "#":
            count += 1
    if 0 <= index + 1 < len(newLine) and index % row < 5:
        if newLine[index + 1] == "#":
            count += 1

    if 0 <= index - row < len(newLine):
        if newLine[index - row] == "#":
            count += 1
    if 0 <= index + row < len(newLine):
        if newLine[index + row] == "#":
            count += 1

    if 0 <= index - row - 1 < len(newLine) and index % row > 0:
        if newLine[index - row - 1] == "#":
            count += 1
    if 0 <= index - row + 1 < len(newLine) and index % row < 5:
        if newLine[index - row + 1] == "#":
            count += 1

    if 0 <= index + row - 1 < len(newLine) and index % row > 0:
        if newLine[index + row - 1] == "#":
            count += 1
    if 0 <= index + row + 1 < len(newLine) and index % row < 5:
        if newLine[index + row + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][2])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, value, row, col)
    for i in range(0, len(newLine), row):
        print("".join(newLine[i:i + row]))


if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)