#include <bits/stdc++.h>
using namespace std;
char str[1223][12][12];
int dis[1223][1223];
int n, m, k, w;
int vis[1223], dd[1223];
int cal(int x, int y) {
  int ans = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) ans += str[x][i][j] != str[y][i][j];
  return ans;
}
int uu[1223], pre[1223];
int cc[1223];
int solve(int n) {
  int t = 0, ans = 0;
  for (int i = 1; i <= n; ++i) dd[i] = dis[0][i], pre[i] = 0;
  vis[0] = 1;
  for (int x = 1; x <= n; ++x) {
    int maxv = 0x3f3f3f3f;
    int k = 0;
    for (int i = 1; i <= n; ++i)
      if (!vis[i] && dd[i] < maxv) maxv = dd[i], k = i;
    uu[++t] = k;
    vis[k] = 1;
    ans += dd[k];
    for (int i = 1; i <= n; ++i)
      if (!vis[i] && dd[i] > dis[k][i]) dd[i] = dis[k][i], pre[i] = k;
  }
  return ans;
}
int main() {
  cin >> n >> m >> k >> w;
  for (int i = 1; i <= k; ++i) {
    dis[0][i] = dis[i][0] = n * m;
    for (int j = 0; j < n; ++j) scanf("%s", str[i][j]);
    for (int j = 1; j < i; ++j) dis[i][j] = dis[j][i] = cal(i, j) * w;
  }
  int ans = solve(k);
  printf("%d\n", ans);
  for (int i = 1; i <= k; ++i) {
    printf("%d %d\n", uu[i], pre[uu[i]]);
  }
}
