/* GCC */
#include<stdio.h>
#include<conio.h>
main()
{
    FILE *fin, *fout;
    int q, i, j, n, m, an[1010], am[1010], rel=0, n1, m1, i1, j1, c=0, d, k;
    short a[1010][1010];
    fin = fopen("display.in","r");
    fout = fopen("display.out","w");
    q = fscanf(fin, "%d%d%d%d%d", &n, &m, &n1, &m1, &k);
    for (i=0; i<n; i++)
     for (j=0; j<m; j++)
      q = fscanf(fin, "%d", &a[i][j]);
    for (i=0; i<n-n1+1; i++)
     for (j=0; j<m-m1+1; j++)
      if (a[i][j] != 0)
      {
        d = k - a[i][j];
        for (i1=i; i1<i+n1; i1++)
         for (j1=j; j1<j+m1; j1++)
          a[i1][j1]=(a[i1][j1] + d) % k;
          c = c + d;
      }
    for (i=0; i<n; i++)
     for (j=0; j<m; j++)
      if (a[i][j] != 0) rel = 1;

    if (rel==0) fprintf(fout, "%d\n", c); else fprintf(fout, "impossible\n");
    fclose(fin);
    fclose(fout);
}
