{ Free Pascal }

var
 n,m,n1,m1,k,i,j,ans:longint;
 mas:array[0..1001,0..1001] of integer;
 b:boolean;

procedure smena(x,y:longint);
var
 si,sj,sk:longint;
begin
sk:=k-mas[x,y];
 if (sk<>k) and (sk<>0) then
   begin
     for si:=x to x+n1-1 do
        for sj:=y to y+m1-1 do
          begin
             mas[si,sj]:=(mas[si,sj]+sk) mod k;
          end;
      ans:=ans+sk;
   end;
end;

begin
assign(input,'display.in');
assign(output,'display.out');
reset(input);
rewrite(output);
 readln(n,m);
 readln(n1,m1);
 readln(k);
ans:=0;
 for i:=1 to n do
    for j:=1 to m do
        read(mas[i,j]);

 for i:=1 to n-n1+1 do
   for j:=1 to m-m1+1 do
      smena(i,j);

b:=true;
for i:=n-n1+2 to n do
  for j:=1 to m do
    if mas[i,j]<>0 then
       b:=false;

for i:=1 to n do
  for j:=m-m1+2 to m do
    if mas[i,j]<>0 then
       b:=false;

if b then
  writeln(ans)
else
  writeln('impossible');


end.
