#include <bits/stdc++.h>
using namespace std;
int z, n, m, k, w, h, c;
vector<pair<int, int> > ret;
char tab[1004][12][12];
int diff[1004][1004];
vector<pair<int, int> > nbs[1004];
int vis[1004];
int res[1004];
int prv[1004];
int dist[1004];
int bfs(int start) {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      pq;
  pq.push(make_pair(w * h, start));
  prv[start] = start;
  dist[start] = 0;
  int res = 0;
  while (pq.size()) {
    pair<int, int> c = pq.top();
    pq.pop();
    int v = c.second;
    if (vis[v]++) continue;
    res += c.first;
    ret.push_back(make_pair(v + 1, (prv[v] == v ? 0 : prv[v] + 1)));
    for (int i = 0; i < nbs[v].size(); i++) {
      int nb = nbs[v][i].first;
      int second = nbs[v][i].second;
      if (dist[nb] > second) {
        prv[nb] = v;
        dist[nb] = second;
        pq.push(make_pair(second, nb));
      }
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> h >> w >> n >> c;
  for (int i = 0; i < n; i++) dist[i] = 1000000001;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < h; j++)
      for (int k = 0; k < w; k++) cin >> tab[i][j][k];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      for (int k = 0; k < h; k++)
        for (int l = 0; l < w; l++)
          diff[i][j] += (tab[i][k][l] != tab[j][k][l]);
      diff[j][i] = diff[i][j];
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      if (c * diff[i][j] < w * h) {
        nbs[i].push_back(make_pair(j, c * diff[i][j]));
        nbs[j].push_back(make_pair(i, c * diff[i][j]));
      }
    }
  int res = 0;
  for (int i = 0; i < n; i++)
    if (dist[i] >= 1000000001) {
      res += bfs(i);
    }
  cout << res << endl;
  for (int i = 0; i < ret.size(); i++) {
    cout << ret[i].first << " " << ret[i].second << endl;
  }
  return 0;
}
