codeparrot/codecomplex huggingface dataset:
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;


public class Main {
	static int n, m, k;
	static int inf = (int) 1e9;
	static class Pair {
		int x, y;
		Pair(int a, int b) {
			x = a; y = b;
		}
	}
	static int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
	static boolean valid(int x, int y) {
		return x >= 0 && x < n && y >= 0 && y < m;
	}
	static int[][] bfs(int[] xs, int[] ys) {
		int[][] dist = new int[n][m];
		for(int i = 0; i < n; i++)
			Arrays.fill(dist[i], inf);
		Queue<Pair> q = new LinkedList<>();
		for(int i = 0; i < k; i++) {
			dist[xs[i]][ys[i]] = 0;
			q.add(new Pair(xs[i], ys[i]));
		}

		while(!q.isEmpty()) {
			Pair p = q.remove();
			for(int d = 0; d < 4; d++) {
				int nx = p.x + dx[d], ny = p.y + dy[d];
				if(valid(nx, ny) && dist[nx][ny] == inf) {
					dist[nx][ny] = dist[p.x][p.y] + 1;
					q.add(new Pair(nx, ny));
				}
			}
		}

		return dist;
	}

	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner();
		int n = in.nextInt() ; 
		int m = in.nextInt(); 
		int k = in.nextInt(); 
		int x[] = new int[k] ; 
		int y[] = new int[k] ; 
		int trees [][] = new int [n][m] ;


		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				trees[i][j]=Integer.MAX_VALUE ;

		for (int i = 0; i < k; i++)
		{
			x[i]=in.nextInt()-1; 
			y[i]=in.nextInt()-1;
			trees[x[i]][y[i]]=0 ;
		}

		int dis = Integer.MIN_VALUE ; ;
		int xp=0; ;
		int yp=0;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				if(trees[i][j] != 0)
					for (int j2 = 0; j2 < k; j2++)
						trees[i][j]=Math.min(trees[i][j], Math.abs(i-x[j2])+Math.abs(j-y[j2]));

		for (int i = 0; i <n; i++)
			for (int j = 0; j < m; j++)
				if(trees[i][j] > dis)
				{
					dis=trees[i][j];
					xp=i+1;
					yp=j+1;
				}
		PrintWriter out = new PrintWriter("output.txt");
		out.printf("%d %d\n", xp ,yp);
		out.close();
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;
		Scanner() throws FileNotFoundException {
			br = new BufferedReader(new FileReader("input.txt"));
		}

		String next() throws IOException {
			while(st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
	}
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.awt.Point;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class sdffsdf {

	public static void main(String[] args) {
		try{
			File file = new File("input.txt");
			Scanner sc = new Scanner(file);
			String s = sc.nextLine();
			String[] seperatedd = s.split(" ");
			int x = Integer.parseInt(seperatedd[0]);
			int y = Integer.parseInt(seperatedd[1]);
			int[][] grid = new int[x][y];
			for(int i = 0; i < x; i++)
			{
				for(int j = 0; j < y; j++)
				{
					grid[i][j] = 0;
				}
			}
			s = sc.nextLine();
			int z = Integer.parseInt(s);
			LinkedList<Point> BFS = new LinkedList<Point>();
			s = sc.nextLine();
			String[] seperated = s.split(" ");
			for(int i = 0; i < seperated.length; i = i + 2)
			{
				Point temp = new Point();
				temp.x = Integer.parseInt(seperated[i])-1;
				temp.y = Integer.parseInt(seperated[i+1])-1;
				grid[temp.x][temp.y] = 1;
				BFS.addLast(temp);
			}
			while(!BFS.isEmpty())
			{
				Point temp = new Point();
				temp = BFS.removeFirst();
			 	int	k = temp.x;
			 	int l = temp.y;
				
				if(!(l+1 >= y || grid[k][l+1] == 1))
				{
					Point temp1 = new Point();
					temp1.x = k;
					temp1.y = l+1;
					grid[temp1.x][temp1.y] = 1;
					BFS.addLast(temp1);
				}
				if(!(k+1 >= x || grid[k+1][l] == 1))
				{
					Point temp1 = new Point();
					temp1.x = k+1;
					temp1.y = l;
					grid[temp1.x][temp1.y] = 1;
					BFS.addLast(temp1);
				}
				
								
				if(!(l-1 < 0 || grid[k][l-1] == 1))
				{
					Point temp1 = new Point();
					temp1.x = k;
					temp1.y = l-1;
					grid[temp1.x][temp1.y] = 1;
					BFS.addLast(temp1);
				}
				if(!(k-1 < 0 || grid[k-1][l] == 1))
				{
					Point temp1 = new Point();
					temp1.x = k-1;
					temp1.y = l;
					grid[temp1.x][temp1.y] = 1;
					BFS.addLast(temp1);
				}
				if(BFS.isEmpty())
				{
			try {
						File fil = new File("output.txt");
						PrintWriter out = new PrintWriter(fil);
						int v1 = (int)temp.getX() + 1;
						int v2 = (int)temp.getY() + 1;
						out.println(v1 + " " + v2); 
				        out.close();	
					}
					catch (Exception e) {}
				}
			}
		}
		catch (Exception e) {
			System.out.println("nbvnb");
		}

	}

}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.awt.Point;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.HashSet;

public class FireAgain {

	Point[] coordinate;
	Queue<Point> q = new LinkedList<>();
	// HashSet<Point> vis = new HashSet<>();
	boolean[][] vis;
	PrintStream out;
	int x, y;

	boolean distance(Point word1, Point word2) {

		if (Math.abs(word1.x - word2.x) == 1 && Math.abs(word1.y - word2.y) == 1)
			return false;

		if (Math.abs(word1.x - word2.x) == 1 && word1.y == word2.y)
			return true;

		if (word1.x == word2.x && Math.abs(word1.y - word2.y) == 1)
			return true;

		return false;
	}

	void bfs(Point s) {

		while (!q.isEmpty()) {
			s = q.poll();

			Point p = new Point();
			p.x = s.x - 1;
			p.y = s.y;

			if (p.x >= 1 && p.x <= x && p.y >= 1 && p.y <= y) {
				if (!vis[p.x][p.y]) {
					vis[p.x][p.y] = true;
					q.add(p);

				}
			}

			p = new Point();
			p.x = s.x + 1;
			p.y = s.y;

			if (p.x >= 1 && p.x <= x && p.y >= 1 && p.y <= y) {
				if (!vis[p.x][p.y]) {
					vis[p.x][p.y] = true;
					q.add(p);

				}
			}

			p = new Point();
			p.x = s.x;
			p.y = s.y - 1;

			if (p.x >= 1 && p.x <= x && p.y >= 1 && p.y <= y) {
				if (!vis[p.x][p.y]) {
					vis[p.x][p.y] = true;
					q.add(p);

				}
			}
			
			p = new Point () ;
			p.x = s.x ;
			p.y = s.y + 1;
			
			if (p.x >= 1 && p.x <= x && p.y >= 1 && p.y <= y) {
				if (!vis[p.x][p.y]) {
					vis[p.x][p.y] = true ;
					q.add(p);
					
				}
			}

			if (q.size() == 0)
				out.print(s.x + " " + s.y);
		}

	}

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub

		FireAgain F = new FireAgain();
		 Scanner in = new Scanner (new FileReader("input.txt"));
		 F.out = new PrintStream(new File("output.txt"));
	
		F.x = in.nextInt();
		F.y = in.nextInt();

		int l = 0;
		F.vis = new boolean[F.x + 1][F.y + 1];

		int k = in.nextInt();

		for (int i = 0; i < k; i++) {
			Point P = new Point(in.nextInt(), in.nextInt());
			F.vis[P.x][P.y] = true; // add in set
			F.q.add(P);
		}

		F.bfs(F.q.peek());

	}

}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.util.*;
import java.io.*;

public class C {
	public static void main(String[] args) throws Exception {
		final int fuck = 2001;

		Scanner in = new Scanner(new File("input.txt"));
		PrintWriter out = new PrintWriter(new File("output.txt"));
		int n = in.nextInt(), m = in.nextInt();
		int[] D = new int[ fuck*fuck ], Q = new int[ fuck*(fuck + 1) ],
			dx = new int[] { 1, -1, 0, 0},
			dy = new int[] { 0, 0, -1, 1};
		Arrays.fill(D, -1);
		int H = -1, T = 0;

		int k = in.nextInt(), ans = 0;
		for(int i = 0; i < k; ++i) {
			int x = in.nextInt(), y = in.nextInt();
			D[x * fuck + y] = 0;

			++H; H %= Q.length;
			ans = Q[H] = x * fuck + y;
		}
		
		while(H >= T) {
			int idx = Q[T++]; T %= Q.length;
			int x = idx / fuck, y = idx % fuck;
			for(int i = 0; i < 4; ++i) {
				int wtf = (dx[i] + x) * fuck + (dy[i] + y);
				if(dx[i] + x <= n && dx[i] + x >= 1 && dy[i] + y <= m && dy[i] + y >= 1 && D[wtf] == -1) {
					D[wtf] = D[idx] + 1;
					++H; H %= Q.length;
					Q[H] = wtf;

					if(D[wtf] >= D[ans])
						ans = wtf;
				}
			}
		}

		out.println((ans / fuck) + " " + (ans % fuck));
		out.close();
		in.close();
	}
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
//package c;
import java.util.*;
import java.io.*;

public class Main {

	int n,m;
	int d[][];
	Queue<int[]> q = new LinkedList<int[]>();
	int cur[];
	
	public void run() throws Exception{
		Scanner in = new Scanner(new File("input.txt"));
		PrintWriter out = new PrintWriter(new File("output.txt"));
		n = in.nextInt();
		m = in.nextInt();
		int k = in.nextInt();
		d = new int[n][m];
		for(int i=0;i<n;i++) Arrays.fill(d[i], Integer.MAX_VALUE/2);
		for(int i=0;i<k;i++){
			int x = in.nextInt()-1;
			int y = in.nextInt()-1;
			d[x][y] = 0;
			q.add(new int[]{x,y});
		}
		
		
		while(q.size() > 0){
			cur = q.poll();
			int x = cur[0];
			int y = cur[1];
			add(x, y+1);
			add(x+1, y);
			add(x-1, y);
			add(x, y-1);
		}
		int max = 0;
		int x = 0;
		int y = 0;
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if (max < d[i][j]){
					max = d[i][j];
					x = i;
					y = j;
				}
		out.println((x+1) + " " + (y+1));
		out.close();
	}
	
	private void add(int x, int y){
		if (x < 0 || y < 0) return;
		if (x >=n || y >=m) return;
		if (d[x][y] > d[cur[0]][cur[1]] + 1){
			d[x][y] = d[cur[0]][cur[1]] + 1;
			q.add(new int[]{x,y});
		}
	}
	
	public static void main(String[] args) throws Exception{
		new Main().run();
	}

}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Solution {

    BufferedReader in;
    StringTokenizer st;
    PrintWriter out;

    int n, m, k;
    int[] x, y;

    char[] qx = new char[4000000], qy = new char[4000000];
    int b, e;

    char[][] d;

    int[] dx = { -1, 0, 1, 0 }, dy = { 0, -1, 0, 1 };

    void bfs() {
        b = e = 0;
        for (int i = 0; i < d.length; i++) {
            Arrays.fill(d[i], (char)(1 << 14));
        }
        for (int i = 0; i < k; ++i) {
            qx[e] = (char) x[i];
            qy[e++] = (char) y[i];
            d[x[i]][y[i]] = 0;
        }
        for (; b < e; ++b) {
            int x = qx[b];
            int y = qy[b];
            for (int i = 0; i < 4; ++i) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && nx < n && ny >= 0 && ny < m)
                    if (d[nx][ny] > d[x][y] + 1) {
                        d[nx][ny] = (char) (d[x][y] + 1);
                        qx[e] = (char) nx;
                        qy[e++] = (char) ny;
                    }
            }
        }
    }

    void solve() throws IOException {
        n = ni();
        m = ni();
        k = ni();
        x = new int[k];
        y = new int[k];
        for (int i = 0; i < k; ++i) {
            x[i] = ni() - 1;
            y[i] = ni() - 1;
        }
        d = new char[n][m];
        bfs();
        int x = -1, y = -1, last = -1;
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                if (d[i][j] > last) {
                    last = d[i][j];
                    x = i;
                    y = j;
                }
        ++x;
        ++y;
        out.println(x + " " + y);
    }

    public Solution() throws IOException {
        Locale.setDefault(Locale.US);
        in = new BufferedReader(new FileReader("input.txt"));
        out = new PrintWriter("output.txt");
        solve();
        in.close();
        out.close();
    }

    String nline() throws IOException {
        return in.readLine();
    }

    String ns() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(nline());
        }
        return st.nextToken();
    }

    int ni() throws IOException {
        return Integer.valueOf(ns());
    }

    long nl() throws IOException {
        return Long.valueOf(ns());
    }

    double nd() throws IOException {
        return Double.valueOf(ns());
    }

    public static void main(String[] args) throws IOException {
        new Solution();
    }
}
// Problem: 35_C. Fire Again
// Time complexity: cubic

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.Scanner;


public class P35C {
    int n, m;
    int [][]fire;
    public P35C() throws FileNotFoundException {
        Scanner in = new Scanner(new FileReader("input.txt"));
        n = in.nextInt();
        m = in.nextInt();
        int k = in.nextInt();
        fire = new int[k][2];
        for (int i = 0; i < k; i++){
            fire[i][0] = in.nextInt();
            fire[i][1] = in.nextInt();
        }
        in.close();
        
        int []last = new int[2];
        int lastBurn = -1;
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                int burn = Integer.MAX_VALUE;
                for (int l = 0; l < k; l++){
                    int burnAux = dist(i, j, fire[l][0], fire[l][1]);
                    burn = Math.min(burn, burnAux);
                }
                if(burn >= lastBurn){
                    lastBurn = burn;
                    last[0] = i;
                    last[1] = j;
                    
            }
            }
        }
        

        PrintStream out = new java.io.PrintStream( "output.txt" );
        out.print(last[0] + " " + last[1]);
        out.close();
    }
    
     int dist(int x1, int y1, int x2, int y2){
         return Math.abs(x2 - x1) + Math.abs(y2 - y1);
     }
    
    public static void main (String []args) throws FileNotFoundException{
        new P35C();
    }
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
/**
 * Created by IntelliJ IDEA.
 * User: Taras_Brzezinsky
 * Date: 9/16/11
 * Time: 1:27 PM
 * To change this template use File | Settings | File Templates.
 */



import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.StringTokenizer;

public class TaskC extends Thread {
    public TaskC() {
        try {
            this.input = new BufferedReader(new FileReader("input.txt"));
            this.output = new PrintWriter("output.txt");
            this.setPriority(Thread.MAX_PRIORITY);
        } catch (Throwable e) {
            System.exit(666);
        }
    }

    private void solve() throws Throwable {
        int n = nextInt();
        int m = nextInt();
        int k = nextInt();
        Queue<Integer> qX = new ArrayDeque<Integer>();
        Queue<Integer> qY = new ArrayDeque<Integer>();
        boolean [][]was = new boolean[n][m];
        for (int i = 0; i < k; ++i) {
            int x = nextInt() - 1, y = nextInt() - 1;
            qX.add(x);
            qY.add(y);
            was[x][y] = true;
        }
        int lastX = -1, lastY = -1;
        while (!qX.isEmpty()) {
            lastX = qX.poll();
            lastY = qY.poll();
            for (int i = 0; i < dx.length; ++i) {
                int nextX = lastX + dx[i], nextY = lastY + dy[i];
                if (nextX < n && nextY < m && nextX >= 0 && nextY >= 0 && !was[nextX][nextY]) {
                    qX.add(nextX);
                    qY.add(nextY);
                    was[nextX][nextY] = true;
                }
            }
        }
        ++lastX;
        ++lastY;
        output.println(lastX + " " + lastY);



    }

    public void run() {
        try {
            solve();
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            System.exit(666);
        } finally {
            output.flush();
            output.close();
        }
    }

    public static void main(String[] args) {
        new TaskC().start();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    private double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    private String nextToken() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine());
        }
        return tokens.nextToken();
    }
    static final int PRIME = 3119;
    static final int[]dx = {1, -1, 0, 0}, dy = {0, 0, -1, 1};
    private BufferedReader input;
    private PrintWriter output;
    private StringTokenizer tokens = null;
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scn = new Scanner(new File("input.txt"));
        BufferedWriter out = new BufferedWriter(new FileWriter("output.txt")); 
        
        int r = scn.nextInt();
        int c = scn.nextInt();
        
        int[][] a = new int[r][c];
        
        for(int[] i: a)
            Arrays.fill(i, 1<<30);
        
        int k = scn.nextInt();
            
        Queue<State> q = new LinkedList<State>();
        for(int l = 0; l < k; l++){
            int i = scn.nextInt()-1;
            int j = scn.nextInt()-1;
        
            a[i][j] = 0;
            q.add(new State(i, j, 0));
        }
        
        while(!q.isEmpty()){
            State st = q.poll();
            
            a[st.i][st.j] = st.c;
            
            for(int d = 0; d < 4; d++){
                int ii = st.i + di[d];
                int jj = st.j + dj[d];
                
                if(ii < 0 || ii >= r || jj < 0 || jj >= c)continue;
                if(a[ii][jj] != 1 << 30)continue;
                
                a[ii][jj] = st.c+1;
                q.add(new State(ii, jj, st.c+1));
            }
        }
        
        int max = 0;
        for(int i = 0; i < r; i++)
            for(int j = 0; j < c; j++)
                max = Math.max(max, a[i][j]);
        
        for(int i = 0; i < r; i++)
            for(int j = 0; j < c; j++)
                if(a[i][j] == max){
//                  System.out.println(i+1 + " " + (j+1));
                    out.write((i+1)+" "+(j+1));
                    out.newLine();
                    
                    out.close();
                    
                    return;
                }
        
    }
    static int[] di = {0, 0, -1, 1};
    static int[] dj = {1, -1, 0, 0};
}

class State{
    int i, j, c;
    public State(int ii, int ji, int ci){
        i = ii;
        j = ji;
        c = ci;
    }
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.*;
import java.util.*;
public class CF_35C {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new FileReader("input.txt"));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));

        StringTokenizer st1 = new StringTokenizer(f.readLine());
        int n = Integer.parseInt(st1.nextToken());
        int m = Integer.parseInt(st1.nextToken());

        boolean[][] visited = new boolean[n][m];

        int k = Integer.parseInt(f.readLine());
        LinkedList<state1> ll = new LinkedList<state1>();
        StringTokenizer st = new StringTokenizer(f.readLine());
        for(int i = 0; i < k; i++) {
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());

            ll.add(new state1(x - 1, y - 1));
            visited[x - 1][y - 1] = true;
        }

        int lastx = 1;
        int lasty = 1;
        while(!ll.isEmpty()) {
            state1 focus = ll.remove();
            lastx = focus.x+1;
            lasty = focus.y+1;

            //System.out.println(lastx + " " + lasty);

            visited[focus.x][focus.y] = true;
            if(focus.x+1 < n && !visited[focus.x+1][focus.y]) {
                ll.add(new state1(focus.x+1, focus.y));
                visited[focus.x+1][focus.y] = true;
            }
            if(focus.x-1 >= 0 && !visited[focus.x-1][focus.y]) {
                ll.add(new state1(focus.x-1, focus.y));
                visited[focus.x-1][focus.y] = true;
            }
            if(focus.y+1 < m && !visited[focus.x][focus.y+1]) {
                ll.add(new state1(focus.x, focus.y+1));
                visited[focus.x][focus.y+1] = true;
            }
            if(focus.y-1 >= 0 && !visited[focus.x][focus.y-1]) {
                ll.add(new state1(focus.x, focus.y-1));
                visited[focus.x][focus.y-1] = true;
            }
        }
        out.println(lastx + " " + lasty);
        out.close();
    }
}

class state1 {
    int x, y;
    state1(int x, int y) {
        this.x = x; this.y = y;
    }
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


public class ProblemD {

    static int n;
    static int m;
    static boolean[][] fire;
    
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("input.txt"));
        n = sc.nextInt();
        m = sc.nextInt();
        int k = sc.nextInt();
        fire = new boolean[n][m];
        Queue<Pos> q = new LinkedList<Pos>();
        for (int i = 0; i < k; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            q.add(new Pos(x - 1, y - 1));
            fire[x - 1][y - 1] = true;
        }
        int[] di = new int[] { 1, -1, 0, 0 };
        int[] dj = new int[] { 0, 0, 1, -1};
        Pos last = null;
        while (q.size() > 0) {
            Pos pos = q.poll();
            last = pos;
            for (int kk = 0; kk < 4; kk++) {
                int ni = pos.i + di[kk];
                int nj = pos.j + dj[kk];
                if (ni >= 0 && nj >= 0 && ni < n && nj < m) {
                    if (!fire[ni][nj]) {
                        fire[ni][nj] = true;
                        q.add(new Pos(ni, nj));
                    }
                }
            }
        }
        PrintWriter out = new PrintWriter(new File("output.txt"));
        out.println((last.i + 1) + " " + (last.j + 1));
        out.flush();
        out.close();
    }
    

}

class Pos {
    
    int i, j;

    public Pos(int i, int j) {
        super();
        this.i = i;
        this.j = j;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + i;
        result = prime * result + j;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Pos other = (Pos) obj;
        if (i != other.i)
            return false;
        if (j != other.j)
            return false;
        return true;
    }
    
    
};