#include <bits/stdc++.h>
using namespace std;
int n, m, k, x[105][105], d[130][105][105], cnt[130], ans = INT_MAX, ta, tb, tc;
priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >,
               greater<pair<int, pair<int, int> > > >
    q[130];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool u[105][105];
void go(int a, int b, int c) {
  u[b][c] = 1;
  if (d[a][b][c] == x[b][c]) return;
  for (int i = 1; i * 2 < a; i++)
    if ((i & a) == i && d[i][b][c] + d[a - i][b][c] - x[b][c] == d[a][b][c]) {
      go(i, b, c);
      go(a - i, b, c);
      return;
    }
  int td = d[a][b][c] - x[b][c];
  for (int i = 0; i < 4; i++) {
    b += dx[i], c += dy[i];
    if (b >= 0 && b < n && c >= 0 && c < m && d[a][b][c] == td) {
      go(a, b, c);
      return;
    }
    b -= dx[i], c -= dy[i];
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", x[i] + j);
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &ta, &tb);
    ta--, tb--;
    q[1 << i].push(make_pair(d[1 << i][ta][tb] = x[ta][tb], make_pair(ta, tb)));
  }
  for (int i = 1; i < (1 << k); i++)
    for (int j = 0; j < k; j++)
      if (i & (1 << j)) cnt[i]++;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j < (1 << k); j++) {
      if (cnt[j] != i) continue;
      for (int l = 1; l * 2 < j; l++)
        if ((l & j) == l)
          for (int ii = 0; ii < n; ii++)
            for (int jj = 0; jj < m; jj++)
              d[j][ii][jj] = min(d[j][ii][jj] ? d[j][ii][jj] : INT_MAX,
                                 d[l][ii][jj] + d[j - l][ii][jj] - x[ii][jj]);
      if (i == k) {
        for (int ii = 0; ii < n; ii++)
          for (int jj = 0; jj < m; jj++)
            if (d[j][ii][jj] && d[j][ii][jj] < ans)
              ans = d[j][ii][jj], ta = ii, tb = jj;
        go(j, ta, tb);
        continue;
      }
      if (i != 1)
        for (int ii = 0; ii < n; ii++)
          for (int jj = 0; jj < m; jj++)
            q[j].push(make_pair(d[j][ii][jj], make_pair(ii, jj)));
      while (!q[j].empty()) {
        ta = q[j].top().second.first, tb = q[j].top().second.second,
        tc = q[j].top().first;
        q[j].pop();
        if (d[j][ta][tb] != tc) continue;
        for (int l = 0; l < 4; l++) {
          ta += dx[l], tb += dy[l];
          if (ta >= 0 && ta < n && tb >= 0 && tb < m &&
              (!d[j][ta][tb] || tc + x[ta][tb] < d[j][ta][tb]))
            q[j].push(
                make_pair(d[j][ta][tb] = tc + x[ta][tb], make_pair(ta, tb)));
          ta -= dx[l], tb -= dy[l];
        }
      }
    }
  printf("%d\n", ans);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) printf("%c", u[i][j] ? 'X' : '.');
    printf("\n");
  }
  return 0;
}
