#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
int W,H,D,a[625][625],b[625];
int id(int i,int j){
	return i*W+j;
}
int main(){
	while(true){
		cin>>W>>H>>D;
		if(W==0) break;
		rep(i,H) rep(j,W) cin>>b[id(i,j)];
		rep(i,H) rep(j,W) rep(k,H) rep(h,W){
			a[id(k,h)][id(i,j)]=0;
			int d=abs(i-k)+abs(j-h);
			if(d==D||d==0) a[id(k,h)][id(i,j)]=1;
		}
		bool used[625]={};
		int eq=H*W;
		rep(j,eq){
			int i=0;
			while(i<eq && (used[i]||a[i][j]==0)) i++;
			if(i==eq) continue;
			used[i]=true;
			rep(k,eq){
				if(i==k) continue;
				if(a[k][j]==0) continue;
				rep(l,eq) a[k][l]^=a[i][l];
				b[k]^=b[i];
			}
		}
		bool can=true;
		rep(i,eq) if(!used[i]&&b[i]!=0) can=false;
		cout<<can<<endl;
	}
}