// Arup Guha
// 4/9/2024
// Solution to FHSPS Problem: Twelve Years a Slave
// Shown to COP 3503 to illustrate use of Floyd-Warshall's Algorithm
// Problem Description: https://www.cs.ucf.edu/~dmarino/progcontests/mysols/highschool/fhsps/2014/probset.pdf (Problem G)

import java.util.*;

public class twelve {

	final public static int MOVIETIME = 134;

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		int[][] d = new int[n][n];
		
		// Read in adjacency matrix.
		for (int i=0; i<n; i++)
			for (int j=0; j<n; j++)
				d[i][j] = stdin.nextInt();
				
		// Run Floyd-Warshall's.
		for (int k=0; k<n; k++)
			for (int i=0; i<n; i++)
				for (int j=0; j<n; j++)
					d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]);
					
		// Get the queries.
		int numQ = stdin.nextInt();
		
		// Process queries.
		for (int i=0; i<numQ; i++) {
		
			// Get query.
			int s = stdin.nextInt();
			int e = stdin.nextInt();
			int maxT = stdin.nextInt();
		
			int res = 0;
			
			// Try each movie theater.
			for (int j=1; j<n; j+=2) 
				if (d[s][j]+d[j][e] + MOVIETIME <= maxT)
					res++;
			
			// Ta da!
			System.out.println(res);
		}	
	}
}