#K53592. Valid Checkerboard Grid

    ID: 29565 Type: Default 1000ms 256MiB

Valid Checkerboard Grid

Valid Checkerboard Grid

You are given a grid of size \( n \times m \) containing characters '0' and '1'. Your task is to determine whether the grid is arranged in a valid checkerboard pattern. A valid checkerboard pattern is defined such that no two adjacent cells (sharing an edge) have the same value.

Formally, if we define two potential valid patterns:

  • Pattern 1: The top-left cell is \(0\) and every cell \((i,j)\) should have value \((i+j) \mod 2\).
  • Pattern 2: The top-left cell is \(1\) and every cell \((i,j)\) should have value \((i+j+1) \mod 2\).

If the grid matches either pattern, then it is a valid checkerboard grid. Otherwise, it is not.

Note that the grid is provided as multiple test cases in a single input.

inputFormat

The first line of input contains an integer \(T\) (\(T \ge 1\)) denoting the number of test cases. For each test case, the first line contains two space-separated integers \(n\) and \(m\), representing the number of rows and columns in the grid. This is followed by \(n\) lines, each containing a string of length \(m\) consisting of characters '0' and '1'.

Example:

2
3 3
010
101
010
3 3
000
111
000

outputFormat

For each test case, output a single line containing YES if the grid is a valid checkerboard pattern, or NO otherwise.

Example:

YES
NO
## sample
2
3 3
010
101
010
3 3
000
111
000
YES

NO

</p>