#include <bits/stdc++.h>
using namespace std;

#define IDX(i, j) ((i)*m+(j))

typedef vector<int> Vec;
typedef vector<Vec> Mat;

int n;
int m;
int d;
int S[114][514];
int Case;

int main() {
  while (1) {
    scanf("%d%d%d", &m, &n, &d);
    if ((n|m|d) == 0) return 0;

    for (int i=0; i<n; i++) {
      for (int j=0; j<m; j++) {
        scanf("%d", &S[i][j]);
      }
    }

    int sz = n*m;
    Mat aug(sz, Vec(sz+1, 0));
    for (int i=0; i<n; i++) {
      for (int j=0; j<m; j++) {
        int idx = IDX(i, j);
        aug[idx][idx] = 1;
        aug[idx][sz] = S[i][j];
        for (int k=0; k<n; k++) {
          for (int l=0; l<m; l++) {
            if (abs(k-i)+abs(l-j) == d) aug[idx][IDX(k, l)] = 1;
          }
        }
      }
    }

    int lim = 0;
    for (int idx=0; idx<sz+1; idx++) {
      int chosen = -1;
      for (int i=lim; i<sz; i++) {
        if (aug[i][idx]) {
          chosen = i;
          break;
        }
      }

      if (chosen == -1) continue;

      swap(aug[chosen], aug[lim]);
      for (int i=lim+1; i<sz; i++) {
        if (i == lim) continue;
        if (!aug[i][idx]) continue;
        for (int j=idx; j<=sz; j++) {
          aug[i][j] ^= aug[lim][j];
        }
      }

      lim++;
    }

    bool fail = false;
    for (int i=0; i<sz; i++) {
      bool pos = true;
      for (int j=0; j<sz; j++) {
        if (aug[i][j]) {
          pos = false;
          break;
        }
      }

      if (aug[i][sz] == 0) continue;
      if (pos) {
        fail = true;
        break;
      }
    }

    if (fail) puts("0");
    else puts("1");
  }
}