#include <bits/stdc++.h>
char g[1001][11][11];
int type[1001];
int so[1001];
int va[1001];
using namespace std;
int n, m, k, w;
int dis(int i, int j) {
  int ans = 0;
  for (int l = 1; l <= n; l++) {
    for (int r = 1; r <= m; ++r) {
      if (g[i][l][r] != g[j][l][r]) ans++;
    }
  }
  return ans;
}
int main() {
  while (cin >> n >> m >> k >> w) {
    for (int i = 1; i <= k; ++i)
      for (int j = 1; j <= n; ++j) scanf("%s", g[i][j] + 1);
    for (int i = 1; i <= k; ++i) va[i] = n * m;
    int di = 1;
    int ans = 0;
    memset(type, 0, sizeof(type));
    for (int i = 1; i <= k; ++i) {
      int mi = m * n + 1;
      int tt;
      for (int j = 1; j <= k; ++j) {
        if (va[j] >= 0 && va[j] < mi) {
          mi = va[j];
          tt = j;
        }
      }
      so[di++] = tt;
      ans += mi;
      va[tt] = -1;
      for (int j = 1; j <= k; ++j) {
        if (va[j] < 0) continue;
        int tem = dis(j, tt) * w;
        if (tem < va[j]) {
          type[j] = tt;
          va[j] = tem;
        }
      }
    }
    cout << ans << endl;
    for (int i = 1; i <= k; ++i) {
      cout << so[i] << " " << type[so[i]] << endl;
    }
  }
  return 0;
}
