#include <iostream>
#include <algorithm>
#include <vector>
#include <bitset>
#include <cassert>
using namespace std;

// mod2 行列ライブラリ (bitset を使って高速化、横は SIZE 固定とする)
// TODO: 乗算の verify
struct BinaryMatrix {
    int H, W;
    static const int SIZE = 2010;
    vector< bitset<SIZE> > mat;
    BinaryMatrix(int H_, int W_) : H(H_), W(W_), mat(H_) {}

    // 乗算に使用 (これ微妙に転置じゃないけどなんていうんだ)
    BinaryMatrix T(const BinaryMatrix& A) {
        int h = A.H, w = A.W;
        BinaryMatrix res(w, h);
        for(int i=0; i<h; i++) {
            for(int j=0; j<w; j++) {
                res[j][i] = A[i][j];
            }
        }
        return res;
    }

    BinaryMatrix& operator*=(const BinaryMatrix& rhs) {
        assert(W == rhs.H);
        BinaryMatrix res(H, rhs.W), trhs = T(rhs);
        for(int i=0; i<H; i++) {
            for(int j=0; j<rhs.W; j++) {
                res[i][j] = (mat[i] & trhs[j]).count() % 2;
            }
        }
        return (*this = res);
    }
    BinaryMatrix& operator+=(const BinaryMatrix &rhs) {
        assert(H == rhs.H and W == rhs.W);
        for(int i=0; i<H; i++) mat[i] ^= rhs[i];
        return *this;
    }
    BinaryMatrix& operator-=(const BinaryMatrix &rhs) {
        return (*this += rhs);
    }

    BinaryMatrix operator*(const BinaryMatrix &rhs) {
        return (BinaryMatrix(*this) *= rhs);
    }
    BinaryMatrix operator+(const BinaryMatrix &rhs) {
        return (BinaryMatrix(*this) += rhs);
    }
    BinaryMatrix operator-(const BinaryMatrix &rhs) {
        return (BinaryMatrix(*this) -= rhs);
    }

    bool operator==(const BinaryMatrix &rhs) const {
        if(H != rhs.H or W != rhs.W) return false;
        for(int i=0; i<H; i++) if(mat[i] != rhs[i]) return false;
        return true;
    }
    bool operator!=(const BinaryMatrix &rhs) const {
        return !(*this == rhs);
    }

    const bitset<SIZE>& operator[](int k) const { return mat[k]; }
    bitset<SIZE>& operator[](int k) { return mat[k]; }
};

BinaryMatrix eigen(size_t N) {
    BinaryMatrix res(N, N);
    for(size_t i=0; i<N; i++) res[i][i] = 1;
    return res;
}

BinaryMatrix pow(BinaryMatrix mat, long long int k) {
    BinaryMatrix res = eigen(mat.H);
    for(; k>0; k>>=1) {
        if(k & 1) res *= mat;
        mat *= mat;
    }
    return res;
}

int gaussianEliminationBinary(BinaryMatrix &mat, bool ext=false) {
    int N = mat.H, M = mat.W, rank = 0;
    for(int j=0; j+ext<M; j++) {
        int piv = -1;
        for(int i=rank; i<N; i++) {
            if(mat[i][j] != 0) piv = i, i = N;
        }
        if(piv < 0) continue;
        swap(mat[rank], mat[piv]);
        for(int i=0; i<N; i++) {
            if(i == rank or mat[i][j] == 0) continue;
            mat[i] ^= mat[rank];
        }
        rank++;
    }
    return rank;
}

vector<int> linearEquationBinary(BinaryMatrix A, vector<int> b) {
    int N = A.H, M = A.W;
    BinaryMatrix mat(N, M+1);
    for(int i=0; i<N; i++) {
        for(int j=0; j<=M; j++) {
            mat[i][j] = (j < M ? A[i][j] : b[i]);
        }
    }

    int rank = gaussianEliminationBinary(mat, true);
    vector<int> res(N);
    for(int i=0; i<N; i++) {
        res[i] = mat[i][M];
        if(i >= rank and mat[i][M] != 0) return {};
    }
    return res;
}

int detBinary(BinaryMatrix A) {
    int N = A.H;
    for(int j=0; j<N; j++) {
        int piv = -1;
        for(int i=j; i<N; i++) {
            if(A[i][j] != 0) piv = i, i = N;
        }
        if(piv < 0) return 0;
        swap(A[piv], A[j]);
        for(int i=j+1; i<N; i++) {
            if(A[i][j]) A[i] ^= A[j];
        }
    }
    int res = 1;
    for(int i=0; i<N; i++) res *= A[i][i];
    return res;
}

int solve_testcase() {
    int W, H, D; cin >> W >> H >> D;
    if(W == 0 and H == 0 and D == 0) return 1;

    vector< vector<int> > A(H, vector<int>(W));
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            cin >> A[i][j];
        }
    }

    auto get_idx = [&](int r, int c) {
        return r * W + c;
    };

    int N = H * W;
    BinaryMatrix mat(N, N);
    vector<int> vec(N);
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            int x = get_idx(i, j);
            mat[x][x] = 1;
            vec[x] = A[i][j];
            
            for(int dx=0; dx<=D; dx++) {
                int dy = D - dx;
                for(int f=-1; f<=1; f++) {
                    for(int g=-1; g<=1; g++) {
                        if(f == 0 or g == 0) continue;
                        int k = i + f * dx;
                        int l = j + g * dy;
                        if(k < 0 or k >= H or l < 0 or l >= W) continue;
                        // fprintf(stderr, "i = %d, j = %d, k = %d, l = %d\n", i, j, k, l);

                        int y = get_idx(k, l);
                        mat[x][y] = 1;
                    }
                }
            }
        }
    }

    auto res = linearEquationBinary(mat, vec);
    cout << (res.size() > 0) << endl;
    // for(auto e : res) cerr << e << " ";
    // cerr << endl;
    return 0;
}

int main() {
    while(!solve_testcase());
}



Measured memory_bytes: 3460