#include <bits/stdc++.h>
using namespace std;
const int maxn4 = 1e4 + 85, maxn5 = 1e5 + 69, maxn6 = 1e6 + 85,
          maxn7 = 1e7 + 69, maxn8 = 1e8 + 85, maxn9 = 1e9 + 69, modi1 = 1e7 + 7,
          modi2 = 1e9 + 7;
const long long int mod1 = 1e7 + 7, mod2 = 1e9 + 7;
int mn = INT_MAX, mx = INT_MIN;
long long int n, m, w, k;
char c[1111][13][13];
vector<pair<long long int, pair<long long int, long long int> > > e;
vector<long long int> H[1111];
long long int par[1111], visited[1111];
int rot(int v) { return par[v] < 0 ? v : par[v] = rot(par[v]); }
void merge(long long int a, long long int b) {
  if ((a = rot(a)) == (b = rot(b))) return;
  if (par[a] > par[b]) swap(a, b);
  par[a] = b;
}
void dfs(long long int u, long long int paar) {
  visited[u] = 1;
  if (paar != -1) {
    cout << u + 1 << ' ';
    if (paar == 1001) paar = -1;
    cout << paar + 1 << '\n';
  }
  for (auto v : H[u])
    if (!visited[v]) dfs(v, u);
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> n >> m >> k >> w;
  for (int i = 0; i < k; i++)
    for (int j = 0; j < n; j++)
      for (int p = 0; p < m; p++) cin >> c[i][j][p];
  for (int i = 0; i < k; i++)
    for (int j = i + 1; j < k; j++) {
      long long int sum = 0;
      for (int q = 0; q < n; q++)
        for (int p = 0; p < m; p++)
          if (c[i][q][p] != c[j][q][p]) sum++;
      e.push_back({w * sum, {i, j}});
    }
  long long int root = 1001;
  for (int i = 0; i < k; i++) e.push_back({n * m, {i, root}});
  sort((e).begin(), (e).end());
  memset(par, -1, sizeof(par));
  long long int sum = 0;
  for (auto u : e) {
    long long int a = u.second.first, b = u.second.second, w = u.first;
    if (rot(a) != rot(b)) {
      sum += w;
      merge(a, b);
      H[a].push_back(b);
      H[b].push_back(a);
    }
  }
  cout << sum << "\n";
  dfs(1001, -1);
  return 0;
}
