Harmonious Matrices

Time limit1sMemory limit128 MB

Summary
Given m and n (up to 40), output the lexicographically smallest nonzero matrix of bits where every cell sees an even number of 1s among itself and its orthogonal neighbors.
Level

Hard8 of 10

Topics
Bit manipulation, Greedy, Brute force, Math
Solved
No attempts yet

Problem

Call an m×nm \times n matrix of bits harmonious if every cell has an even number of 11 bits among its neighbors. A cell counts as a neighbor of itself, and its neighbors also include the cells directly above, below, to the left, and to the right of it (whenever those cells exist). So a cell has at most five neighbors, and fewer near an edge or corner.

For example, the following 4×44 \times 4 matrix is harmonious:

0 1 0 0
1 1 1 0
0 0 0 1
1 1 0 1

Given mm and nn, produce an m×nm \times n harmonious matrix of bits.

Input

The first line contains an integer ZZ (Z≤40Z \le 40). Each of the next ZZ lines contains two space-separated positive integers mm and nn, each at most 4040, describing one instance.

Output

For each instance, output its m×nm \times n matrix, one row per line, with the entries of a row separated by single spaces. Print the instances in order, with no blank line between them.

The all-zero matrix is always harmonious, so an instance can have many harmonious matrices. To make the answer unique, output the lexicographically smallest non-zero harmonious matrix; if the all-zero matrix is the only harmonious matrix, output the all-zero matrix. Compare two matrices by reading their entries in row-major order (top to bottom, and left to right within each row), treating 00 as smaller than 11.

Examples1

  1. Example 1

    Input
    2
    4 4
    1 6
    
    Expected output
    0 0 0 1
    0 0 1 1
    0 1 0 1
    1 1 1 0
    0 0 0 0 0 0