#include <bits/stdc++.h>
using namespace std;
char g[1001][10][10];
int diff[1001][1001], d[1001], pr[1001], order[1001];
int n, m, k, w;
bool vis[10001];
int main() {
  scanf("%d%d%d%d", &n, &m, &k, &w);
  for (int l = (1); l < (long long)(k + 1); l++)
    for (int i = (0); i < (long long)(n); i++) scanf("%s", g[l][i]);
  for (int i = (1); i < (long long)(k + 1); i++)
    for (int j = (i + 1); j < (long long)(k + 1); j++) {
      diff[i][j] = 0;
      for (int r = (0); r < (long long)(n); r++)
        for (int c = (0); c < (long long)(m); c++)
          diff[i][j] += (g[i][r][c] != g[j][r][c]);
      diff[i][j] *= w;
      diff[j][i] = diff[i][j];
    }
  for (int i = (1); i < (long long)(k + 1); i++)
    diff[0][i] = diff[i][0] = n * m;
  for (int i = (0); i < (long long)(k + 1); i++) diff[i][i] = 0;
  for (int i = (0); i < (long long)(k + 1); i++) d[i] = (int)(1e9 + 10);
  d[0] = 0;
  memset(pr, -1, sizeof pr);
  memset(vis, 0, sizeof vis);
  long long ans = 0;
  for (int i = (0); i < (long long)(k + 1); i++) {
    int mnd = (int)(1e9 + 10), mn;
    for (int j = (0); j < (long long)(k + 1); j++)
      if (!vis[j] && d[j] < mnd) mnd = d[j], mn = j;
    vis[mn] = true;
    for (int j = (0); j < (long long)(k + 1); j++)
      if (!vis[j] && d[j] > diff[mn][j]) d[j] = diff[mn][j], pr[j] = mn;
    ans += mnd;
    order[i] = mn;
  }
  printf(
      "%lld"
      "\n",
      ans);
  for (int i = (1); i < (long long)(k + 1); i++)
    printf("%d %d\n", order[i], pr[order[i]]);
  return 0;
}
