#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100, MAX_K = 7, INF = 0x3F3F3F3F;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int n, m, cnt, bd;
int maz[MAX_N][MAX_N], st[MAX_N][MAX_N];
int F[MAX_N][MAX_N][(1 << (MAX_K))];
int pre[MAX_N][MAX_N][(1 << (MAX_K))];
bool inq[MAX_N][MAX_N][(1 << (MAX_K))], vis[MAX_N][MAX_N];
queue<int> Q;
void DFS(int x, int y, int bm) {
  vis[x][y] = 1;
  if (!pre[x][y][bm]) return;
  int tx = pre[x][y][bm] >> 24, ty = (pre[x][y][bm] >> 12) & ((1 << (12)) - 1),
      tbm = pre[x][y][bm] & ((1 << (12)) - 1);
  DFS(tx, ty, tbm);
  if (x == tx && y == ty) DFS(tx, ty, (bm - tbm) | st[x][y]);
}
int main() {
  cin >> n >> m >> cnt;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      cin >> maz[i][j];
    }
  for (int i = 0, x, y; i < cnt; ++i) {
    cin >> x >> y;
    --x;
    --y;
    st[x][y] = (1 << (i));
    bd += maz[x][y];
    maz[x][y] = 0;
  }
  memset(F, 0x3F, sizeof F);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (st[i][j]) F[i][j][st[i][j]] = 0;
  for (int bm = 1; bm < (1 << (cnt)); ++bm) {
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        if (st[i][j] && !(bm & st[i][j])) continue;
        for (int sbm = (bm - 1) & bm; sbm; sbm = (sbm - 1) & bm) {
          int tmp = F[i][j][sbm | st[i][j]] + F[i][j][(bm - sbm) | st[i][j]] -
                    maz[i][j];
          if (tmp < F[i][j][bm])
            F[i][j][bm] = tmp,
            pre[i][j][bm] = (((i) << 24) + ((j) << 12) + ((sbm | st[i][j])));
        }
        if (F[i][j][bm] < INF)
          Q.push((((i) << 24) + ((j) << 12) + (bm))), inq[i][j][bm] = 1;
      }
    while (!Q.empty()) {
      int x = Q.front() >> 24;
      int y = (Q.front() >> 12) & ((1 << (12)) - 1);
      int bm = Q.front() & ((1 << (12)) - 1);
      Q.pop();
      inq[x][y][bm] = 0;
      for (int i = 0; i < 4; ++i) {
        int tx = x + dx[i], ty = y + dy[i];
        if (tx >= n || ty >= m || tx < 0 || ty < 0) continue;
        int tbm = bm | st[tx][ty];
        if (F[x][y][bm] + maz[tx][ty] < F[tx][ty][tbm]) {
          F[tx][ty][tbm] = F[x][y][bm] + maz[tx][ty];
          pre[tx][ty][tbm] = (((x) << 24) + ((y) << 12) + (bm));
          if (!inq[tx][ty][tbm] && bm == tbm)
            Q.push((((tx) << 24) + ((ty) << 12) + (tbm))), inq[tx][ty][tbm] = 1;
        }
      }
    }
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (st[i][j]) {
        cout << F[i][j][(1 << (cnt)) - 1] + bd << endl;
        DFS(i, j, (1 << (cnt)) - 1);
        for (int x = 0; x < n; ++x) {
          for (int y = 0; y < m; ++y) {
            if (st[x][y] || vis[x][y])
              putchar('X');
            else
              putchar('.');
          }
          putchar('\n');
        }
        return 0;
      }
  return 0;
}
