#include<bits/stdc++.h>
#define rep(i,a) for(int i=0;i<(int)(a);i++)
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;

inline int gauss_jordan(const mat& A, const vec& b){
  int n = A.size();
  mat B(n,vec(n+1));
  rep(i,n)rep(j,n)B[i][j] = A[i][j];
  rep(i,n)B[i][n] = b[i];

  rep(i,n){
    int p = i;
    for(int j=i;j<n;j++){
      if(B[j][i]==1){p = j; break;}
    }
    swap(B[i],B[p]);
    if(B[i][i] == 0)continue;;
    
    rep(j,n){
      if(i!=j && B[j][i]){
	for(int k=i;k<=n;k++)(B[j][k] += B[i][k]) &= 1;
      }
    }
  }
  rep(i,n){
    bool f = false;
    rep(j,n)f |= (B[i][j]==1);
    if(!f && B[i][n])return 0;
  }
  return 1;
}

const int dy[] = {-1,-1,1,1}, dx[] = {-1,1,-1,1};
inline vec manhattan(int y, int x, int d, int h, int w){
  vec res(h*w,0);
  res[y*w+x] = 1;
  rep(dir,4){
    rep(k,d+1){
      int ny = y + k*dy[dir], nx = x + (d-k)*dx[dir];
      if(ny<0 || nx<0 || ny>=h || nx>=w)continue;
      res[ny*w+nx] = 1;
    }
  }
  return res;
}

int main(){
  int w,h,d;
  while(scanf("%d%d%d",&w,&h,&d),h){
    mat A(h*w);
    vec b(h*w);
    rep(i,h)rep(j,w){
      A[i*w+j] = manhattan(i,j,d,h,w);
      scanf("%d",&b[i*w+j]);
    }
    cout << gauss_jordan(A,b) << endl;
  }
}