#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int arr[1000000];
int Size[1000000];
void makeSet(int v) {
  for (int i = 0; i <= v + 1; i++) {
    arr[i] = i;
    Size[i] = 1;
  }
}
int find(int u) {
  if (arr[u] == u) return u;
  arr[u] = find(arr[u]);
  return arr[u];
}
void munion(int u, int v) {
  int rootu = find(u);
  int rootv = find(v);
  if (rootu == rootv) return;
  if (Size[rootu] < Size[rootv]) {
    arr[rootu] = arr[rootv];
    Size[rootv] += Size[rootu];
  } else {
    arr[rootv] = arr[rootu];
    Size[rootu] += Size[rootv];
  }
}
bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  return a.first < b.first;
}
string a[10001][11];
long long n, m, k, w;
long long diff(long long i, long long j) {
  long long cnt = 0;
  for (long long x = 0; x < n; x++) {
    for (long long y = 0; y < m; y++) {
      if (a[i][x][y] != a[j][x][y]) cnt++;
    }
  }
  return cnt;
}
void dfs(int u, int par, vector<long long> adj[]) {
  if (par != -1) {
    cout << u << " " << par << endl;
  }
  for (auto v : adj[u]) {
    if (v != par) dfs(v, u, adj);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n >> m >> k >> w;
  for (long long i = 1; i < k + 1; i++) {
    for (long long j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  vector<pair<int, pair<int, int>>> edges;
  for (long long i = 1; i < k + 1; i++) {
    edges.push_back(make_pair(n * m, make_pair(0, i)));
  }
  for (long long i = 1; i < k + 1; i++) {
    for (long long j = i + 1; j <= k; j++) {
      long long temp = diff(i, j) * w;
      edges.push_back(make_pair(temp, make_pair(i, j)));
    }
  }
  sort(edges.begin(), edges.end(), cmp);
  makeSet(k);
  int E = edges.size();
  long long ans = 0;
  vector<long long> adj[k + 1];
  for (int i = 0; i < E; i++) {
    int u = edges[i].second.first;
    int v = edges[i].second.second;
    int we = edges[i].first;
    int rootu = find(u);
    int rootv = find(v);
    if (rootu == rootv) continue;
    ans += we;
    munion(u, v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  cout << ans << endl;
  dfs(0, -1, adj);
}
