#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class ColorfulBoard { public:
	int theMin(vector <string> board)
	{
		int H = board.size();
		int W = board[0].size();

		vector<bool> okY(H), okX(W);
		for(bool changed=true; changed; )
		{
			changed = false;
			for(int y=0; y<H; ++y) if(!okY[y])
			{
				set<char> cc(board[y].begin(), board[y].end());
				cc.erase('*');
				if( cc.size() == 1 ) {
					okY[y] = changed = true;
					for(int x=0; x<W; ++x)
						board[y][x] = '*';
				}
			}
			for(int x=0; x<W; ++x) if(!okX[x])
			{
				set<char> cc;
				for(int y=0; y<H; ++y)
					cc.insert(board[y][x]);
				cc.erase('*');
				if( cc.size() == 1 ) {
					okX[x] = changed = true;
					for(int y=0; y<H; ++y)
						board[y][x] = '*';
				}
			}
		}

		int okys = 0;
		for(int y=0; y<H; ++y)
			if( okY[y] )
				okys++;
		int okxs = 0;
		for(int x=0; x<W; ++x)
			if( okX[x] )
				okxs++;
		if( okxs!=W && okys!=H )
			return -1;
		if( okxs!=W )
			return okys;
		if( okys!=H )
			return okxs;
		return min(okxs, okys);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, ColorfulBoard().theMin(board));}
int main(){

CASE(0)
	string board_[] = {"SSS",
 "SRR",
 "SMM"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 4; 
END
CASE(1)
	string board_[] = {"BBBBBBBB",
 "BBBBBBBB",
 "BBBBBBBB",
 "BBBBBBBB",
 "BBBBBBBB"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 5; 
END
CASE(2)
	string board_[] = {"Ab",
 "bA"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = -1; 
END
CASE(3)
	string board_[] = {"iiiii",
 "iwiwi"}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 4; 
END
CASE(4)
	string board_[] = {"ffffffffff",
 "xfxxoofoxo",
 "ffffffffff",
 "xfxxoofoxo",
 "ffffffffff",
 "ooxxoofoxo",
 "xfxxoofoxo",
 "xfxxoxfxxo",
 "ffxxofffxo",
 "xfxxoxfxxo"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 17; 
END
/*
CASE(5)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = ; 
END
CASE(6)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
