#include<bits/stdc++.h>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)


const int N = 25;
const int NN = N*N;

class Mat : public vector<vector<int> >{
public:
  void init(int h,int w){
    clear();
    rep(i,h)push_back(vector<int>(w));
  }
  void print(){
    rep(i,size()){
      rep(j,at(i).size())printf("%d ",at(i).at(j));puts("");
    }
  }
};

int haki(Mat& mat){
  int used[NN]={0};
  int y = mat.size();
  int x = mat[0].size();

  int rank = 0;
  rep(i,x){
    rep(j,y){
      if(used[j]==0 && mat[j][i]==1){
	used[j] = 1;
	rank++;

	rep(k,y){
	  if(used[k]==0 && mat[k][i]==1){
	    rep(p,x)mat[k][p]^=mat[j][p];
	  }
	}
      }
    }
  }

  return rank;
}


int w,h,d;
int board[N][N];

bool input(){
  cin>>w>>h>>d;
  if(w==0)return false;

  rep(i,h)rep(j,w)cin>>board[i][j];

  return true;
}


Mat A;
Mat B;
int n;
int solve(){
  n=w*h;
  A.init(n, n);
  B.init(n, n+1);

  rep(i,n){
    rep(j,n){
      int x = i%w;
      int y = i/w;
      int s = j%w;
      int t  = j/w;
      if((x==s && y==t) || abs(x-s)+abs(y-t)==d){
	A[i][j] = 1;
	B[i][j] = 1;
      }
    }
  }

  rep(i,n){
    B[i][n]=board[i/w][i%w];
  }

  //  A.print();
  //B.print();

  int va = haki(A);
  int vb = haki(B);

  //printf("%d : %d\n",va,vb);
  if(va==vb)return 1;
  return 0;
}

int main(){
  while(input())cout<<solve()<<endl;
}