#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.1415926535
#define pf push_front
#define ll long long int
#define ull unsigned long long int
typedef pair<int,int> pii;
uint64_t seed = reinterpret_cast<uint64_t>(new char);
mt19937_64 rd(seed);
//uniform_int_distribution<int> rg(1, n);
//int RandomNumber = rg(rd);
// vector < int > v(N,-1); sqrtl for square root of ll
// vector of size N with initial value -1
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> OST;
#define gc getchar_unlocked
const int N=60005;
const int M=1000005;
int upd[2005];
vector <pii> v[1005];
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i,j,k,l,n,m,p;
cin>>n>>m>>k;
int arr[n+5][m+5];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>arr[i][j];
}
}
int cnt=0;
for(i=0;i<n;i++)
{
for(auto pk : v[i]){
upd[pk.first]--; upd[pk.second]++;
}
int pre=0;
for(j=0;j<m;j++)
{
pre=pre+upd[j];
int xd=pre;
if(arr[i][j]==0){
if(xd%2==0){
if((j+k-1)>=m || (i+k-1)>=n){
cout<<-1<<endl; return 0;
}
upd[j]++; pre++; upd[j+k]--;
v[i+k].pb({j,j+k});
cnt++;
}
}
if(arr[i][j]==1){
if(xd%2){
if((j+k-1)>=m || (i+k-1)>=n){
cout<<-1<<endl; return 0;
}
upd[j]++; pre++; upd[j+k]--;
cnt++; v[i+k].pb({j,j+k});
}
}
}
}
cout<<cnt<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CnVza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gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKE5VTEwpOwogICAgaW50IGksaixrLGwsbixtLHA7CiAgICBjaW4+Pm4+Pm0+Pms7CiAgICBpbnQgYXJyW24rNV1bbSs1XTsKICAgIGZvcihpPTA7aTxuO2krKykKICAgIHsKICAgICAgICBmb3Ioaj0wO2o8bTtqKyspCiAgICAgICAgewogICAgICAgICAgICBjaW4+PmFycltpXVtqXTsKICAgICAgICB9CiAgICB9CiAgICBpbnQgY250PTA7CiAgICBmb3IoaT0wO2k8bjtpKyspCiAgICB7CiAgICAgICAgZm9yKGF1dG8gcGsgOiB2W2ldKXsKICAgICAgICAgICAgdXBkW3BrLmZpcnN0XS0tOyAgdXBkW3BrLnNlY29uZF0rKzsKICAgICAgICB9CiAgICAgICAgaW50IHByZT0wOwogICAgICAgIGZvcihqPTA7ajxtO2orKykKICAgICAgICB7CiAgICAgICAgICAgIHByZT1wcmUrdXBkW2pdOwogICAgICAgICAgICBpbnQgeGQ9cHJlOwogICAgICAgICAgICBpZihhcnJbaV1bal09PTApewogICAgICAgICAgICAgICAgaWYoeGQlMj09MCl7CiAgICAgICAgICAgICAgICAgICAgaWYoKGoray0xKT49bSB8fCAoaStrLTEpPj1uKXsKICAgICAgICAgICAgICAgICAgICAgICAgY291dDw8LTE8PGVuZGw7ICByZXR1cm4gMDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgdXBkW2pdKys7IHByZSsrOyAgdXBkW2ora10tLTsKICAgICAgICAgICAgICAgICAgICB2W2kra10ucGIoe2osaitrfSk7IAogICAgICAgICAgICAgICAgICAgIGNudCsrOwogICAgICAgICAgICAgICAgfSAgCiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoYXJyW2ldW2pdPT0xKXsKICAgICAgICAgICAgICAgIGlmKHhkJTIpewogICAgICAgICAgICAgICAgICAgIGlmKChqK2stMSk+PW0gfHwgKGkray0xKT49bil7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQ8PC0xPDxlbmRsOyAgcmV0dXJuIDA7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIHVwZFtqXSsrOyAgcHJlKys7ICB1cGRbaitrXS0tOwogICAgICAgICAgICAgICAgICAgIGNudCsrOyAgIHZbaStrXS5wYih7aixqK2t9KTsgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0PDxjbnQ8PGVuZGw7CiAgICByZXR1cm4gMDsgCn0K