from fractions import Fraction

def width(A):
    w = len(A[0])
    assert all(len(A[i]) == w for i in range(height(A)))
    return w

# height of matrix A
def height(A):  return len(A)

# swaps two rows in a matrix A.
def swap(rowj, rowk, A):
    assert(0 <= rowj < height(A))
    assert(0 <= rowk < height(A))
    A[rowj], A[rowk] = A[rowk], A[rowj]


# Adds row*factor to row 'to' in A
def addTo(row, factor, to, A):
    assert(0 <= row < height(A))
    assert(0 <= to < height(A))
    for i in range(width(A)):
        A[to][i] += A[row][i]*factor

# multiplies row by nonzero factor
def times(row, factor, A):
    assert(factor != 0)
    assert(0 <= row < height(A))
    for i in range(width(A)):
        A[row][i] *= factor

# prints the matrix nicely:
def print_mtx(A):
    print("\n")
    for row in A:
        print(" ".join(str(x) for x in row))
    print("\n")
    

# converts matrix to rational numbers:
def to_rational(A):
    for row_idx in range(height(A)):
        for col_idx in range(width(A)):
            A[row_idx][col_idx] = Fraction(A[row_idx][col_idx])