import std.stdio;
import std.ascii;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

class Matrix {
    int Y, X;
    int[][] F;
    this(int Y, int X) {
        this.Y = Y;
        this.X = X;
        F = new int[][](Y, X);
    }
    void set(int y, int x, int v) { F[y][x] = v; }
    int get(int y, int x) { return F[y][x]; }
    int[] get(int y) { return F[y]; }
    override string toString() const {
        string[] Ret;
        foreach (L; F) Ret ~= L.map!"a ? 'o' : 'x'".array;
        return Ret.join("\n");
    }
    // 繧ャ繧ヲ繧ケ縺ョ豸亥悉豕? 遐エ螢顔噪. rank繧定ソ斐☆.
    int GaussianElimination() {
        int rank = 0;
        int ny = 0;
        int pivot(int x) {
            foreach (y; ny .. Y) {
                if (F[y][x] != 0) return y;
            }
            return -1;
        }
        foreach (int px; 0 .. X) {
            int py = pivot(px);
            if (py < 0) continue;
            rank++;
            swap(F[py], F[ny]);
            py = ny;
            ny++;
            foreach (int y; 0 .. Y) {
                if (y == py) continue;
                int d = F[y][px];
                if (d == 0) continue;
                foreach (int x; 0 .. X) {
                    F[y][x] = (F[py][x] + F[y][x]) % 2;
                }
            }
        }
        return rank;
    }
}


const dy = [-1, 0, 1, 0],
      dx = [0, 1, 0, -1];

void main() {
    int X, Y, D;
    int[][] F;
    bool input() {
        scanf("%d %d %d\n", &X, &Y, &D);
        if (X == 0 && Y == 0 && D == 0) return false;
        F = new int[][Y];
        foreach (i; 0 .. Y) F[i] = readln.chomp.split(" ").map!(to!int).array;
        return true;
    }
    void solve() {
        Matrix m = new Matrix(Y * X, Y * X + 1);
        Matrix n = new Matrix(Y * X, Y * X);
        foreach (y; 0 .. Y) {
            foreach (x; 0 .. X) {
                m.set(Y * y + x, Y * X, F[y][x]);
            }
        }
        foreach (y1; 0 .. Y) {
            foreach (x1; 0 .. X) {
                m.set(X * y1 + x1, X * y1 + x1, 1);
                n.set(X * y1 + x1, X * y1 + x1, 1);
                foreach (y2; 0 .. Y) {
                    foreach (x2; 0 .. X) {
                        if (abs(y1 - y2) + abs(x1 - x2) == D) {
                            m.set(X * y1 + x1, X * y2 + x2, 1);
                            n.set(X * y1 + x1, X * y2 + x2, 1);
                        }
                    }
                }
            }
        }
        writeln( ( m.GaussianElimination == n.GaussianElimination ).to!int );
    }
    while (input) solve;
}