// Arup Guha
// 9/16/2026
// Exam #1 Review Practice Questions 

import java.util.*;

public class e1review {
	
	// Directions of movement allowed for adjacency.
	final public static int[] DR = {-1,-1,-1, 0,0, 1,1,1};
	final public static int[] DC = {-1, 0, 1,-1,1,-1,0,1};
	

	public static void main(String[] args) {
	/*
		System.out.println(count("mississippi", 's'));
		
		for (int i=1; i<100; i++) {
			double x = 1.0*i/5;
			System.out.println(mysqrt(x)+"\t"+Math.sqrt(x));
		}
		
		int[] myarr = {3, 1, 6, 2, 4, 5};
		System.out.println("num inversions = "+numInversions(myarr));
		
		pizza();*/
		
		char[][] mine = makeRndGrid(10, 12, 55);
		fillNumsAlt(mine);
		print(mine);
	}

	// Returns the number of times c occurs in str.
	public static int count(String str, char c) {
	
		// My counter.
		int res = 0;
		
		// Go through each string index.
		for (int i=0; i<str.length(); i++)
			
			// Add to counter if it's the right letter.
			if (str.charAt(i) == c)
				res++;
				
		// Ta da!
		return res;
	}
	
	// Returns the square root of x.
	public static double mysqrt(double x) {
		
		// Set low and high.
		double low = Math.min(1, x);
		double high = Math.max(1, x);
		
		for (int i=0; i<100; i++) {
			
			double mid = (low+high)/2;
			
			// Our guess is too small, increase low.
			if (mid*mid < x)
				low = mid;
			
			// Our guess is too big.
			else
				high = mid;
			
		}
		
		return low;
	}
	
	// Returns the number of inversions in arr.
	public static int numInversions(int[] arr) {
		
		int inversion = 0;
		
		// Go through all ordered pairs where i < j.
		for (int i=0; i<arr.length; i++) {
			for (int j=i+1; j<arr.length; j++) {
				
				// Count if it's inverted.
				if (arr[i] > arr[j])
					inversion++;
			}
		}
		
		return inversion;
		
	}
	
	// Pizza exercise.
	public static void pizza() {
		
		// Get # of types of pizza.
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		
		// REad in costs of each pizza.
		double cost[] = new double[n];
		for (int i=0; i<n; i++)
			cost[i] = stdin.nextDouble();
		
		// Read in orders and process.
		int numOrders = stdin.nextInt();
		for (int i=1; i<=numOrders; i++) {
			
			// Go through each quantity of pizza in the order and add
			// in the cost of each type of pizza.
			double total = 0;
			for (int j=0; j<n; j++) {
				int quantity = stdin.nextInt();
				total += cost[j]*quantity;
			}
			
			// Prints out whole total for this order.
			System.out.println("On day "+i+" we spent "+total);
		}
		
	}
	
	// Makes a minesweeper grid with rows number of rows and cols number of
	// columns with numbombs number of bombs.
	public static char[][] makeRndGrid(int rows, int cols, int numbombs) {
		
		Random rndObj = new Random();
		
		// Make grid.
		char[][] grid = new char[rows][cols];
		
		// Fill with underscores.
		for (int i=0; i<rows; i++)
			Arrays.fill(grid[i], '_');
		
		int cntBombs = 0;
		
		// Keep going until we get enough bombs.
		while (cntBombs < numbombs) {
			
			// Make random row and col.
			int myr = rndObj.nextInt(rows);
			int myc = rndObj.nextInt(cols);
			
			// Skip it, there's already a bomb.
			if (grid[myr][myc] == '*')
				continue;
			
			// Add bomb and update count.
			grid[myr][myc] = '*';
			cntBombs++;
		}
		
		// Here is our grid.
		return grid;
	}
	
	// Fills the numbers for all non-bomb squares in grid.
	public static void fillNums(char[][] grid) {
		
		// Go through each square.
		for (int i=0; i<grid.length; i++) {
			for (int j=0; j<grid[i].length; j++) {
				
				// No need to count, skip it.
				if (grid[i][j] == '*')
					continue;
				
				// Get adjacent bombs.
				int numBombs = countAdj(grid, i, j);
				
				// fill character.
				grid[i][j] = (char)('0' + numBombs);
			}
		}
	}
	
	// Returns # of adjacent bombs to grid[r][c].
	public static int countAdj(char[][] grid, int r, int c) {
		
		// Go in each direction.
		int res = 0;
		for (int i=0; i<DR.length; i++) {
			
			// Adjacent location in direction i from (r,c).
			int nR = r + DR[i];
			int nC = c + DC[i];
			
			// Out of bounds.
			if (nR<0 || nR>=grid.length || nC<0 || nC>=grid[0].length)
				continue;
			
			// Count it!
			if (grid[nR][nC] == '*')
				res++;
		}
		
		// Here is our answer.
		return res;
	}
	
	// Alternate idea suggested by a student.
	public static void fillNumsAlt(char[][] grid) {
		
		// Update all counts.
		for (int i=0; i<grid.length; i++)
			for (int j=0; j<grid[i].length; j++)
				if (grid[i][j] == '_')
					grid[i][j] = '0';
		
		// Now, go through each square.
		for (int i=0; i<grid.length; i++) {
			for (int j=0; j<grid[i].length; j++) {
				
				// No need to count, skip it.
				if (grid[i][j] != '*')
					continue;
				
				// Loop adjacent to this bomb.
				for (int k=0; k<DR.length; k++) {
					int nR = i + DR[k];
					int nC = j + DC[k];
					
					// This goes out of bounds.
					if (nR<0 || nR>=grid.length || nC<0 || nC>=grid[0].length)
						continue;

					// We don't want to mess it up if it's a bomb, but if it's
					// not, add 1 to the character.
					if (grid[nR][nC] != '*')
						grid[nR][nC]++;
				}
			}
		}
	}
	
	// For testing.
	public static void print(char[][] grid) {
		for (int i=0; i<grid.length; i++)
			System.out.println(new String(grid[i]));
	}

}