#include <bits/stdc++.h>
using namespace std;
 
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for(auto i = 0 * (n); i < (n); ++i)
#define FOR(i, s, n) for(size_t i = (s); i < (n); ++i)

typedef bitset<26*25> Vector;
typedef vector<Vector> Matrix;
struct GaussJordanBit {
	typedef size_t T;
	GaussJordanBit(Matrix mat, T col) : x(move(mat)) {
		T row = x.size(), r = 0;
		REP(c, col) {
			T p = r;
			FOR(i, r, row) if (x[i][c]) p = i;
			swap(x[r], x[p]);
			if (x[r][c]) {
				if (c == col - 1) possible = 0; // ??????????????????????????????????????????
				REP(i, row) if (r != i && x[i][c]) x[i] ^= x[r];
				if (++r == row) break;
			}
		}
		rank = r;
	}
	Matrix x;
	T rank;
	bool possible = 1; // ?????¢????¬?????¨????????§£??????????????´???????????????????§£????????? <-> true
};

bool ba[30][30];

int main() {
#ifdef INPUT_FROM_FILE
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	cin.tie(0);
	ios::sync_with_stdio(0);
	int m, n, d;
	while (cin >> m >> n >> d, m || n || d) {
		Matrix A(n * m);
		REP(i, n) {
			REP(j, m) {
				cin >> ba[i][j];
				A[i * m + j][i * m + j] = 1;
				REP(k, n) {
					REP(l, m) {
						int dis = abs(i - k) + abs(j - l);
						if (dis == d) {
							A[k * m + l][i * m + j] = 1;
						}
					}
				}
				A[i * m + j][n * m] = ba[i][j];
			}
		}
		cout << (GaussJordanBit(move(A), n * m + 1).possible ? 1 : 0) << endl;
	}
}