#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
  
using namespace std;
  
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
  
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
  
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int matrix[25 * 25][25 * 25];
int vect[25 * 25];
bool gaussian(int W, int H){
  int N = W * H;
  int target = 0;
  REP(x, N){
    int pivot = target;
    FOR(y, target + 1, N) if(matrix[y][x] == 1) pivot = y;
    if(matrix[pivot][x] == 0) continue;
    REP(i, N) swap(matrix[pivot][i], matrix[target][i]);
    swap(vect[pivot], vect[target]);
    FOR(y, target + 1, N) if(matrix[y][x] == 1){
      FOR(i, x, N) matrix[y][i] ^= matrix[target][i];
      vect[y] ^= vect[target];
    }
    target ++;
  }
  FOR(y, target, N) if(vect[y] == 1) return false;
  return true;
}
  
int main(){
  int W, H, D;
  while (cin>>W>>H>>D && W) {
    memset(matrix, 0, sizeof(matrix));
    memset(vect, 0, sizeof(vect));
    REP(y, H)REP(x, W) cin>>vect[y * W + x];
    REP(y1, H)REP(x1, W){
      REP(y2, H)REP(x2, W){
        if(abs(y1 - y2) + abs(x1 - x2) == D || (x1 == x2 && y1 == y2)){
          matrix[y1 * W + x1][y2 * W + x2] = 1;
        }
      }
    }
    int N = W * H;
    bool ans = gaussian(W, H);
    if(ans) cout<<1<<endl;
    else cout<<0<<endl;
  }
  return 0;
}