// Arup Guha
// 3/21/2017
// Solution to 2017 March USACO Bronze Problem: Modern Art
// Just edited Platinum solution to change how input is read.

import java.util.*;
import java.io.*;

public class art_bronze {

	public static int n;
	public static int[][] grid;

	public static int[] minX;
	public static int[] minY;
	public static int[] maxX;
	public static int[] maxY;

	public static void main(String[] args) throws Exception {

		// Read in data.
		BufferedReader stdin = new BufferedReader(new FileReader("art.in"));
		n = Integer.parseInt(stdin.readLine().trim());

		// Store the grid and the min and max x and y's for each number.
		grid = new int[n][n];
		minX = new int[n*n+1];
		maxX = new int[n*n+1];
		minY = new int[n*n+1];
		maxY = new int[n*n+1];
		Arrays.fill(minX, n*n+1);
		Arrays.fill(minY, n*n+1);
		Arrays.fill(maxX, -1);
		Arrays.fill(maxY, -1);

		// Just to see # of unique values showing on the grid (not counting 0).
		HashSet<Integer> showing = new HashSet<Integer>();

		// See what we might count.
		boolean[] count = new boolean[n*n+1];

		// Read in everything.
		for (int i=0; i<n; i++) {
			String line = stdin.readLine();
			for (int j=0; j<n; j++) {
				grid[i][j] = line.charAt(j) - '0';
				minX[grid[i][j]] = Math.min(minX[grid[i][j]], i);
				minY[grid[i][j]] = Math.min(minY[grid[i][j]], j);
				maxX[grid[i][j]] = Math.max(maxX[grid[i][j]], i);
				maxY[grid[i][j]] = Math.max(maxY[grid[i][j]], j);
				if (grid[i][j] != 0) {
					showing.add(grid[i][j]);
					count[grid[i][j]] = true;
				}
			}
		}

		// Try each number.
		for (int i=1; i<=n*n; i++) {

			// Go through its bounding box.
			for (int a=minX[i]; a<=maxX[i]; a++) {
				for (int b=minY[i]; b<=maxY[i]; b++) {

					// If some number is IN this bounding box, that number couldn't have been first.
					if (grid[a][b] != i) {
						count[grid[a][b]] = false;
					}
				}
			}
		}

		// Now count them up.
		int res = 0;
		for (int i=1; i<=n*n; i++)
			if (count[i])
				res++;

		// Write result.
		PrintWriter out = new PrintWriter(new FileWriter("art.out"));
		out.println(res);
		out.close();
		stdin.close();
	}
}