#!/usr/bin/python

def checkio(pattern,data1):
			matrix = []
			for i in data1:
						matrix.append(i)
									
			pattern_coord = []
			for i in range(len(pattern)):
						for j in range(len(pattern[i])):
									coord = [i,j,pattern[i][j]]
									pattern_coord.append(coord)
			
#			for i in pattern_coord:
#						print (i)
			
			x = 0
			y = 0
			flag = False
			while x < len(matrix)-len(pattern):
							flag = False
							while y <= len(matrix[x])-len(pattern[0]):
#											print (len(matrix[x])-len(pattern[0]))
#											print (x,y)
											if matrix[x][y] == pattern[0][0]:
#														print (x,y)
														for i in pattern_coord:
																	if matrix[x+i[0]][y+i[1]] == i[2]:
#																			print (matrix[x+i[0]][y+i[1]])
																			flag = True
																	else:
																			flag = False
#																			print (flag)
																			break
											if flag:
														for i in pattern_coord:
																	if matrix[x+i[0]][y+i[1]] == 1:
																			matrix[x+i[0]][y+i[1]] = 3
																	else:
																			matrix[x+i[0]][y+i[1]] = 2													
														y = 0
														flag = False
														
											elif flag == False:
													y += 1
#											print (x,y)
							x +=1
							y = 0 											
				
			return matrix											
							
print (checkio([[1, 0], [1, 1]],[[0, 1, 0, 1, 0],[0, 1, 1, 0, 0],[1, 0, 1, 1, 0],[1, 1, 0, 1, 1],[0, 1, 1, 0, 0]]))
print (checkio([[1, 1], [1, 1]],
        [[1, 1, 1],
         [1, 1, 1],
         [1, 1, 1]]))

print (checkio([[1, 1, 0], [0, 1, 1], [1,0,1]],
        [[0, 1, 0, 1, 0],
         [0, 1, 1, 0, 0],
         [1, 0, 1, 1, 0],
         [1, 1, 0, 1, 1],
         [0, 1, 1, 0, 0]]))