import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main { public static int[][] dirs = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; &nbs...