#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <valarray>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int M, N, D;

int solve(valarray<valarray<int> >& A) {
    int p = 0;
    for (int i = 0; i < M*N; i++) {
        int r;
        for (r = p; r < M*N; r++) {
            if (A[r][i] != 0) break;
        }
        if (r == M*N) continue;
        swap(A[p], A[r]);
        for (int j = p+1; j < M*N; j++) {
            if (A[j][i]) {
                A[j] += A[p];
                A[j] %= 2;
            }
        }
        p++;
    }
    for (int r = 0; r < M*N; r++) {
        if (A[r][M*N] != 0) {
            int i;
            for (i = 0; i < M*N; i++) {
                if (A[r][i] != 0) break;
            }
            if (i == M*N) return 0;
        }
    }
    return 1;
}

int main(void) {
    while (cin >> M >> N >> D && M) {
        valarray<valarray<int> > A(valarray<int>(0, M*N+1), M*N);
        for (int i = 0; i < M*N; i++) {
            cin >> A[i][M*N];
            int x0 = i % M, y0 = i / M;
            for (int j = 0; j < M*N; j++) {
                int x1 = j%M, y1 = j/M;
                int d = abs(x0 - x1) + abs(y0 - y1);
                if (d == 0 || d == D) A[i][j] = 1;
            }
        }
        cout << solve(A) << endl;
    }
    return 0;
}