#include <bits/stdc++.h>
using namespace std;
int X, Y, m, k;
char maps[1001][10][10];
int G[1001][1001];
int w[1001];
int taken[1001], padre[1001];
struct cmp {
  bool operator()(pair<int, int> a, pair<int, int> b) {
    return G[a.first][a.second] > G[b.first][b.second];
  }
};
int main() {
  while (cin >> Y >> X >> m >> k) {
    memset(G, 0, sizeof(G)), memset(taken, 0, sizeof(taken));
    for (int i = (1); i < (m + 1); i++) {
      for (int y = (0); y < (Y); y++)
        for (int x = (0); x < (X); x++) {
          cin >> maps[i][x][y];
          for (int j = (1); j < (i); j++)
            if (maps[i][x][y] != maps[j][x][y]) G[i][j] += k, G[j][i] += k;
        }
      G[0][i] = X * Y;
      G[i][0] = X * Y;
      padre[i] = 0;
    }
    priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> q;
    taken[0] = true;
    for (int i = (1); i < (m + 1); i++) q.push(make_pair(0, i));
    vector<pair<int, int> > r;
    int mcost = 0;
    while (((int)q.size())) {
      pair<int, int> v = q.top();
      q.pop();
      if (!taken[v.second]) {
        taken[v.second] = 1;
        r.push_back(make_pair(v.second, v.first));
        mcost += G[v.first][v.second];
        for (int i = (1); i < (m + 1); i++)
          if (i != v.second && !taken[i]) {
            q.push(make_pair(v.second, i));
          }
      }
    }
    cout << mcost << endl;
    for (typeof((r).begin()) it = (r).begin(); it != (r).end(); ++it)
      cout << it->first << ' ' << it->second << endl;
    r.clear();
  }
  return 0;
}
