#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > ans;
const int MAXN = 1004;
const int INF = 1000000000;
char s[1004][14][14];
int u;
int prim(int n, int mat[][MAXN], int *pre) {
  int mind[MAXN], ret = 0;
  int v[MAXN], i, j, k;
  for (i = 0; i < n; i++) {
    mind[i] = u;
    v[i] = 0;
    pre[i] = -1;
  }
  for (mind[j = 0] = u; j < n; j++) {
    for (k = -1, i = 0; i < n; i++) {
      if (!v[i] && (k == -1 || mind[i] < mind[k])) {
        k = i;
      }
    }
    v[k] = 1;
    ans.push_back(make_pair(k + 1, pre[k] + 1));
    ret += mind[k];
    for (i = 0; i < n; i++) {
      if (!v[i] && mat[k][i] < mind[i]) {
        mind[i] = mat[pre[i] = k][i];
      }
    }
  }
  return ret;
}
int n, m, k, w;
int dist(int x, int y) {
  int cnt = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (s[x][i][j] != s[y][i][j]) cnt++;
  return cnt * w;
}
int p[1004];
int d[1004], e[1004][1004];
int main() {
  cin >> n >> m >> k >> w;
  u = n * m;
  for (int c = 0; c < k; ++c)
    for (int i = 0; i < n; ++i) scanf("%s", s[c][i]);
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < k; ++j)
      if (i != j) e[i][j] = min(n * m, dist(i, j));
  cout << prim(k, e, p) << endl;
  for (int i = 0; i < k; ++i)
    cout << ans[i].first << " " << ans[i].second << endl;
  return 0;
}
