/* GCC */
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <queue>
#include <sstream>

using namespace std;

long long otv;
int n, m, n1, m1, i, j, k, l, o, t;
vector < vector <int> > a;
bool flag;

int main()
{
    freopen("display.in", "r", stdin);
    freopen("display.out", "w", stdout);

    otv = 0;
    scanf("%d %d", &n, &m);
    scanf("%d %d", &n1, &m1);
    scanf("%d", &k);
    a.resize(n + 10, vector <int> (m + 10));
    for(i = 0; i < n; i++)
        for(j = 0; j < m; j++)
            scanf("%d", &a[i][j]);

    for(i = 0; i < n - n1 + 1; i++)
        for(j = 0; j < m - m1 + 1; j++)
        {
            //cerr << i << " " << j << endl;

            if(a[i][j] == 0)
                continue;

            otv = otv + (k - a[i][j]);
            //cerr << i << " " << j << " " << otv << endl;

            t = a[i][j];

            for(l = i; l < i + n1; l++)
                for(o = j; o < j + m1; o++)
                    a[l][o] = (a[l][o] - t + k) % k;

            /*for(l = 0; l < n; l++)
            {
                for(o = 0; o < m; o++)
                    cerr << a[l][o] << " ";
                cerr << endl;
            }
            cerr << endl;*/
        }

    flag = false;
    for(i = n - n1 + 1; i < n; i++)
        for(j = 0; j < m; j++)
            if(a[i][j] != 0)
            {
                flag = true;
                break;
            }

    if(flag == true)
    {
        printf("impossible\n");
        return 0;
    }

    for(i = 0; i < n - n1 + 1; i++)
        for(j = m - m1 + 1; j < m; j++)
            if(a[i][j] != 0)
            {
                flag = true;
                break;
            }

    if(flag == true)
    {
        printf("impossible\n");
        return 0;
    }

    cout << otv << endl;

    return 0;
}

