#include <bits/stdc++.h>
using namespace std;
const int DX[8] = {1, -1, 0, 0, 1, 1, -1, -1};
const int DY[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const int intmax = 0x7fffffff;
int mp[105][105], n, m, nk;
int d[105][105][1 << 7];
pair<int, int> fa1[105][105][1 << 7], fa2[105][105][1 << 7];
char f[105][105];
bool vis[105][105];
struct node {
  int x, y;
  node(int a, int b) : x(a), y(b) {}
};
queue<node> qu;
inline int pow2(int n) { return 1 << n; }
inline int get2(int n, int p) { return (1 << p) & n; }
void print(int x, int y, int k) {
  f[x][y] = 'X';
  if (fa1[x][y][k].first == -1)
    if (fa2[x][y][k].second == -1)
      return;
    else {
      print(x, y, fa2[x][y][k].first);
      print(x, y, fa2[x][y][k].second);
    }
  else
    print(fa1[x][y][k].first, fa1[x][y][k].second, k);
}
int main() {
  scanf("%d%d%d", &n, &m, &nk);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &mp[i][j]);
  memset(d, 0x3f, sizeof(d));
  for (int i = 0; i < nk; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    d[a - 1][b - 1][pow2(i)] = mp[a - 1][b - 1];
    fa1[a - 1][b - 1][pow2(i)] = make_pair(-1, -1);
    fa2[a - 1][b - 1][pow2(i)] = make_pair(-1, -1);
  }
  for (int i = 1; i < pow2(nk); i++) {
    for (int j = 0; j < n; j++)
      for (int k = 0; k < m; k++)
        for (int l = (i - 1) & i; l; l = (l - 1) & i) {
          d[j][k][i] = min(d[j][k][i], d[j][k][l ^ i] + d[j][k][l] - mp[j][k]);
          if (d[j][k][i] == d[j][k][l ^ i] + d[j][k][l] - mp[j][k]) {
            fa1[j][k][i] = make_pair(-1, -1);
            fa2[j][k][i] = make_pair(l ^ i, l);
          }
        }
    for (int j = 0; j < n; j++)
      for (int k = 0; k < m; k++) qu.push(node(j, k));
    memset(vis, 0, sizeof(vis));
    while (!qu.empty()) {
      node tmp = qu.front();
      qu.pop();
      int x = tmp.x;
      int y = tmp.y;
      int dis = d[x][y][i];
      vis[x][y] = 0;
      for (int di = 0; di < 4; di++) {
        int nx = x + DX[di];
        int ny = y + DY[di];
        if (nx < n && nx >= 0 && ny < m && ny >= 0) {
          if (dis + mp[nx][ny] < d[nx][ny][i]) {
            d[nx][ny][i] = dis + mp[nx][ny];
            fa1[nx][ny][i] = make_pair(x, y);
            fa2[nx][ny][i] = make_pair(-1, -1);
            if (!vis[nx][ny]) {
              qu.push(node(nx, ny));
              vis[nx][ny] = 1;
            }
          }
        }
      }
    }
  }
  int ans = intmax;
  int ansx, ansy;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      ans = min(ans, d[i][j][pow2(nk) - 1]), f[i][j] = '.';
      if (ans == d[i][j][pow2(nk) - 1]) ansx = i, ansy = j;
    }
  printf("%d\n", ans);
  print(ansx, ansy, pow2(nk) - 1);
  for (int i = 0; i < n; i++) puts(f[i]);
}
