Загрузка данных


import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // Пропускаем первую строку с числом n, так как длина есть у самой строки команд
        br.readLine(); 
        String s = br.readLine();
        if (s == null) return;

        int r = 0, c = 0;
        int minR = 0, maxR = 0, minC = 0, maxC = 0;

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == 'U') r--;
            else if (ch == 'D') r++;
            else if (ch == 'L') c--;
            else if (ch == 'R') c++;

            if (r < minR) minR = r;
            if (r > maxR) maxR = r;
            if (c < minC) minC = c;
            if (c > maxC) maxC = c;
        }

        System.out.println((maxR - minR + 1) + " " + (maxC - minC + 1));
    }
}