#include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

const int dy[]={-1,0,1,0},dx[]={0,-1,0,1},dy2[]={1,-1,-1,1},dx2[]={1,1,-1,-1};
int m,n,d,s[25][25];
int main()
{
	while(scanf("%d%d%d",&n,&m,&d),m){
		int N=m*n;
		vector<vi> a(N,vi(N+1,0));
		rep(i,m)rep(j,n)scanf("%d",s[i]+j), a[i*n+j][N]=s[i][j];
		rep(i,N)(a[i][i]+=1)%=2;
		rep(i,m)rep(j,n)rep(dir,4)rep(k,d){
			int y=i+dy[dir]*d+dy2[dir]*k, x=j+dx[dir]*d+dx2[dir]*k;
			if(0<=y&&y<m&&0<=x&&x<n){
				(a[y*n+x][i*n+j]+=1)%=2;
			}
		}
		int j=0;
		rep(i,N){
			int mi=i;
			if(!a[mi][j])for(int k=i+1;k<N;k++)if(a[mi]<a[k]){
				mi=k;
				if(a[mi][j])break;
			}
			swap(a[i],a[mi]);
			for(;j<N&&a[i][j]==0;j++);
			if(j>=N)break;
			assert(a[i][j]);
			rep(k,N)if(i<k&&a[k][j])for(int l=j;l<=N;l++)(a[k][l]+=a[i][l])%=2;
			j++;
		}
		bool ok=1; j=0;
		rep(i,N)if(ok&&a[i][N]){
			for(;j<N&&a[i][j]==0;j++);
			if(j>=N)ok=0;
		}
		printf("%d\n",(int)ok);
	}
	return 0;
}