#include <bits/stdc++.h>
using namespace std;
char c[1010][110];
int cost[1010][1010];
vector<pair<int, pair<int, int> > > e;
int p[1010];
int rnd = 0;
int get(int a) {
  if (p[a] == a) return a;
  return p[a] = get(p[a]);
}
bool uni(int a, int b) {
  a = get(a);
  b = get(b);
  if (a == b) return false;
  if (rnd ^= 1) {
    p[a] = b;
  } else {
    p[b] = a;
  }
  return true;
}
vector<int> a[1010];
int n, m, k, w;
void calc(int v, int pr = -1) {
  if (v != k - 1) {
    cout << v + 1 << ' ';
    if (pr == k - 1) {
      cout << 0 << endl;
    } else
      cout << pr + 1 << endl;
  }
  for (int i = 0; i < int(a[v].size()); i++) {
    int to = a[v][i];
    if (to == pr) continue;
    calc(to, v);
  }
}
int main() {
  cin >> n >> m >> k >> w;
  string s;
  getline(cin, s);
  for (int i = 0; i < int(k); i++) {
    for (int j = 0; j < int(n); j++) {
      for (int z = 0; z < int(m); z++) {
        scanf("%c", &c[i][j * m + z]);
      }
      getline(cin, s);
    }
  }
  n *= m;
  for (int i = 0; i < int(k); i++) {
    for (int j = 0; j < int(i); j++) {
      for (int z = 0; z < int(n); z++) {
        cost[i][j] += (c[i][z] != c[j][z]);
      }
      cost[i][j] *= w;
    }
  }
  for (int i = 0; i < int(k); i++) {
    cost[i][k] = cost[k][i] = n;
  }
  for (int i = 0; i < int(k + 1); i++) {
    for (int j = 0; j < int(i); j++) {
      e.push_back(make_pair(cost[i][j], make_pair(i, j)));
    }
  }
  ++k;
  sort(e.begin(), e.end());
  for (int i = 0; i < int(k); i++) p[i] = i;
  int ans = 0;
  for (int i = 0; i < int(e.size()); i++) {
    if (uni(e[i].second.first, e[i].second.second)) {
      a[e[i].second.first].push_back(e[i].second.second);
      a[e[i].second.second].push_back(e[i].second.first);
      ans += e[i].first;
    }
  }
  cout << ans << endl;
  calc(k - 1);
  return 0;
}
