#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

vector <string> piece, grid;
vector <string> templates[28];
vector <int> x, y;
vector <string> use;
int ans;

bool in(int c, int a, int b) {
	return(c >= a && c <= b ? true : false);
}

void flood(int a, int b) {
	int i, j;
	int k;
	x.push_back(a);
	y.push_back(b);
	for (i = -1; i < 2; i++) {
		for (j = -1; j < 2; j++) {
			if (i * j != 0)
				continue;
			if (i == 0 && j == 0)
				continue;
			if (in(a + i, 0, 9) && in(b + j, 0, 9)) {
				if (use[a + i][b + j] == '#') {
					bool durrrr = true;
					for (k = 0; k < x.size(); k++) {
						if (x[k] == a + i && y[k] == b + j)
							durrrr = false;
					}
					if (durrrr == true)
						flood(a + i, b + j);	
				}
			}
		}
	}
	return;
}

string shift(string blah, int a) {
	int i, j;
	while (blah.size() < 10)
		blah += '.';
	for (i = 0; i < a; i++) {
		for (j = blah.size() - 1; j > 0; j--) {
			blah[j] = blah[j - 1];
		}
		blah[0] = '.';
	}
	return(blah);
}

void go(int a) {
	int pos, k, i, j, count;
	bool lineempty = true, skip;
	bool filled;
	for (k = 0; k < 10; k++) {
		lineempty = true;
		use.clear();
		count = 0;
		for (i = 0; i < 4; i++) {
			use.push_back(shift(templates[a][i], k));
		}
		for (i = 0; i < 6; i++) {
			use.push_back(grid[i]);
		}

		for (i = use.size() - 1; i > -1; i--) {
			skip = false;
			for (j = 0; j < 10; j++) {
				if (use[i][j] == '#') {
					skip = true;
					break;
				}
			}
			if (skip == true)
				continue;
			pos = i;
			break;
		}
		while (lineempty == true) {
			for (i = pos; i > 0; i--) {
				for (j = 0; j < 10; j++) {
					use[i][j] = use[i - 1][j];
				}
			}
			for (i = 0; i < 10; i++) {
				use[0][i] = '.';
			}

			lineempty = true;
			for (i = 0; i < 10; i++) {
				if (use[pos - 1][i] == '#') {
					lineempty = false;
					break;
				}
			}
		}
		
		x.clear();
		y.clear();
		for (i = 0; i < 10; i++) {
			if (use[pos - 1][i] == '#') {
				flood(pos - 1, i);
				break;
			}
		}

		for (i = pos; i > -1; i--) {
			for (j = 0; j < 10; j++) {
				use[i][j] = '.';
			}
		}

		bool done = false;
		while (!done) {
			for (i = 0; i < x.size(); i++) {
				if (in(x[i] + 1, 0, 9)) {
					if (use[x[i] + 1][y[i]] == '#') 
						done = true;
				}
				else
					done = true;
			}
			if (done)
				break;
			for (i = 0; i < x.size(); i++) {
				x[i]++;
			}
		}

		for (i = 0; i < x.size(); i++) {
			use[x[i]][y[i]] = '#';
		}

	
	/*	for (i = 0; i < 10; i++) {
			for (j = 0; j < 10; j++) {
				cout << use[i][j];
			}
			cout << endl;
		}
*/
		for (i = 0; i < 10; i++) {
			filled = true;
			for (j = 0; j < 10; j++) {
				if (use[i][j] == '.') {
					filled = false;
					break;
				}
			}
			if (filled == true)
				count++;
		}
		if (count > ans)
			ans = count;

	}
	return;
}
				



void def() {
	bool lineempty= true;
	int i, j;
	for (i = 0; i < 4; i++) {
		if (piece[0][i] == '#') {
			lineempty = false;
		}
	}
	while (lineempty == true) {
		for (i = 0; i < 3; i++) {
			for (j = 0; j < 4; j++) {
				piece[i][j] = piece[i + 1][j];
			}
		}
		for (i = 0; i < 4; i++) {
			piece[3][i] = '.';
		}
		for (i = 0; i < 4; i++) {
			if (piece[0][i] == '#') {
				lineempty = false;
			}
		}
	}
	lineempty = true;
	for (i = 0; i < 4; i++) {
		if (piece[i][0] == '#') {
			lineempty = false;
		}
	}
	while (lineempty == true) {
		for (i = 0; i < 4; i++) {
			for (j = 0; j < 3; j++) {
				piece[i][j] = piece[i][j + 1];
			}
		}
		for (i = 0; i < 4; i++) {
			piece[i][3] = '.';
		}
		for (i = 0; i < 4; i++) {
			if (piece[i][0] == '#') {
				lineempty = false;
			}
		}
	}
	return;
}

bool equal(vector <string> a, vector <string> b) {
	int i, j;
	for (i = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			if (a[i][j] != b[i][j])
				return(false);
		}
	}
	return(true);
}

int main() {
	ifstream filein("DATA5.txt");
	ofstream fileout("OUT5.txt");

	int i, j, left;
	int pos;
	string temps;

	for (i = 0; i < 28; i++) {
		for (j = 0; j < 4; j++) {
			templates[i].push_back("....");
		}
	}

	templates[0][0] = "####";
	templates[0][1] = "....";
	templates[0][2] = "....";
	templates[0][3] = "....";

	templates[1][0] = "#...";
	templates[1][1] = "#...";
	templates[1][2] = "#...";
	templates[1][3] = "#...";

	templates[2][0] = "####";
	templates[2][1] = "....";
	templates[2][2] = "....";
	templates[2][3] = "....";

	templates[3][0] = "#...";
	templates[3][1] = "#...";
	templates[3][2] = "#...";
	templates[3][3] = "#...";

	templates[4][0] = "###.";
	templates[4][1] = "#...";
	templates[4][2] = "....";
	templates[4][3] = "....";

	templates[5][0] = "#...";
	templates[5][1] = "#...";
	templates[5][2] = "##..";
	templates[5][3] = "....";
	
	templates[6][0] = "..#.";
	templates[6][1] = "###.";
	templates[6][2] = "....";
	templates[6][3] = "....";

	templates[7][0] = "##..";
	templates[7][1] = ".#..";
	templates[7][2] = ".#..";
	templates[7][3] = "....";

	templates[8][0] = "##..";
	templates[8][1] = ".##.";
	templates[8][2] = "....";
	templates[8][3] = "....";

	templates[9][0] = ".#..";
	templates[9][1] = "##..";
	templates[9][2] = "#...";
	templates[9][3] = "....";

	templates[10][0] = "##..";
	templates[10][1] = ".##.";
	templates[10][2] = "....";
	templates[10][3] = "....";

	templates[11][0] = ".#..";
	templates[11][1] = "##..";
	templates[11][2] = "#...";
	templates[11][3] = "....";

	templates[12][0] = "###.";
	templates[12][1] = ".#..";
	templates[12][2] = "....";
	templates[12][3] = "....";

	templates[13][0] = "#...";
	templates[13][1] = "##..";
	templates[13][2] = "#...";
	templates[13][3] = "....";

	templates[14][0] = ".#..";
	templates[14][1] = "###.";
	templates[14][2] = "....";
	templates[14][3] = "....";

	templates[15][0] = ".#..";
	templates[15][1] = "##..";
	templates[15][2] = ".#..";
	templates[15][3] = "....";

 templates[16][0] = "##..";
 templates[16][1] = "##..";
 templates[16][2] = "....";
 templates[16][3] = "....";

 templates[17][0] = "##..";
 templates[17][1] = "##..";
 templates[17][2] = "....";
 templates[17][3] = "....";

 templates[18][0] = "##..";
 templates[18][1] = "##..";
 templates[18][2] = "....";
 templates[18][3] = "....";

 templates[19][0] = "##..";
 templates[19][1] = "##..";
 templates[19][2] = "....";
 templates[19][3] = "....";

 templates[20][0] = ".##.";
 templates[20][1] = "##..";
 templates[20][2] = "....";
 templates[20][3] = "....";

 templates[21][0] = "#...";
 templates[21][1] = "##..";
 templates[21][2] = ".#..";
 templates[21][3] = "....";

 templates[22][0] = ".##.";
 templates[22][1] = "##..";
 templates[22][2] = "....";
 templates[22][3] = "....";

 templates[23][0] = "#...";
 templates[23][1] = "##..";
 templates[23][2] = ".#..";
 templates[23][3] = "....";

 templates[24][0] = "###.";
 templates[24][1] = "..#.";
 templates[24][2] = "....";
 templates[24][3] = "....";

 templates[25][0] = "##..";
 templates[25][1] = "#...";
 templates[25][2] = "#...";
 templates[25][3] = "....";

 templates[26][0] = "#...";
 templates[26][1] = "###.";
 templates[26][2] = "....";
 templates[26][3] = "....";

 templates[27][0] = ".#..";
 templates[27][1] = ".#..";
 templates[27][2] = "##..";
 templates[27][3] = "....";



/*
####

###
#

##
 ##

###
 #

##
##

 ##
##

###
  #
*/
	int loop = 5;
	while (loop--) {
		ans = 0;
		piece.clear();
		grid.clear();
		for (i = 0; i < 4; i++) {
			filein >> temps;
			piece.push_back(temps);
		}
		for (i = 0; i < 6; i++) {
			filein >> temps;
			grid.push_back(temps);
		}

	/*	for (i = 0; i < 6; i++) {
			for (j = 0; j < 10; j++) {
				cout << grid[i][j];
			}
			cout << endl;
		}*/
		def();
	/*	for (i = 0; i < 4; i++) {
			for (j = 0; j < 4; j++) {
				cout << piece[i][j];
			}
			cout << endl;
		}*/

		for (i = 0; i < 28; i++) {
			if (equal(piece, templates[i])) {
				pos = i;
				break;
			}
		}

		while (pos % 4 != 0)
			pos--;
		left = pos;
		for (i = left; i < left + 4; i++) {
			go(i);
		}

		fileout << ans << endl;
	}

	filein.close();
	fileout.close();
	return(0);
}
