I solved the problem with DFS using recursion, stack and stack with pruning.
Main part
import java.util.Scanner;
import java.util.Stack;
public class ABC015C {
static int n, k;
static int[][] field;
static boolean[][] visited;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
n = sc …