#include <bits/stdc++.h>
using namespace std;
struct edge {
  int a, b, c;
} e[1000100];
int t, sz, n, m, k, w, d[2000][2000], p[2000];
string a[2000][20];
pair<int, int> st[2000];
bool used[2000];
vector<int> g[2000];
int get(int x) { return p[x] == x ? x : p[x] = get(p[x]); }
void join(int a, int b) { p[get(a)] = get(b); }
bool cmp(edge a, edge b) { return a.c < b.c; }
void dfs(int v) {
  used[v] = 1;
  for (int i = 0; i < (int)g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to]) {
      st[++t] = make_pair(to + 1, v + 1);
      dfs(to);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> k >> w;
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < n; ++j) cin >> a[i][j];
  for (int i = 0; i < k; ++i) p[i] = i;
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < k; ++j) {
      if (i == j) continue;
      for (int l = 0; l < n; ++l)
        for (int s = 0; s < m; ++s)
          if (a[i][l][s] != a[j][l][s]) d[i][j]++;
      if (w * d[i][j] >= n * m)
        d[i][j] = int(1e9);
      else
        d[i][j] *= w;
    }
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < k; ++j)
      if (i != j) {
        ++sz;
        e[sz].a = i;
        e[sz].b = j;
        e[sz].c = d[i][j];
      }
  sort(e + 1, e + sz + 1, cmp);
  int ans = 0;
  for (int i = 1; i <= sz; ++i)
    if (get(e[i].a) != get(e[i].b) && e[i].c < int(1e9)) {
      ans += e[i].c;
      join(e[i].a, e[i].b);
      g[e[i].b].push_back(e[i].a);
      g[e[i].a].push_back(e[i].b);
    }
  for (int i = 0; i < k; ++i)
    if (!used[i]) {
      st[++t] = make_pair(i + 1, 0);
      ans += n * m;
      dfs(i);
    }
  cout << ans << '\n';
  for (int i = 1; i <= t; ++i)
    cout << st[i].first << ' ' << st[i].second << '\n';
}
