Problem:


爆弾の連鎖

縦 8、横 8 のマスからなる図1 のような平面があります。その平面上に、いくつかの爆弾が置かれています。図2 にその例を示します（● = 爆弾）。

	

               
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	

       	       
□□□●□□●□

	       
□□□□□●□□

	       
●□□□●□□●

	       
□□●□□□●□

	       
□●□□□□□□

	       
□□□□●□□□

	       
●□●□□□●□

	       
□●□●□□●□

	       
	

図1図2

爆弾が爆発すると、その爆弾の上下左右 3 マスに爆風の影響が及び、それらのマスに置かれている爆弾も連鎖的に爆発します。たとえば、図 3 に示す爆弾が爆発すると図 4 の■のマスに爆風の影響が及びます。

	

               
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	       
□□□●□□□□

	       
□□□□□□□□

	       
□□□□□□□□

	

  
□□□□□□□□

	       
□□□□□□□□

	       
□□□■□□□□

	       
□□□■□□□□

	       
□□□■□□□□

	       
■■■●■■■□

	       
□□□■□□□□

	       
□□□■□□□□

	       
	

図3図4

爆弾が置かれている状態と最初に爆発する爆弾の位置を読み込んで、最終的な平面の状態を出力するプログラムを作成してください。

Input

入力は以下の形式で与えられます。

n
（空行）
データセット 1
（空行）
データセット 2
..
..
データセット n

１行目にデータセットの数 n (n ≤ 20) が与えられます。続いて n 個のデータセットが与えられます。各データセットの直前に１つの空行が与えられます。各データセットは以下の形式で与えられます。

g1,1g2,1...g8,1
g1,2g2,2...g8,2
:
g1,8g2,8...g8,8
X
Y

最初の８行に、平面を表す８つの文字列が与えられます。各文字列は８文字からなる数列で、爆弾が置かれているマスを 1、置かれていないマスを 0 で表します。
続く２行で、最初に爆発する爆弾の X 座標と Y 座標が与えられます。左上、左下、右上、右下の座標が、それぞれ、(1, 1)、(1, 8)、(8, 1)、(8, 8) です。例えば、図 4 に示す爆弾が最初に爆発するとき、与えられる座標は (4, 6) です。

Output

各データセットについて以下のように出力して下さい。

爆発しないで残った爆弾のあるマスを 1、爆弾のないマスを 0 で表現することとします。平面の一行を数字 8 個からなる一行とし 8 行の文字列で最終的な平面の状態を出力してください。各データセットの先頭は、サンプル出力のように Data x: から出力しなければなりません。ここで、x はデータセットの番号です。

Sample Input

2

00010010
00000100
10001001
00100010
01000000
00001000
10100010
01010010
2
5

00010010
00000100
10001001
00100010
01000000
00001000
10100010
01010010
2
5

Output for the Sample Input

Data 1:
00000000
00000100
10001001
00100000
00000000
00001000
10100000
00000000
Data 2:
00000000
00000100
10001001
00100000
00000000
00001000
10100000
00000000



Sample input:
2

00010010
00000100
10001001
00100010
01000000
00001000
10100010
01010010
2
5

00010010
00000100
10001001
00100010
01000000
00001000
10100010
01010010
2
5


Sample output:
Data 1:
00000000
00000100
10001001
00100000
00000000
00001000
10100000
00000000
Data 2:
00000000
00000100
10001001
00100000
00000000
00001000
10100000
00000000

Submission status: Accepted

Solution code:
def paint(f_inp, x, y):
    if f_inp[y][x]:
        f_inp[y][x] = False
        if f_inp[y][x - 1]:
            f_inp = paint(f_inp, x - 1, y)
        if f_inp[y][x - 2]:
            f_inp = paint(f_inp, x - 2, y)
        if f_inp[y][x - 3]:
            f_inp = paint(f_inp, x - 3, y)
        if f_inp[y][x + 1]:
            f_inp = paint(f_inp, x + 1, y)
        if f_inp[y][x + 2]:
            f_inp = paint(f_inp, x + 2, y)
        if f_inp[y][x + 3]:
            f_inp = paint(f_inp, x + 3, y)
        if f_inp[y - 1][x]:
            f_inp = paint(f_inp, x, y - 1)
        if f_inp[y - 2][x]:
            f_inp = paint(f_inp, x, y - 2)
        if f_inp[y - 3][x]:
            f_inp = paint(f_inp, x, y - 3)
        if f_inp[y + 1][x]:
            f_inp = paint(f_inp, x, y + 1)
        if f_inp[y + 2][x]:
            f_inp = paint(f_inp, x, y + 2)
        if f_inp[y + 3][x]:
            f_inp = paint(f_inp, x, y + 3)
    return f_inp

i_data = 1
n = int(input())
for i in range(n):
    _ = input()
    print("Data " + str(i_data) + ":")
    f = [[False for i in range(14)] for j in range(14)]
    for i in range(8):
        inp = input()
        for j in range(8):
            if inp[j] == "1":
                f[i + 3][j + 3] = True
    x = int(input())
    y = int(input())
    f = paint(f, x + 2, y + 2)
    for i in range(8):
        s = ""
        for j in range(8):
            if f[i + 3][j + 3]:
                s = s + "1"
            else:
                s = s + "0"
        print(s)
    i_data += 1

Submission status: Accepted

Solution code:
def e(x,y):
 A[y][x]='0'
 for dx,dy in[[-3,0],[-2,0],[-1,0],[1,0],[2,0],[3,0],[0,-3],[0,-2],[0,-1],[0,1],[0,2],[0,3]]:
  if 0<=x+dx<8 and 0<=y+dy<8 and A[y+dy][x+dx]=='1':e(x+dx,y+dy)

for i in range(int(input())):
 print(f'Data {i+1}:')
 input()
 A=[list(input())for _ in[0]*8]
 e(int(input())-1,int(input())-1)
 for r in A:print(*r,sep='')

Submission status: Accepted

Solution code:
def e(x,y):
 A[y][x]='0'
 for dx,dy in[[-3,0],[-2,0],[-1,0],[1,0],[2,0],[3,0],[0,-3],[0,-2],[0,-1],[0,1],[0,2],[0,3]]:
  if 0<=x+dx<8 and 0<=y+dy<8 and A[y+dy][x+dx]=='1':e(x+dx,y+dy)

for i in range(int(input())):
 print(f'Data {i+1}:')
 input()
 A=[list(input())for _ in[0]*8]
 e(int(input())-1,int(input())-1)
 for r in A:print(''.join(r))

Submission status: Accepted

Solution code:
def e(x,y):
 A[y][x]='0'
 for d in range(-3,4):
  if 0<=x+d<8 and A[y][x+d]=='1':e(x+d,y)
  if 0<=y+d<8 and A[y+d][x]=='1':e(x,y+d)

for i in range(int(input())):
 print(f'Data {i+1}:')
 input()
 A=[list(input())for _ in[0]*8]
 e(int(input())-1,int(input())-1)
 for r in A:print(*r,sep='')

Submission status: Accepted

Solution code:
def e(x,y):
 A[y][x]='0'
 for d in range(-3,4):
  0<=x+d<8 and A[y][x+d]=='1'and e(x+d,y)
  0<=y+d<8 and A[y+d][x]=='1'and e(x,y+d)
for i in range(int(input())):
 print(f'Data {i+1}:')
 input()
 A=[list(input())for _ in[0]*8]
 e(int(input())-1,int(input())-1)
 for r in A:print(*r,sep='')

Submission status: Accepted

Solution code:
def e(x,y):
 A[y][x]='0'
 for d in range(-3,4):
  0<=x+d<8and A[y][x+d]=='1'and e(x+d,y)
  0<=y+d<8and A[y+d][x]=='1'and e(x,y+d)
for i in range(int(input())):
 print(f'Data {i+1}:')
 input()
 A=[list(input())for _ in[0]*8]
 e(int(input())-1,int(input())-1)
 for r in A:print(*r,sep='')

Submission status: Accepted

Solution code:
def e(x,y):
 A[y][x]='0'
 for d in range(-3,4):
  s,t=x+d,y+d
  0<=s<8and A[y][s]=='1'and e(s,y)
  0<=t<8and A[t][x]=='1'and e(x,t)
for i in range(int(input())):
 print(f'Data {i+1}:')
 input()
 A=[list(input())for _ in[0]*8]
 e(int(input())-1,int(input())-1)
 for r in A:print(*r,sep='')

Submission status: Accepted

Solution code:
def e(x,y):
 A[y][x]='0'
 for d in range(-3,4):
  0<=x+d<8and'1'==A[y][x+d]and e(x+d,y)
  0<=y+d<8and'1'==A[y+d][x]and e(x,y+d)
for i in range(int(input())):
 print(f'Data {i+1}:');input()
 A=[list(input())for _ in[0]*8]
 e(int(input())-1,int(input())-1)
 for r in A:print(*r,sep='')

Submission status: Accepted

Solution code:
def b(x,y):
 A[y][x]='0'
 for d in range(-3,4):
  0<=x+d<8and'1'==A[y][x+d]and b(x+d,y)
  0<=y+d<8and'1'==A[y+d][x]and b(x,y+d)
e=input
for i in range(int(e())):
 print(f'Data {i+1}:')
 e()
 A=[list(e())for _ in[0]*8]
 b(int(e())-1,int(e())-1)
 for r in A:print(*r,sep='')

Submission status: Accepted

Solution code:
direct = ((0, -1), (0, -2), (0, -3), (0, 1), (0, 2), (0, 3),
          (-1, 0), (-2, 0), (-3, 0), (1, 0), (2, 0), (3, 0))

def bomb(x, y, mp):
  mp[y][x] = "0"
  for dx, dy in direct:
    nx, ny = x + dx, y + dy
    if 0 <= nx <= 7 and 0 <= ny <= 7 and mp[ny][nx] == "1":
      bomb(nx, ny, mp)

n = int(input())

for i in range(n):
  input()
  mp = [list(input()) for _ in range(8)]
  x, y = int(input()) - 1, int(input()) - 1
  
  bomb(x, y, mp)

  print("Data " + str(i + 1) + ":")
  
  for line in mp:
    print("".join(map(str,line)))

Submission status: Accepted

Solution code:
# AOJ 0071 Bombs Chain
# Python3 2018.6.22 bal4u

n = int(input())
for i in range(n):
	print("Data ", i+1, ":", sep='')
	input()
	a = [[0 for c in range(15)] for r in range(15)]
	for r in range(8): a[r] = list(input())
	Q = []
	c,r = int(input())-1, int(input())-1
	Q.append((r, c))
	while len(Q):
		r, c = Q.pop(0)
		a[r][c] = '0';
		for nr in range(r-3, r+4):
			if nr >= 0 and nr < 8:
				if a[nr][c] == '1':
					Q.append((nr, c))
					a[nr][c] = '2'
		for nc in range(c-3, c+4):
			if nc >= 0 and nc < 8:
				if a[r][nc] == '1':
					Q.append((r, nc))
					a[r][nc] = '2'
	for r in range(8):
		t = ''.join(a[r])
		s = t.replace("2", "0")
		print(s[0:8], sep='')

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

import sys


def board_to_string(board):
    return "\n".join(["".join([str(i) for i in row]) for row in board])


def get_answer(board, positions):
    x, y = positions.pop(0)
    board[y][x] = 0
    explode_range = []

    for i in [j for j in xrange(-3, 4) if j]:
        if (0 <= (x + i) <= 7):
            explode_range.append((x + i, y))
        if (0 <= (y + i) <= 7):
            explode_range.append((x, y + i))

    for explode_pos in explode_range:
        x, y = explode_pos
        if board[y][x] == 1:
            board[y][x] = 0
            positions.append((x, y))
    if positions:
        return get_answer(board, positions)
    else:
        return board


def main():
    s = sys.stdin.read()
    params = [param.rstrip() for param in s.split("\n\n")][1:]

    for i, param in enumerate(params):
        print "Data %d:" % (i + 1)

        lines = param.split("\n")
        board = [map(int, line) for line in lines[:-2]]
        x, y = map(int, lines[-2:])
        x -= 1
        y -= 1
        positions = []
        positions.append((x, y))

        ans_board = get_answer(board, positions)
        print board_to_string(ans_board)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def isOnBoard(x,y):
    return 0<=x<8 and 0<=y<8
for dataset in xrange(input()):
    raw_input()
    board=[0]*8
    for i in xrange(8):
        board[i]=map(int,raw_input())
    x,y=input(),input()
    dx=[-3,-2,-1,0,0,0,1,2,3,0,0,0]
    dy=[0,0,0,-3,-2,-1,0,0,0,1,2,3]
    stack=[(x-1,y-1)]
    board[y-1][x-1]=0
    while stack:
        x,y=stack.pop()
        for i in xrange(len(dx)):
            if isOnBoard(x+dx[i],y+dy[i]):
                if board[y+dy[i]][x+dx[i]]==1:
                    board[y+dy[i]][x+dx[i]]=0
                    stack.append((x+dx[i],y+dy[i]))
    print "Data {}:".format(dataset+1)
    print "\n".join(["".join(map(str,line)) for line in board])

Submission status: Accepted

Solution code:
x=[0,0,0,0,0,0,1,2,3,-1,-2,-3]
y=[1,2,3,-1,-2,-3,0,0,0,0,0,0]

def dfs(ty,tx):
    if L[ty][tx]==1:
        L[ty][tx]=0
        for i,j in zip(x,y):
            dx,dy=tx+i,ty+j
            if 0<=dx<=7 and 0<=dy<=7:
                dfs(dy,dx)
    return

n=input()
for i in range(n):
    skip=raw_input()
    L=[map(int,raw_input()) for j in range(8)]
    ax,ay=input(),input()
    dfs(ay-1,ax-1)
    print "Data %d:"%(i+1)
    for j in range(8):
        print "".join(map(str,L[j]))

Submission status: Accepted

Solution code:
from itertools import chain
explosion = [i for i in range(-3, 4) if i] + [0] * 6

def bomb(x, y):
	field[y][x] = 0
	for dx, dy in zip(explosion, reversed(explosion)):
		nextX = x + dx
		nextY = y + dy
		if nextX in range(0, 8) and nextY in range(0, 8):
			if field[nextY][nextX] == 1:
				bomb(nextX, nextY)
			field[nextY][nextX] = 0

for i in xrange(input()):
	raw_input()
	field = [[int(c) for c in raw_input()] for j in range(8)]
	x = input() - 1
	y = input() - 1
	bomb(x, y)
	print "Data %d:" % (i + 1)
	for f in field:
		print "".join(str(i) for i in f)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin

def bomb(surface, x, y):
    surface[y][x] = 0

    start = (x-3) if (x-3) > 0 else 0
    end = (x+4) if (x+4) < 8 else 8
    for i in range(start, end):
        if surface[y][i]:
            bomb(surface, i, y)

    start = (y-3) if (y-3) > 0 else 0
    end = (y+4) if (y+4) < 8 else 8
    for i in range(start, end):
        if surface[i][x]:
            bomb(surface, x, i)

N = int(stdin.readline())
for n in range(1, N+1):
    stdin.readline()

    surface = []
    for _ in range(8):
        surface.append([int(c) for c in stdin.readline().rstrip()])
    x = int(stdin.readline())
    y = int(stdin.readline())

    bomb(surface, x-1, y-1)

    print('Data {}:'.format(n))
    for line in surface:
        print(''.join(str(d) for d in line))

Submission status: Accepted

Solution code:
import sys

n = input()

for t in range(n):
    ls = raw_input()
    ls = [map(int, list(raw_input())) for x in range(8)]

    sx, sy = input(), input()

    dx = [1,0,-1,0] + [2,0,-2,0] + [3,0,-3,0]
    dy = [0,1,0,-1] + [0,2,0,-2] + [0,3,0,-3]

    def isin(x, y): return 0 <= x < 8 and 0 <= y < 8
    def dfs(x, y):
        ls[y][x] = 0
        for d in range(len(dx)):
            xx, yy = x + dx[d], y + dy[d]
            if isin(xx, yy) and ls[yy][xx] == 1:
                dfs(xx, yy)

    dfs(sx-1, sy-1)

    print "Data {}:".format(t+1)
    for raw in ls:
        for col in raw:
            sys.stdout.write(str(col))
        sys.stdout.write('\n')

Submission status: Accepted

Solution code:
import sys

n = input()

for t in range(n):
    ls = raw_input()
    ls = [map(int, list(raw_input())) for x in range(8)]

    sx, sy = input(), input()

    dx = [1,0,-1,0] + [2,0,-2,0] + [3,0,-3,0]
    dy = [0,1,0,-1] + [0,2,0,-2] + [0,3,0,-3]

    def isin(x, y): return 0 <= x < 8 and 0 <= y < 8

    class state:
        def __init__(self, x, y):
            self.x = x
            self.y = y

    stack = [state(sx-1, sy-1)]
    while len(stack):
        s = stack.pop()
        ls[s.y][s.x] = 0
        for i in range(len(dx)):
            xx = s.x + dx[i]
            yy = s.y + dy[i]
            if isin(xx, yy) and ls[yy][xx] == 1:
                stack.append(state(xx, yy))

    print "Data {}:".format(t+1)
    for raw in ls:
        for col in raw:
            sys.stdout.write(str(col))
        sys.stdout.write('\n')

Submission status: Accepted

Solution code:
import sys
from collections import deque

n = input()

for t in range(n):
    ls = raw_input()
    ls = [map(int, list(raw_input())) for x in range(8)]

    sx, sy = input(), input()

    dx = [1,0,-1,0] + [2,0,-2,0] + [3,0,-3,0]
    dy = [0,1,0,-1] + [0,2,0,-2] + [0,3,0,-3]

    def isin(x, y): return 0 <= x < 8 and 0 <= y < 8

    class state:
        def __init__(self, x, y):
            self.x = x
            self.y = y

    queue = deque([state(sx-1, sy-1)])
    while len(queue):
        s = queue.popleft()
        ls[s.y][s.x] = 0
        for i in range(len(dx)):
            xx = s.x + dx[i]
            yy = s.y + dy[i]
            if isin(xx, yy) and ls[yy][xx] == 1:
                queue.append(state(xx, yy))

    print "Data {}:".format(t+1)
    for raw in ls:
        for col in raw:
            sys.stdout.write(str(col))
        sys.stdout.write('\n')

Submission status: Accepted

Solution code:
def bomb(x,y):
    global M
    s=M[y+2]
    R=[-3,-2,-1,1,2,3]
    if s[x+2]=="0":return
    M[y+2]=s[:x+2]+"0"+s[x+3:]
    for d in R:
        bomb(x+d,y)
        bomb(x,y+d)
    return

A=range(14)
B=range(8)
M=["00000000000000" for i in A]
z="000"
n=input()
for i in range(n):
    s=raw_input()
    for j in B:
        M[j+3]=z+raw_input()+z
    x=input()
    y=input()
    bomb(x,y)
    print "Data %d:" %(i+1)
    for j in B:
        print M[j+3][3:-3]

Submission status: Accepted

Solution code:
def bomb(x,y):
    global M
    s=M[y+2]
    R=[-3,-2,-1,1,2,3]
    if s[x+2]=="0":return
    M[y+2]=s[:x+2]+"0"+s[x+3:]
    for d in R:
        bomb(x+d,y)
        bomb(x,y+d)
    return

A=range(14)
B=range(3,11)
M=["00000000000000" for i in A]
z="000"
n=input()
for i in range(n):
    s=raw_input()
    for j in B:
        M[j]=z+raw_input()+z
    x=input()
    y=input()
    bomb(x,y)
    print "Data %d:" %(i+1)
    for j in B:
        print M[j][3:-3]

Submission status: Accepted

Solution code:
def bomb(x,y):
    global M
    s=M[y]
    R=[-3,-2,-1,1,2,3]
    if s[x]=="0":return
    M[y]=s[:x]+"0"+s[x+1:]
    for d in R:
        bomb(x+d,y)
        bomb(x,y+d)
    return

A=range(14)
B=range(3,11)
M=["00000000000000" for i in A]
z="000"
n=input()
for i in range(n):
    s=raw_input()
    for j in B:
        M[j]=z+raw_input()+z
    x=input()+2
    y=input()+2
    bomb(x,y)
    print "Data %d:" %(i+1)
    for j in B:
        print M[j][3:-3]

Submission status: Accepted

Solution code:
def bomb(x,y):
    s=M[y]
    if s[x]=="0":return
    M[y]=s[:x]+"0"+s[x+1:]
    for e in [-3,-2,-1,1,2,3]:
        bomb(x+e,y)
        bomb(x,y+e)
    return

A=range(14)
B=range(3,11)
M=["00000000000000" for i in A]
z="000"
n=input()+1
for i in range(1,n):
    s=raw_input()
    for j in B:
        M[j]=z+raw_input()+z
    x=input()+2
    y=input()+2
    bomb(x,y)
    print "Data %d:" %(i)
    for j in B:
        print M[j][3:-3]

Submission status: Accepted

Solution code:
def bomb(x,y):
    s=M[y]
    if s[x]=="0":return
    M[y]=s[:x]+"0"+s[x+1:]
    for e in [-3,-2,-1,1,2,3]:
        bomb(x+e,y)
        bomb(x,y+e)
    return

A=range(14)
B=range(3,11)
M=["00000000000000" for i in A]
z="000"
n=input()
for i in range(n):
    s=raw_input()
    for j in B:
        M[j]=z+raw_input()+z
    x=input()+2
    y=input()+2
    bomb(x,y)
    print "Data %d:" %(i+1)
    for j in B:
        print M[j][3:-3]

Submission status: Accepted

Solution code:
def bomb(x,y):
    s=M[y]
    if s[x]=="0":return
    M[y]=s[:x]+"0"+s[x+1:]
    R=[-3,-2,-1,1,2,3]
    for e in R:
        bomb(x+e,y)
        bomb(x,y+e)
    return

A=range(14)
B=range(3,11)
M=["00000000000000" for i in A]
z="000"
n=input()
for i in range(n):
    s=raw_input()
    for j in B:
        M[j]=z+raw_input()+z
    x=input()+2
    y=input()+2
    bomb(x,y)
    print "Data %d:" %(i+1)
    for j in B:
        print M[j][3:-3]

Submission status: Accepted

Solution code:
def bomb(x,y):
    s=M[y]
    if s[x]=="0":return
    M[y]=s[:x]+"0"+s[x+1:]
    R=[-3,-2,-1,1,2,3]
    for e in R:
        bomb(x+e,y)
        bomb(x,y+e)
    return

A=range(3,11)
M=["00000000000000" for i in range(14)]
z="000"
n=input()
for i in range(n):
    s=raw_input()
    for j in A:
        M[j]=z+raw_input()+z
    x=input()+2
    y=input()+2
    bomb(x,y)
    print "Data %d:" %(i+1)
    for j in A:
        print M[j][3:-3]

Submission status: Accepted

Solution code:
def bomb(x,y):
    s=M[y]
    if s[x]=="0":return
    M[y]=s[:x]+"0"+s[x+1:]
    R=[-3,-2,-1,1,2,3]
    for e in R:
        bomb(x+e,y)
        bomb(x,y+e)
    return

A=range(14)
B=range(3,11)
M=["0"*14 for i in A]
z="000"
n=input()
for i in range(n):
    s=raw_input()
    for j in B:
        M[j]=z+raw_input()+z
    x=input()+2
    y=input()+2
    bomb(x,y)
    print "Data %d:" %(i+1)
    for j in B:
        print M[j][3:-3]

Submission status: Accepted

Solution code:
def bomb(x,y):
    s=M[y]
    if s[x]=="0":return
    M[y]=s[:x]+"0"+s[x+1:]
    R=[-3,-2,-1,1,2,3]
    for e in R:
        bomb(x+e,y)
        bomb(x,y+e)
    return

A=range(14)
B=range(3,11)
M=["0"*14 for i in A]
z="000"
N=range(input())
for i in N:
    s=raw_input()
    for j in B:
        M[j]=z+raw_input()+z
    x=input()+2
    y=input()+2
    bomb(x,y)
    print "Data %d:" %(i+1)
    for j in B:
        print M[j][3:-3]

Submission status: Accepted

Solution code:
def bomb(field,x,y):
    rfield = field[:][:]
    rfield[y][x] = 0
    for i in range(-3,4):
        if 0 <= x+i <= 7:
            if rfield[y][x+i] == 1:
                rfield = bomb(rfield,x+i,y)
        if 0 <= y+i <= 7:
            if rfield[y+i][x] == 1:
                rfield = bomb(rfield,x,y+i)
    return rfield


n = int(raw_input())
for data in range(1,n+1):
    field = []
    raw_input()
    for line in range(8):
        field.append(map(int, list(raw_input())))
    x = int(raw_input())-1
    y = int(raw_input())-1

    field = bomb(field,x,y)
    print "Data %d:"%(data)
    for i in range(8):
        print "".join(map(str, field[i]))

Submission status: Accepted

Solution code:
def bomb(f,x,y):
    rf = f[:][:]
    rf[y][x] = 0
    for i in [-3,-2,-1,1,2,3]:
        if 0 <= x+i <= 7:
            if rf[y][x+i] == 1: rf = bomb(rf,x+i,y)
        if 0 <= y+i <= 7:
            if rf[y+i][x] == 1: rf = bomb(rf,x,y+i)
    return rf


n = int(raw_input())
for data in range(1,n+1):
    f = []
    raw_input()
    for line in range(8):
        f.append(map(int, list(raw_input())))
    x = int(raw_input())-1
    y = int(raw_input())-1

    f = bomb(f,x,y)

    print "Data %d:"%(data)
    for i in range(8):
        print "".join(map(str, f[i]))

Submission status: Accepted

Solution code:
dx = [-3, -2, -1, 0, 0, 0, 0, 0, 0, 1, 2, 3]
dy = [0, 0, 0, -1, -2, -3, 1, 2, 3, 0, 0, 0]


def bomb(y, x):
    field[y][x] = 0
    for k in xrange(len(dx)):
        ny = y + dy[k]
        nx = x + dx[k]
        if nx < 0 or ny < 0 or nx >= 8 or ny >= 8:
            continue
        if field[ny][nx] == 1:
            bomb(ny, nx)


T = input()
for t in xrange(1, T + 1):
    field = []
    raw_input()
    for i in xrange(8):
        field.append(map(int, raw_input()))
    x, y = input() - 1, input() - 1
    bomb(y, x)
    print "Data %d:" % t
    for i in xrange(8):
        print ''.join(map(str, field[i]))

Submission status: Accepted

Solution code:
n = int(input())
nums = [-3, -2, -1, 1, 2, 3]

for i in range(n):
    s = input()
    lst =[]
    for j in range(8):
        lst.append(list(input()))

    X = int(input())
    Y = int(input())
    bomblst = [[X-1, Y-1]]

    for j in bomblst:
        lst[j[1]][j[0]] = '0'
                
        for num in nums:
            kx = [j[0], j[1] + num]
            if 0 <= kx[1] <= 7:
                if lst[kx[1]][kx[0]] == '1':
                    bomblst.append(kx)

            ky = [j[0] + num, j[1]]
            if 0 <= ky[0] <= 7:
                if lst[ky[1]][ky[0]] == '1':
                    bomblst.append(ky)

    print('Data ' + str(i+1) + ':')
    for k in lst:
        print(''.join(k))

Submission status: Accepted

Solution code:
def bumb(i, j) :
    global M
    M[j][i] = 0
    for p in range(-3, 4) :
        if 0 <= i+p < 8 :
            if M[j][i+p] == '1' :
                bumb(i+p, j)
        if 0 <= j+p < 8 :
            if M[j+p][i] == '1' :
                bumb(i, j+p)

n = int(input())
for i in range(n) :
    M = []
    none = input()
    for j in range(8) :
        M.append(list(input()))
    X = int(input())
    Y = int(input())
    bumb(X-1, Y-1)
    
    print('Data ', i+1, ':', sep='')
    for j in range(8) :
        print(*M[j], sep='')

Submission status: Accepted

Solution code:
from collections import deque
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
    N = int(readline())
    dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
    L = 8
    for i in range(N):
        readline()
        G = [list(map(int, readline().strip())) for i in range(L)]
        x = int(readline())-1
        y = int(readline())-1
        que = deque([(x, y)])
        G[y][x] = 0
        while que:
            x, y = que.popleft()
            for dx, dy in dd:
                for k in range(1, 4):
                    nx = x + dx*k; ny = y + dy*k
                    if not 0 <= nx < L or not 0 <= ny < L:
                        continue
                    if G[ny][nx]:
                        que.append((nx, ny))
                        G[ny][nx] = 0
        write("Data %d:\n" % (i+1))
        for line in G:
            write("".join(map(str, line)))
            write("\n")
solve()

Submission status: Accepted

Solution code:
num = int(input())
def chain(L, x, y):
    if L[y][x] == 0:
        return
    L[y][x] = 0
    for i in range(-3, 4):
        nx = x + i
        if nx in range(8):
            if L[y][nx] > 0:
                chain(L, nx, y)
        ny = y + i
        if ny in range(8):
            if L[ny][x] > 0:
                chain(L, x, ny)
    return

for k in range(num):
    input()
    L = []
    for i in range(8):
        l = [int(x) for x in input()]
        L.append(l)
    x = int(input())-1
    y = int(input())-1

    chain(L, x, y)

    print("Data {}:".format(k+1))

    for l in L:
        print( "".join(map(str,l)) )

Submission status: Accepted

Solution code:
def bomb(y, x, a):
    a[y][x] = '0'
    b = [[-3, 0], [-2, 0], [-1, 0], [1, 0], [2, 0], [3, 0],
         [0, -3], [0, -2], [0, -1], [0, 1], [0, 2], [0, 3]]
    for dx, dy in b:
        if 0 <= x + dx < 8 and 0 <= y + dy < 8 and a[y + dy][x + dx] == '1':
            bomb(y + dy, x + dx, a)

    return a


for i in range(int(input())):
    input()
    a = [list(input()) for _ in range(8)]
    x = int(input())
    y = int(input())

    bomb(y - 1, x - 1, a)
    print('Data %d:' % (i+1))
    [print(*x, sep='') for x in a]

Submission status: Accepted

Solution code:
def dfs_bomb(A,x,y):

	X = [ 1, 0,-1, 0, 2, 0,-2, 0, 3, 0,-3, 0]
	Y = [ 0, 1, 0,-1, 0, 2, 0,-2, 0, 3, 0,-3]

	A[x][y] = "0"

	for i in range(12):
		xx = x + X[i]
		yy = y + Y[i]

		if 0 <= xx < 8 and 0 <= yy < 8:
			if A[xx][yy] == "1":
				dfs_bomb(A,xx,yy)

if __name__ == '__main__':

	cnt = int(input())

	for i in range(cnt):
		input()
		A = []
		for _ in range(8):
			A.append(list(input()))

		y = int(input()) - 1
		x = int(input()) - 1

		dfs_bomb(A,x,y)

		print("Data "+str(i+1)+":")
		for z in range(8):
			print("".join(A[z]))

Submission status: Accepted

Solution code:
def bomb(x,y):
    for bi in range(-3,4):
        if 0<=x+bi<=7 and field[x+bi][y]=="1":field[x+bi][y]="2"
        if 0<=y+bi<=7 and field[x][y+bi]=="1":field[x][y+bi]="2"
        field[x][y]="0"
n=int(input())
for i in range(n):
    x=input()
    field=[]
    for j in range(8):
        s=input()
        f_s=[]
        for k in s:
            f_s.append(k)
        field.append(f_s)
    x=int(input())
    y=int(input())
    bomb(y-1,x-1)
    while "2" in [flatten for inner in field for flatten in inner]:
        for y in range(8):
            for x in range(8):
                if field[y][x]=="2":bomb(y,x)
    for j in range(8):field[j]=''.join(field[j])
    print(f'Data {i+1}:')
    for j in field:print(j)

Submission status: Accepted

Solution code:
from copy import copy

n = int(input())
bomb = [[0 for i in range(8)] for j in range(8)]
for count in range(n):
    _ = input()
    for i in range(8):
        bomb[i] = [int(i) for i in list(input())]
    y = int(input()) -1
    x = int(input()) -1
    exp = []
    exp.append([x,y])
    while(len(exp) != 0):
        exp_ = copy(exp)
        for x,y in exp_:
            bomb[x][y] = 0
            for i in range(1,4):
                if x+i < 8:
                    if bomb[x+i][y] == 1:
                        exp.append([x+i,y])
                        bomb[x+i][y] = 0
                if y+i < 8:
                    if bomb[x][y+i] == 1:
                        exp.append([x,y+i])
                        bomb[x][y+i] = 0
                if x-i > -1:
                    if bomb[x-i][y] == 1:
                        exp.append([x-i,y])
                        bomb[x-i][y] = 0
                if y-i > -1:
                    if bomb[x][y-i] == 1:
                        exp.append([x,y-i])
                        bomb[x][y-i] = 0
            exp.remove([x,y])
    print("Data {}:".format(count+1))
    for i in range(8):
        print(*bomb[i],sep="")

Submission status: Runtime Error

Solution code:
n = int(input())
input()

for i in range(n):
	board = [list(input()) for i in range(8)]
	sx = int(input()) - 1
	sy = int(input()) - 1
	input()
	
	board[sy][sx] == 2
	bomb = []
	n_bomb = [[sy, sx]]
	points = []
	for j in range(len(board)):
		while '1' in board[j]:
			px = board[j].index('1')
			py = j
			points.append([py, px])
			board[j][px] = '2'
	
	while len(n_bomb) != 0:
		bomb += n_bomb
		n_bomb = []
		for b in bomb:
			for p in points:
				if (p[0] == b[0] and b[1]-3 <= p[1] <= b[1]+3) or (p[1] == b[1] and b[0]-3 <= p[0] <= b[0]+3):
					points.remove(p)
					n_bomb.append(p)
	
	ans = [['0'] * 8 for j in range(8)]
	for p in points:
		ans[p[0]][p[1]] = '1'

	print("Data %s:" % str(i+1))
	for p in ans:
		print("".join(p))

Submission status: Runtime Error

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

MAX_Y = 8
MAX_X = 8

def main():
    # 入力
    N = int(raw_input())
    raw_input()

    for i , j in enumerate(range(N)):
        data = []
        for j in range(MAX_Y):
            input_list = map(int, raw_input())
            data.append(input_list)
        bomb_X = int(raw_input())
        bomb_Y = int(raw_input())
        raw_input()

        bomb_town = explosion(data, ((bomb_X-1, bomb_Y-1),))
        print("Data " + str(i+1) + ":")
        printBomb(bomb_town)

def printBomb(bomb):
    for x in bomb:
        print(reduce(lambda x, y: x+y, (map(str, x))))

# 盤面内か判定
def isIn(x, y):
    return 0<=x<MAX_X and 0<=y<MAX_Y

# 爆弾を爆破
def explosion(data, bomb):
    for b in bomb:
        if data[b[1]][b[0]] == 1:
            new_bomb = bom(data, b)
            burned_data = burned(data, b)
            data = explosion(burned_data, new_bomb)

    return data

# 燃やす
def burned(data, b):
    data[b[1]][b[0]] = 0
    return data

# 燃焼範囲をチェック
def bom(data, b):
    bom_area =  [(-3, 0), (-2, 0), (-1, 0), (1, 0), (2, 0), (3, 0),
                (0, -3), (0, -2), (0, -1), (0, 1), (0, 2), (0, 3)]
    new_bomb = []
    for x in bom_area:
        bomb = tuple([sum(x) for x in zip(x,b)])
        if isIn(*bomb):
            new_bomb.append(bomb)

    return new_bomb

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
def solve(data,p):
    L=[]
    if data[p[1]-1][p[0]-1]=="1":
        data[p[1]-1]=data[p[1]-1][:p[0]-1]+"0"+data[p[1]-1][p[0]:]
        L+=p
    else:
        return []
    bakuhu=[(p[0]-3,p[1]),(p[0]-2,p[1]),(p[0]-1,p[1]),(p[0]+1,p[1]),(p[0]+2,p[1]),(p[0]+3,p[1]),(p[0],p[1]-3),(p[0],p[1]-2),(p[0],p[1]-1),(p[0],p[1]+1),(p[0],p[1]+2),(p[0],p[1]+3)]
    for i in range(len(bakuhu)):
        if bakuhu[i][0]>0 and bakuhu[i][1] >0 and bakuhu[i][0] <9 and bakuhu[i][1] <9:
            L.extend(solve(data,bakuhu[i]))
    return L
    
while True:
    ndata=input()
    for i in range(ndata):
        raw_input()
        data=[]
        for j in range(8):
            data.append(raw_input())
        x=input()
        y=input()
        p=(x,y)
        L=solve(data,p)
        while len(L)!=0:
            x=L.pop(0)
            y=L.pop(0)
            data[y-1]=data[y-1][:x-1]+'0'+data[y-1][x:]
        print "Data"+str(i+1)+':'
        for i in range(len(data)):
            print data[i]

Submission status: Runtime Error

Solution code:
def bomb(f,x,y):
    rf=f[:][:]
    rf[y][x]=0
    for i in [-3,-2,-1,1,2,3]:
        if 0<=x+i<=7:
            if rf[y][x+i]==1:
                rf=bomb(rf,x+i,y)
        if 0<=y+i<=7:
            if rf[y+i][x]==1:
                rf=bomb(rf,x,y+i)
    return rf

n=int(raw_input())
for data in range(1,n+1):
    raw_input()
    f=[]
    for i in range(8):
        l.append([int(x) for x in raw_input()])
    x=int(raw_input())-1
    y=int(raw_input())-1
       # map(int,raw_input().split())
    f=bomb(f,x,y)
    print "Data %d:"%(data)
    for i in range(8):
        print ''.join(map(str, f[i]))