{ Free Pascal }
program display;
var res: int64;
    i1,j1,n,m,n1,m1,k,i,j,kol: longint;
    t: array [1..1000,1..1000] of longint;

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

 for i:=1 to (n-n1+1) do
 begin
   for j:=1 to (m-m1+1) do
   begin
     if t[i,j]>0 then
     begin
     kol:=k-t[i,j];
     for i1:=i to (i+n1-1) do
     for j1:=j to (j+m1-1) do
       t[i1,j1]:=(t[i1,j1]+kol) mod k;
     res:=res+kol;
     end;
   end;

   for j:=1 to m do
     if t[i,j]>0 then
     begin
       res:=-1;
       break;
     end;
  end;

  if res=-1 then writeln('impossible')
  else writeln(res);
 end.







