#include <bits/stdc++.h>
using namespace std;
int n, m, k, w, tot;
char g[1111][11][11];
int visd[1111];
vector<pair<int, int> > ans;
priority_queue<pair<int, pair<int, int> > > q;
void expand(int cur) {
  for (int nxt = 1; nxt <= k; nxt++) {
    if (visd[nxt]) continue;
    int val = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (g[cur][i][j] != g[nxt][i][j]) val += w;
    val = min(val, n * m);
    q.push(make_pair(-val, make_pair(nxt, cur)));
  }
}
int main() {
  scanf("%d%d%d%d", &n, &m, &k, &w);
  for (int i = 1; i <= k; i++)
    for (int j = 0; j < n; j++) scanf("%s", g[i][j]);
  tot = n * m;
  ans.push_back(make_pair(1, 0));
  memset(visd, false, sizeof visd);
  visd[1] = true;
  expand(1);
  for (int i = 2; i <= k;) {
    pair<int, pair<int, int> > p = q.top();
    q.pop();
    int nxt = p.second.first;
    if (visd[nxt]) continue;
    i++;
    int pre = p.second.second;
    int dist = p.first;
    visd[nxt] = true;
    expand(nxt);
    if (-dist == n * m)
      ans.push_back(make_pair(nxt, 0));
    else
      ans.push_back(make_pair(nxt, pre));
    tot -= dist;
  }
  printf("%d\n", tot);
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first, ans[i].second);
  return 0;
}
