

def factorize(q_):
    res = []
    i_ = 2
    tmp_ = q_
    while i_ * i_ <= q_:
        count = 0
        while tmp_ % i_ == 0:
            tmp_ //= i_
            count += 1
        if count != 0:
            res.append((i_, count))
        i_ += 1
    if tmp_ != 1:
        res.append((tmp_, 1))
    return res


class Result:
    def __init__(self, res, path, is_exist):
        self.res = res
        self.path = path
        self.is_exist = is_exist


def get_del_count(val, p):
    tmp = val
    res = 0
    while tmp % p == 0:
        tmp //= p
        res += 1
    return res


def solve(n, m, a, p):
    dp = []
    prev = []
    for i_ in range(n):
        dp.append([])
        prev.append([])
        for j in range(m):
            if i_ == 0 and j == 0:
                dp[0].append(get_del_count(a[0][0], p))
                prev[0].append(-1)
                continue
            opt = -1
            opt_prev = -1
            if i_ > 0 and dp[i_ - 1][j] != -1:
                opt = dp[i_ - 1][j]
                opt_prev = 1
            if j > 0 and dp[i_][j - 1] != -1:
                if opt == -1:
                    opt = dp[i_][j - 1]
                    opt_prev = 2
                else:
                    if opt > dp[i_][j - 1]:
                        opt_prev = 2
                        opt = dp[i_][j - 1]
            if a[i_][j] == 0:
                opt = -1
                opt_prev = -1
            elif opt != -1:
                opt += get_del_count(a[i_][j], p)
            dp[i_].append(opt)
            prev[i_].append(opt_prev)
    path = []
    if dp[n - 1][m - 1] == -1:
        return Result(-1, path, False)
    x = n - 1
    y = m - 1

    while x != 0 or y != 0:
        path.append((x, y))
        if prev[x][y] == 2:
            y -= 1
        else:
            x -= 1
    path.append((0, 0))
    path = reversed(path)
    return Result(dp[n - 1][m - 1], path, True)


if __name__ == '__main__':
    n, m, q = map(int, input().split())
    factors = factorize(q)
    a = []
    ans = Result(-1, [], False)
    for i in range(n):
        tmp = []
        for val in input().split():
            tmp.append(int(val))
        a.append(tmp)
    for i in range(len(factors)):
        p = factors[i][0]
        cnt = factors[i][1]
        result = solve(n, m, a, p)
        result.res //= cnt
        if not ans.is_exist:
            ans = result
        else:
            if result.is_exist:
                if result.res < ans.res:
                    ans = result
    if not ans.is_exist:
        print(-1)
    else:
        print(ans.res)
        for p in ans.path:
            print(p[0] + 1, p[1] + 1)



