def solve
    while(1)
    _input = STDIN.gets
    input = _input.split()
    for i in 1..3
        m = input[0].to_i
        n = input[1].to_i
        d = input[2].to_i
    end
    if(m == 0 && n == 0 && d == 0) then break end
    a = make2d(n,m)
    for i in 0..(n - 1)
        _input = gets
        input = _input.split()
        for j in 0..(m - 1)
            a[i][j] = input[j].to_i
        end
    end
    b = make_matrix(m,n,d,a)
    printf("%d\n", check_matrix(b))
    end
end

def check_matrix(a) 
    height = a.length()
    width = a[0].length
    for k in 0..(width - 2)
        
        max = maxrow(a,k)
        swap(a,k,max)
        
        for i in 0..(height - 1)
            if i != k
                aik = a[i][k]
                for j in k..(width - 1)
                    a[i][j] = (a[i][j] - aik * a[k][j])
                    if ABS(a[i][j]) > 1 then a[i][j] = a[i][j] % 2
                    end
                end
            end
        end
    end
    
    flag = 1
    for i in 0..(height - 1)
        if (a[i][width - 1] != 0 && a[i][i] == 0) then flag = 0
        end
    end
    if flag == 1 then 1
    else 0
    end
end

def make_matrix(m,n,d,a) 
    b = make2d(m * n,(m * n + 1)) 
    for i in 0..(n - 1) 
        for j in 0..(m - 1) 
            
            if a[i][j] == 1
                b[i * m + j][m * n] = 1
            else
                b[i * m + j][m * n] = 0
            end
            
            b[i * m + j][i * m + j] = 1
            
            for k in 0..d
                if (i + k > n - 1) then break 
                else
                    if (j + (d - k) <= m - 1) then b[i * m + j][(i + k) * m + (j + d - k)] = 1
                    end
                    if (j - (d - k) >= 0) then  b[i * m + j][(i + k) * m + (j - d + k)] = 1
                    end
                end
            end
            for k in 0..d
                if (i - k < 0) then break 
                else
                    if (j + (d - k) <= m - 1) then b[i * m + j][(i - k) * m + (j + d - k)] = 1
                    end
                    if (j - (d - k) >= 0)  then b[i * m + j][(i - k) * m + (j - d + k)] = 1
                    end
                end
            end
        end
    end
    b
end

def maxrow(a,k)
    row = a.length()
    max = ABS(a[k][k])
    max_index = k
    for i in k..(row - 1)
        if max < ABS(a[i][k])
            max = ABS(a[i][k])
            max_index = i
        end
    end
    max_index
end

def swap(a,k,l)
    temp = a[k]
    a[k] = a[l]
    a[l] = temp
    a
end

def ABS(n)
    if n < 0
        -n
    else
        n
    end
end

def make1d(n)
    a = Array.new(n)
    for i in 0..(n - 1)
        a[i] = 0
    end
    a
end

def make2d(height,width)
    a = Array.new(height)
    for i in 0..(height-1)
        a[i] = make1d(width)
    end
    a
end

solve