Find the Element

2

2 votes
Medium
Problem

Ratnesh is having a 2D matrix of N*M dimension and a target element of that matrix. He wants to print the matrix in such a way that the path to the target element is in spiral manner.

For example if he has matrix
A B C D U
E F G H V
I J K L W
M N O P X
and target is H, then he will print :
ABCDUVWXPONMIEFGH

and if the element is not found then he will print NO.

INPUT

- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains Two Integers N and M Specifying the Dimension of Array.
- Next N line will contain M space separated Elements.
- Next line will contain The element to be searched.

OUTPUT

- For each test case, output the string of words.

CONSTRAINTS

1 ≤ T ≤ 1000
1 ≤ N ≤ 500
1 ≤ M ≤ 500
1 ≤ Elements ≤ 3

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?