This page is still under construction.

Parts of this page are still being built. What you see may change.

Tensor Product of Matrices

Time limit3sMemory limit128 MB

Summary
Count how many ways a given positive-integer matrix can be written as a tensor product A ⊗ B where neither factor is 1×1.
Level

Medium7 of 10

Topics
Math, Number theory, Matrix, Brute force
Solved
No attempts yet

Problem

There is another way to multiply two matrices, called the tensor product (Kronecker product).

Let AA be a p×qp \times q matrix and BB an n×mn \times m matrix, where neither AA nor BB is a 1×11 \times 1 matrix.

The tensor product A⊗BA \otimes B is a pn×qmpn \times qm matrix obtained by replacing every entry aija_{ij} of AA with the block aij⋅Ba_{ij} \cdot B.

For example:

A=[1234],B=[0567]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \qquad B = \begin{bmatrix} 0 & 5 \\ 6 & 7 \end{bmatrix}

A⊗B=[0501067121401502018212428]A \otimes B = \begin{bmatrix} 0 & 5 & 0 & 10 \\ 6 & 7 & 12 & 14 \\ 0 & 15 & 0 & 20 \\ 18 & 21 & 24 & 28 \end{bmatrix}

Unlike ordinary matrix multiplication, there is no requirement that qq equal nn.

Given a matrix, write a program that counts the number of different ways it can be written as a tensor product A⊗BA \otimes B, where AA and BB are matrices whose entries are positive integers and neither is a 1×11 \times 1 matrix. Two ways are considered different when the matrix AA or the matrix BB differs, either in size or in any entry.

Input

The input consists of several test cases.

The first line of each test case contains the matrix dimensions rr and cc. Each of the next rr lines contains the cc integers of one row of the matrix.

rr and cc are at most 500500, and every entry of the matrix is an integer between 11 and 6553665536 inclusive.

The last line of the input contains two zeros, marking the end of the input.

Output

For each test case, print on its own line the number of different ways the given matrix can be expressed as a tensor product A⊗BA \otimes B.

Examples4

  1. Example 1

    Input
    6 6
    1 1 1 2 2 2
    1 1 1 2 2 2
    1 1 2 2 2 4
    3 3 3 4 4 4
    3 3 3 4 4 4
    3 3 6 4 4 8
    2 2
    3 6
    4 9
    2 4
    15 18 30 36
    20 24 40 48
    0 0
    
    Expected output
    1
    0
    4
    
  2. Example 2

    Input
    2 2
    2 2
    2 2
    0 0
    
    Expected output
    4
    
  3. Example 3

    Input
    2 6
    1 1 2 2 3 3
    1 1 2 2 3 3
    0 0
    
    Expected output
    4
    
  4. Example 4

    Input
    4 4
    1 1 2 2
    1 1 2 2
    3 3 4 4
    3 3 4 4
    0 0
    
    Expected output
    3