#include <vector>
#include <iostream>
#include <valarray>
#include <string>
#include <algorithm>
#include <set>
typedef unsigned long long ull;
typedef long long ll;

#define rep(i,n) for(ull i = 0;i<(n);i++)
#define rep1(i,n) for(ull i = 1;i<=(n);i++)
#define revrep(i,n) for(ull i = (n-1);i>=0;i--)
#define all(n) (n).begin(),(n).end()
#define show(x) cout << "debug " << #x << ":" << x << endl;
#define showmat(x) cout << endl << "debug " << #x << ";" << endl; \
	for(ull __i = 0;__i < x.size();__i++){ \
		for(ull __j = 0;__j<x[0].size();__j++){ \
			cout << x[__i][__j] << " "; \
		} \
		cout << endl; \
	} \
	cout << endl;

using namespace std;

ll M,N,D;
typedef valarray<ll> array_t;
int main(){
	while(cin >> M >> N >> D && M){
		valarray<array_t > mat(array_t(0LL,M*N+1), M*N);
		rep(i,M*N){
			cin >> mat[i][M*N];
			rep(j,M*N){
				int curx=i%M, cury=i/M, trgx=j%M, trgy=j/M;
				ll d = abs(curx-trgx)+abs(cury-trgy);
				if(d == 0 || d == D){
					mat[i][j] = 1;
				}
			}
		}


		//hakidashi
		rep(i,M*N){
			int nonzeroR = -1;
			for(ll p = i;p<M*N;p++){
				if(mat[p][i] != 0){
					nonzeroR = p;
					break;
				}
			}
			if(nonzeroR == -1){
				continue;
			}
			swap(mat[i],mat[nonzeroR]);
			for(ll j = 0;j<M*N;j++){
				if(j == i){
					continue;
				}
				if(mat[j][i] == 1){
					mat[j] += mat[i];
				}
				mat[j] %= 2;
			}
		}

		bool ans = true;
		rep(i,M*N){
			bool rowzero = true;
			rep(j,M*N){
				if(mat[i][j] != 0){
					rowzero = false;
					break;
				}
			}
			if(rowzero && mat[i][M*N] == 1){
				ans = false;
				break;
			}
		}
		if(ans){
			cout << 1 << endl;
		}else{
			cout << 0 << endl;
		}
	}
}