#K40877. Palindrome Path in Grid

    ID: 26740 Type: Default 1000ms 256MiB

Palindrome Path in Grid

Palindrome Path in Grid

You are given a grid with n rows and m columns where each cell contains a lowercase letter. Your task is to determine whether there exists a path from the top-left cell to the bottom-right cell such that the sequence of characters along the path forms a palindrome. You can only move either right or down at each step.

A palindrome is a string that reads the same backward as forward, i.e. \( s = s^R \).

inputFormat

The input starts with an integer T, denoting the number of test cases. For each test case, the first line contains two space-separated integers n and m. Each of the next n lines contains a string of length m representing a row of the grid.

outputFormat

For each test case, output a single line: "YES" if there exists a valid path from the top-left to the bottom-right cell that forms a palindrome, otherwise output "NO".## sample

1
1 1
a
YES