#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e12;
const int MAXN = 1050;
long long e[MAXN][MAXN];
long long dist[MAXN];
bool used[MAXN];
long long parent[MAXN];
std::vector<std::vector<int> > g(1050);
std::vector<string> v[1050];
long long n, m;
long long k, w;
long long getDiff(long long x, long long y) {
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    for (long long j = 0; j < m; ++j) {
      ans += v[x][i][j] != v[y][i][j];
    }
  }
  return w * ans;
}
int main() {
  cin >> n >> m >> k >> w;
  for (long long l = 0; l < k; ++l) {
    for (long long i = 0; i < n; ++i) {
      string s;
      cin >> s;
      v[l].push_back(s);
    }
  }
  long long ans = 0;
  for (int i = 0; i <= k; ++i) {
    for (int j = 0; j <= k; j++) e[i][j] = INF;
  }
  for (long long i = 1; i <= k; ++i) {
    for (long long j = 1; j <= k; ++j) {
      if (i == j) continue;
      e[i][j] = getDiff(i - 1, j - 1);
    }
  }
  for (int i = 1; i <= k; ++i) {
    e[0][i] = e[i][0] = n * m;
  }
  dist[0] = 0;
  for (int i = 1; i <= k; i++) {
    parent[i] = 0;
    dist[i] = INF;
  }
  std::vector<pair<long long, long long> > res;
  for (int i = 0; i <= k; i++) {
    int cur = -1;
    for (int j = 0; j <= k; j++)
      if (!used[j] && (cur == -1 || dist[j] < dist[cur])) {
        cur = j;
      }
    used[cur] = true;
    ans += dist[cur];
    res.push_back(pair<long long, long long>(cur, parent[cur]));
    for (int j = 0; j <= k; j++) {
      if (used[j]) continue;
      if (dist[j] > e[cur][j]) {
        dist[j] = e[cur][j];
        parent[j] = cur;
      }
    }
  }
  cout << ans << endl;
  for (int i = 1; i < res.size(); ++i) {
    cout << res[i].first << " " << res[i].second << endl;
  }
  return 0;
}
