#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > AdjList;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
class uf {
 public:
  vector<int> ps;
  uf(int n) {
    ps.assign(n, 0);
    for (int i = 0; i < n; i++) ps[i] = i;
  }
  int findSet(int i) {
    if (ps[i] == i) return i;
    return ps[i] = findSet(ps[i]);
  }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void Set(int i, int j) {
    int x = findSet(i);
    int y = findSet(j);
    ps[y] = x;
  }
};
string data[1010];
int val(int i, int j) {
  int counter = 0;
  for (int k = 0; k < data[i].size(); k++) {
    counter += (data[i][k] != data[j][k]);
  }
  return counter;
}
vector<int> dfs_num;
void dfs(int i, int p) {
  if (p >= 0) {
    cout << i << ' ' << p << endl;
  }
  dfs_num[i] = 1;
  for (int j = 0; j < AdjList[i].size(); j++) {
    int v = AdjList[i][j];
    if (dfs_num[v] == -1) {
      dfs(v, i);
    }
  }
}
int main() {
  int n, m, k, w;
  cin >> n >> m >> k >> w;
  uf f(k + 1);
  dfs_num.assign(k + 1, -1);
  vector<pair<int, pair<int, int> > > edges;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
      for (int l = 0; l < m; l++) {
        char in;
        cin >> in;
        data[i].push_back(in);
      }
    }
    edges.push_back(
        pair<int, pair<int, int> >(n * m, pair<int, int>(0, i + 1)));
  }
  for (int i = 0; i < k; i++) {
    for (int j = i + 1; j < k; j++) {
      if (i == j) continue;
      edges.push_back(pair<int, pair<int, int> >(val(i, j) * w,
                                                 pair<int, int>(j + 1, i + 1)));
    }
  }
  int ans = 0;
  map<int, int> withZero;
  map<int, int> added;
  vector<pair<int, int> > print;
  AdjList.assign(k + 1, vector<int>());
  sort(edges.begin(), edges.end());
  for (int i = 0; i < edges.size(); i++) {
    pair<int, pair<int, int> > d = edges[i];
    int u = d.second.first;
    int v = d.second.second;
    if (!f.isSameSet(u, v)) {
      AdjList[u].push_back(v);
      AdjList[v].push_back(u);
      ans += d.first;
      f.Set(u, v);
    }
  }
  cout << ans << endl;
  dfs(0, -1);
  return 0;
}
