Hide

Problem B
Orienteering

Languages en sv

Hanna is organizing an orienteering competition. She has already set up the area where the competition will be held, a grid with $N \times M$ squares where each square contains a control point which has a letter. All contestants starts in the upper left corner and finishes in the bottom right corner. They can only run to the right or down when they reach a control. They can choose their own path, and will visit $N + M$ control points in total. The contestants will write down all the letters they come across on the way and hand in the letter sequence at the end to ensure they have passed the right amount of control points. To make sure that no contestant is cheating, Hanna has made a rule that nobody is allowed to hand in the same sequence as someone else. You think this rule is dumb – if two different paths result in the same letter sequence an innocent contestant might be disqualified. Can you help Hanna check if two different paths result in the same sequence?

Input

The first row contains two numbers, $N$ and $M$ ($1 \leq N,M \leq 2\ 000$), where $N$ is the height of the grid, and $M$ is the width of the grid. The following $N$ rows contain a string of length $M$, the control points of a row in the grid. All occuring letters in the grid are non-capital letters from ’a’ to ’z’.

Output

Write "Ja" if there are two paths with the same letter sequence, and "Nej" otherwise.

Points

Your solution will be tested on different test groups. To score points for a group, you must pass all the test cases in that group.

Group

Point value

Constraints

$1$

$20$

$N = 2, M = 2$

$2$

$20$

$N = 3, M = 3$

$3$

$20$

$N = 10, M = 10$

$4$

$40$

No additional constraints.

Explanation of sample 1:

In the first sample case all paths will generate the sequence "abcab". Therefore, there is more than one path that generates the same sequence and the answer is "Ja".

Explanation of sample 2:

All paths generate a different sequence, therefore the answer is "Nej".

Sample Input 1 Sample Output 1
3 3
abc
bca
cab
Ja
Sample Input 2 Sample Output 2
4 3
bfd
ash
cbn
abs
Nej
Sample Input 3 Sample Output 3
5 5
bvjad
vsahi
jncbu
ausug
diugy
Ja