import java.io.*;
import java.util.*;

public class Environment {
  public static final char EMPTY = '.';
  public static final char WATER = '~';
  public static final char WALL = '#';
  public static final char BASE = '@';
  public static final char OCCUPIED = '*';
  public static final char DROPPED = '!';
  
  private int width;
  private int height;
  private int playerId;
  
  private char board[][];
  private HashMap robots;

  public Environment(BufferedReader in, PrintWriter out) throws IOException {
    StringTokenizer st;
    int i, j;

    robots = new HashMap();
    
    st = new StringTokenizer(in.readLine());
    width = Integer.parseInt(st.nextToken());
    height = Integer.parseInt(st.nextToken());
    System.out.println("" + width + " " + height);
    board = new char[width][height];
    char bar[];
    for (i = 0; i < height; i++) {
      bar = in.readLine().toCharArray();
      for (j = 0; j < width; j++) {
        board[j][i] = bar[j];
      }
    }
    st = new StringTokenizer(in.readLine());
    playerId = Integer.parseInt(st.nextToken());
    Player player = new Player(playerId, Integer.parseInt(st.nextToken()),
      Integer.parseInt(st.nextToken()), this);
    robots.put(new Integer(playerId), player);
    String reply, command;
    while ((reply = in.readLine()) != null) {
      System.out.println(reply);
      processReply(reply);
      command = player.move(in.readLine());
      System.out.println(command);
      out.println(command);
    }
  }
  
  public ArrayList listType(char type) {
    ArrayList result = new ArrayList();
    int i, j;
    for (i = 0; i < width; i++) {
      for (j = 0; j < height; j++) {
        if (board[i][j] == type)
	  result.add(new Node(i + 1, j + 1, this));
      }
    }
    return result;
  }

  public char getCondition(int x, int y) {
    if (x <= 0 || y <= 0 || x > width || y > height)
      return WALL;
    Iterator iter = robots.values().iterator();
    while (iter.hasNext()) {
      if (((Robot) iter.next()).positionEquals(x, y))
        return OCCUPIED;
    }
    return board[x - 1][y - 1];
  }

  public void setCondition(int x, int y, char condition) {
    board[x - 1][y - 1] = condition;
  }

  private void processReply(String reply) {
    StringTokenizer st = new StringTokenizer(reply);
    int id;
    String token;
    Robot current = null;
    while (st.hasMoreTokens()) {
      token = st.nextToken();
      if (token.startsWith("#")) {
        id = Integer.parseInt(token.substring(1));
	if (robots.get(new Integer(id)) == null)
	  robots.put(new Integer(id), new Robot(id, this));
	current = (Robot) robots.get(new Integer(id));
      } else if ("N".equals(token)) {
        current.moveNorth();
      } else if ("E".equals(token)) {
        current.moveEast();
      } else if ("S".equals(token)) {
        current.moveSouth();
      } else if ("W".equals(token)) {
        current.moveWest();
      } else if ("P".equals(token)) {
        current.pickUp(new Package(Integer.parseInt(st.nextToken()),
	  current.getX(), current.getY(), this));
      } else if ("D".equals(token)) {
	current.drop(Integer.parseInt(st.nextToken()));
      } else if ("X".equals(token)) {
        current.setX(Integer.parseInt(st.nextToken()));
      } else if ("Y".equals(token)) {
        current.setY(Integer.parseInt(st.nextToken()));
      }
    }
  }
}
