#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
const int inf = (int)1e9 + 9;
const int dx[] = {0, 1, -1, 0};
const int dy[] = {1, 0, 0, -1};
int a[maxn][maxn], id[maxn][maxn], w, h, Num;
int opt[maxn][maxn][1 << 7];
pair<int, int> cache[maxn][maxn][1 << 7];
int type[maxn][maxn][1 << 7];
int imx[20], imy[20];
inline int update(int &a, int value) {
  if (value < a) {
    a = value;
    return 1;
  }
  return 0;
}
bool inq[maxn][maxn];
vector<string> mat;
void dfs(int x, int y, int stat) {
  mat[x - 1][y - 1] = 'X';
  if (id[x][y] != -1 && stat == (1 << id[x][y])) {
    return;
  }
  if (type[x][y][stat] == 1) {
    dfs(x, y, cache[x][y][stat].first);
    dfs(x, y, cache[x][y][stat].second);
  } else {
    dfs(cache[x][y][stat].first, cache[x][y][stat].second, stat);
  }
}
int main() {
  cin >> h >> w >> Num;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> a[i][j];
    }
  }
  memset(id, -1, sizeof id);
  for (int i = 0; i < Num; ++i) {
    cin >> imx[i] >> imy[i];
    id[imx[i]][imy[i]] = i;
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      for (int stat = 0; stat < 1 << Num; ++stat) {
        opt[i][j][stat] = inf;
      }
      if (id[i][j] != -1) {
        opt[i][j][1 << id[i][j]] = a[i][j];
      }
    }
  }
  for (int stat = 1; stat < 1 << Num; ++stat) {
    for (int set = stat; set > 0; set = ((set - 1) & stat)) {
      int tmp = stat ^ set;
      for (int i = 1; i <= h; ++i) {
        for (int j = 1; j <= w; ++j) {
          if (update(opt[i][j][stat],
                     opt[i][j][set] + opt[i][j][tmp] - a[i][j])) {
            type[i][j][stat] = 1;
            cache[i][j][stat] = make_pair(set, tmp);
          }
        }
      }
    }
    queue<int> q;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        inq[i][j] = 0;
        if (opt[i][j][stat] < inf) {
          q.push(i);
          q.push(j);
          inq[i][j] = 1;
        }
      }
    }
    for (; !q.empty();) {
      int x = q.front();
      q.pop();
      int y = q.front();
      q.pop();
      inq[x][y] = 0;
      for (int k = 0; k < 4; ++k) {
        int nx = x + dx[k], ny = y + dy[k];
        if (nx >= 1 && ny >= 1 && nx <= h && ny <= w) {
          if (update(opt[nx][ny][stat], opt[x][y][stat] + a[nx][ny])) {
            type[nx][ny][stat] = 2;
            cache[nx][ny][stat] = make_pair(x, y);
            if (!inq[nx][ny]) {
              inq[nx][ny] = 1;
              q.push(nx);
              q.push(ny);
            }
          }
        }
      }
    }
  }
  int res = inf, x, y;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (update(res, opt[i][j][(1 << Num) - 1])) {
        x = i;
        y = j;
      }
    }
  }
  cout << res << endl;
  mat.resize(h, string(w, '.'));
  dfs(x, y, (1 << Num) - 1);
  for (int i = 0; i < h; ++i) {
    cout << mat[i] << endl;
  }
}
