#include <bits/stdc++.h>
using namespace std;
int n, m, k, w, W0, DD, d[1010], INF = (1 << 30), res;
char s[1010][11][11];
int g[1010][1010], p[1010];
bool u[1010];
vector<int> g1[1010];
void dfs(int v, int p) {
  u[v] = 1;
  printf("%d %d\n", v + 1, (p == -1 || g[p][v] == W0 ? 0 : p + 1));
  for (int i = 0; i < g1[v].size(); i++) {
    if (!u[g1[v][i]]) dfs(g1[v][i], v);
  }
}
int main() {
  scanf("%d%d%d%d", &n, &m, &k, &w);
  W0 = n * m;
  DD = W0 / w;
  for (int kk = 0; kk < k; kk++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf(" %c", &s[kk][i][j]);
      }
    }
  }
  for (int va = 0; va < k; va++) {
    g[va][va] = INF;
    for (int vb = va + 1; vb < k; vb++) {
      int cd = 0;
      for (int i = 0; cd <= DD && i < n; i++) {
        for (int j = 0; cd <= DD && j < m; j++) {
          cd += s[va][i][j] != s[vb][i][j];
        }
      }
      g[va][vb] = g[vb][va] = min(W0, cd * w);
    }
  }
  for (int i = 0; i < k; i++) d[i] = INF;
  d[0] = 0;
  for (int i = 0; i < k; i++) {
    int v = -1;
    for (int j = 0; j < k; j++) {
      if (!u[j] && (v == -1 || d[j] < d[v])) v = j;
    }
    u[v] = 1;
    res += d[v];
    if (v != 0) {
      g1[p[v]].push_back(v);
      g1[v].push_back(p[v]);
    }
    for (int j = 0; j < k; j++) {
      if (g[v][j] < d[j]) {
        d[j] = g[v][j];
        p[j] = v;
      }
    }
  }
  for (int i = 0; i < k; i++) u[i] = 0;
  printf("%d\n", res + W0);
  dfs(0, -1);
}
