#include <bits/stdc++.h>
using namespace std;
int n, m, k, w;
string s[1001][11];
long long dp[1001];
int rest[1001];
bool was[1001];
long long D[1001][1001];
int e[1001], sel[1001];
vector<int> ans;
long long d(int x, int y) {
  int res = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) res += s[x][i][j] != s[y][i][j];
  return res;
}
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (D[a.first][a.second] != D[b.first][b.second])
    return D[a.first][a.second] < D[b.first][b.second];
  return a < b;
}
int main() {
  cin >> n >> m >> k >> w;
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < n; ++j) cin >> s[i][j];
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < i; ++j) D[i][j] = D[j][i] = d(i, j);
  memset(e, 0x7f, sizeof e);
  memset(sel, 0xff, sizeof e);
  e[0] = 0;
  ans.push_back(0);
  dp[0] = n * m;
  for (int i = 0, v; i < k; ++i) {
    v = -1;
    for (int j = 0; j < k; ++j)
      if (!was[j] && (v < 0 || e[j] < e[v])) v = j;
    was[v] = true;
    if (sel[v] != -1) {
      dp[v] = n * m;
      if (dp[v] > D[v][sel[v]] * w)
        dp[v] = D[v][sel[v]] * w, rest[v] = sel[v] + 1;
      ans.push_back(v);
    }
    for (int i = 0; i < k; ++i)
      if (D[v][i] < e[i]) e[i] = D[v][i], sel[i] = v;
  }
  long long res = 0;
  for (int i = 0; i < k; ++i) res += dp[i];
  printf("%I64d\n", res);
  for (int i = 0; i < k; ++i) printf("%d %d\n", ans[i] + 1, rest[ans[i]]);
  return 0;
}
