#include <bits/stdc++.h>
using namespace std;
using namespace std;
long long powm(long long base, long long exp, long long mod = 1000000007) {
  long long ans = 1;
  while (exp) {
    if (exp & 1) ans = (ans * base) % mod;
    exp >>= 1, base = (base * base) % mod;
  }
  return ans;
}
long long n, m, k, w, u, v, res;
char a[1005][15][15];
vector<pair<long long, pair<long long, long long>>> edges;
vector<pair<long long, long long>> ans;
vector<long long> adj[1005];
struct DSU {
  long long sze[1005], arr[1005];
  void init() {
    for (long long i = 1; i < 1005; i++) arr[i] = i, sze[i] = 1;
  }
  void get_union(long long a, long long b) {
    long long root_a = root(a), root_b = root(b);
    if (sze[root_a] < sze[root_b])
      arr[root_a] = arr[root_b], sze[root_b] += sze[root_a];
    else
      arr[root_b] = arr[root_a], sze[root_a] += sze[root_b];
  }
  long long root(long long x) {
    while (arr[x] != x) arr[x] = arr[arr[x]], x = arr[x];
    return x;
  }
} dsu;
void get(long long x, long long y) {
  long long cnt = 0, weight;
  for (long long i = 1; i < n + 1; i++)
    for (long long j = 1; j < m + 1; j++)
      if (a[x][i][j] != a[y][i][j]) cnt++;
  if (x)
    weight = (cnt * w);
  else
    weight = cnt;
  edges.push_back(make_pair(weight, make_pair(x, y)));
}
void dfs(long long curr = 0, long long par = -1) {
  for (auto i : adj[curr])
    if (i != par) ans.push_back({i, curr}), dfs(i, curr);
}
int main() {
  cin >> n >> m >> k >> w, dsu.init();
  for (long long i = 1; i < n + 1; i++)
    for (long long j = 1; j < m + 1; j++) a[0][i][j] = '-';
  for (long long l = 1; l < k + 1; l++)
    for (long long i = 1; i < n + 1; i++)
      for (long long j = 1; j < m + 1; j++) cin >> a[l][i][j];
  for (long long i = 0; i < k + 1; i++)
    for (long long j = i + 1; j < k + 1; j++) get(i, j);
  sort((edges).begin(), (edges).end());
  for (auto i : edges) {
    u = i.second.first, v = i.second.second;
    if (dsu.root(u) != dsu.root(v)) {
      dsu.get_union(u, v), res += i.first;
      adj[u].push_back(v), adj[v].push_back(u);
    }
  }
  cout << res << endl;
  dfs();
  for (auto i : ans) cout << i.first << " " << i.second << endl;
  return 0;
}
