#include <bits/stdc++.h>
using namespace std;
int cost[1010][1010];
char board[1010][11][11];
int dist[1010];
int prv[1010];
int main() {
  int n, m, k, w;
  scanf("%d %d %d %d", &n, &m, &k, &w);
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%s", board[i][j]);
    }
  }
  for (int i = 0; i < k; ++i) {
    for (int j = i + 1; j < k; ++j) {
      for (int r = 0; r < n; ++r) {
        for (int c = 0; c < m; ++c) {
          cost[i][j] += (board[i][r][c] != board[j][r][c]);
        }
      }
      cost[i][j] = cost[j][i] = cost[i][j] * w;
    }
  }
  memset((prv), -1, sizeof(prv));
  ;
  for (int i = 0; i < k; ++i) {
    dist[i] = n * m;
  }
  bool vis[1010] = {false};
  int ans = 0;
  for (int cnt = 0; cnt < k; ++cnt) {
    int mn = INT_MAX;
    int ind = -1;
    for (int i = 0; i < k; ++i) {
      if (!vis[i] && dist[i] < mn) {
        mn = dist[i];
        ind = i;
      }
    }
    vis[ind] = true;
    if (cnt != 0)
      ans += dist[ind];
    else
      ans += n * m;
    for (int i = 0; i < k; ++i) {
      if (!vis[i]) {
        if (cost[ind][i] < dist[i]) {
          dist[i] = cost[ind][i];
          prv[i] = ind;
        }
      }
    }
  }
  printf("%d\n", ans);
  vector<pair<int, int> > v;
  for (int i = 0; i < k; ++i) {
    if (prv[i] == k) prv[i] = -1;
    v.push_back(make_pair(prv[i] + 1, i + 1));
  }
  memset((vis), 0, sizeof(vis));
  ;
  vis[0] = true;
  while (true) {
    bool ok = true;
    for (int i = 1; i <= k; ++i) {
      if (!vis[i]) {
        ok = false;
        break;
      }
    }
    if (ok) break;
    for (int j = 0; j < v.size(); ++j) {
      if (vis[v[j].first] && !vis[v[j].second]) {
        printf("%d %d\n", v[j].second, v[j].first);
        vis[v[j].second] = true;
      }
    }
  }
  printf("\n");
  return 0;
}
