#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>

#define int long long

using namespace std;

vector<pair<int, int>> factorize(int q) {
    vector<pair<int, int>> res;

    int tmp = q;
    for (int i = 2; i * i <= tmp; ++i) {
        int count = 0;
        while (q % i == 0) {
            ++count;
            q /= i;
        }
        if (count)
            res.emplace_back(i, count);
    }
    if (q != 1) {
        res.emplace_back(q, 1);
    }

    return res;
}


struct Result {
    bool isExist = false;
    int count = 0;
    vector<pair<int, int>> path;
};

int inf = 1e12 + 7;
Result solve(int n, int m, vector<vector<int>> a, int p) {
    vector<vector<int>> dp(n, vector<int>(m, inf)), prev(n, vector<int>(m, inf));

    assert(a[0][0] != 0);
    assert(a[n - 1][m - 1] != 0);

    dp[0][0] = 0;
    while(a[0][0] % p == 0) {
        a[0][0] /= p;
        dp[0][0]++;
    }


    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (i == 0 && j == 0) continue;
            int prev_ = inf;
            int from = 0;
            if (i > 0) {
                prev_ = min(prev_, dp[i - 1][j]);
                from = 1;

            }
            if (j > 0) {
                if (prev_ > dp[i][j - 1]) {
                    from = 2;
                    prev_ = dp[i][j - 1];
                }

            }
            if (prev_ == inf || a[i][j] == 0) {
                dp[i][j] = inf;
                continue;
            }
            dp[i][j] = prev_;
            prev[i][j] = from; // 2 - (i, j - 1); 1 - (i - 1, j);
            while(a[i][j] % p == 0) {
                dp[i][j]++;
                a[i][j] /= p;
            }
        }
    }

    Result res;
    if (dp[n - 1][m - 1] == inf) {
        res.isExist = false;
        return res;
    }
    res.isExist = true;
    res.count = dp[n - 1][m - 1];

    int x = n - 1, y = m - 1;

    while(x != 0 || y != 0) {
        res.path.emplace_back(x, y);
        if (prev[x][y] == 1) {
            --x;
        } else {
            --y;
        }
    }
    res.path.emplace_back(0, 0);

    return res;
}

signed main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> a(n, vector<int>(m, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> a[i][j];
        }
    }

    auto factorization = factorize(q);

    Result bestRes;
    for (auto [p, count] : factorization) {
        Result tmp = solve(n, m, a, p);
        tmp.count /= count;
        if (!bestRes.isExist) {
            bestRes = tmp;
            continue;
        }
        if (tmp.isExist && bestRes.count > tmp.count) {
            bestRes = tmp;
        }
    }
    if (!bestRes.isExist) {
        cout << "-1";
        return 0;
    }
    cout << bestRes.count << '\n';
    std::reverse(bestRes.path.begin(), bestRes.path.end());
    for (auto [x, y]: bestRes.path) {
        cout << x + 1 << " " << y + 1 << std::endl;
    }
    return 0;
}
