#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename Vert=int, typename Flow=int, int NV=2048>
class MaxFlow
{
	vector<int> G[NV];
	Flow F[NV][NV];

public:
	void addEdge( Vert s, Vert t, Flow f )
	{
		G[s].push_back(t);
		G[t].push_back(s);
		F[s][t] = f;
		F[t][s] = 0;
	}

	Flow calc( Vert S, Vert D )
	{
		for( Flow total=0 ;; ) {
			// Do BFS and compute the level for each node.
			int LV[NV] = {0};
			vector<int> Q(1, S);
			for(int lv=1; !Q.empty(); ++lv) {
				vector<int> Q2;
				for(size_t i=0; i!=Q.size(); ++i) {
					const vector<int>& ne = G[Q[i]];
					for(size_t j=0; j!=ne.size(); ++j)
						if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
							LV[ne[j]]=lv, Q2.push_back(ne[j]);
				}
				Q.swap(Q2);
			}

			// Destination is now unreachable. Done.
			if( !LV[D] )
				return total;

			// Iterating DFS.
			bool blocked[NV] = {};
			total += dinic_dfs( S, D, LV, 0x7fffffff, blocked );
		}
	}

private:
	Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] )
	{
		Flow flow_out = 0;
		for(size_t i=0; i!=G[v].size(); ++i) {
			int u = G[v][i];
			if( LV[v]+1==LV[u] && F[v][u] ) {
				Flow f = min(flow_in-flow_out, F[v][u]);
				if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) {
					F[v][u]  -= f;
					F[u][v]  += f;
					flow_out += f;
					if( flow_in == flow_out ) return flow_out;
				}
			}
		}
		blocked[v] = (flow_out==0);
		return flow_out;
	}
};

class BlockTheBlockPuzzle { public:
	int minimumHoles(vector <string> board)
	{
		const int H = board.size();
		const int W = board[0].size();
		const int INF = 2500;
		for(int gy=0; gy<H; ++gy)
		for(int gx=0; gx<W; ++gx) if(board[gy][gx] == '$')
		{
			map<pair<int,int>, int> id;
			for(int y=gy%3; y<H; y+=3)
			for(int x=gx%3; x<W; x+=3)
			{
				int new_id = id.size();
				id[make_pair(y,x)] = new_id;
			}

			int N = id.size();
			MaxFlow<>* mf = new MaxFlow<>;
			enum {IN, OUT};

			int Goal;
			vector<int> Start;
			for(int y=gy%3; y<H; y+=3)
			for(int x=gx%3; x<W; x+=3)if(board[y][x]!='H')
			{
				int v1 = id[make_pair(y,x)];
				if(board[y][x]=='$')
					Goal = v1;
				if(board[y][x]=='b')
					Start.push_back(v1);
				
				int e = (board[y][x]=='.' ? 1 : INF);
				mf->addEdge(v1*2+IN, v1*2+OUT, e);
				//cerr << v1 << " -- " << e << " -- " << v1 << endl;

				const int dy[] = {+1,0};
				const int dx[] = {0,+1};
				for(int d=0; d<2; ++d)
				{
					int Y = y+dy[d]*3;
					int X = x+dx[d]*3;
					if(0<=Y&&Y<H&&0<=X&&X<W&&board[Y][X]!='H')
					{
						int v2 = id[make_pair(Y,X)];

						int y1=y+dy[d]*1;
						int x1=x+dx[d]*1;
						int y2=y+dy[d]*2;
						int x2=x+dx[d]*2;
						int e = INF;
						if(board[y1][x1]!='b'&&board[y2][x2]!='b')
							e = (board[y1][x1]=='.')+(board[y2][x2]=='.');
						if(e) {
							mf->addEdge(v1*2+OUT, v2*2+IN, e);
							mf->addEdge(v2*2+OUT, v1*2+IN, e);
							//cerr << v1 << " -- " << e << " -- " << v2 << endl;
						}
					}
				}
			}

			for(int v: Start) {
				//cerr << "S: " << v << endl;
				mf->addEdge(v*2+OUT, N*2+IN, INF);
			}
			//cerr << "G: " << Goal << endl;
			int ans = mf->calc(Goal*2+OUT, N*2+IN);
			delete mf;
			return ans>=INF ? -1 : ans;
		}
		return -1;
	}
};

// 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(_, BlockTheBlockPuzzle().minimumHoles(board));}
int main(){

CASE(0)
	string board_[] = {"b..$",
 "....",
 "HHHH",
 "HHHH"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 2; 
END
CASE(1)
	string board_[] = {"............H..",
 "...............",
 "...............",
 "HHH$HHH.....H..",
 "HHHHHHH........",
 "HHHHHHHH.......",
 "......b..H.....",
 "...............",
 "...............",
 "...H..H..H.....",
 "...............",
 "...............",
 "...............",
 "...............",
 "..............."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 0; 
END
CASE(2)
	string board_[] = {"............H..",
 "...............",
 "...............",
 "HHH$HHH........",
 "HHHHHHH........",
 "HHHHHHHH.......",
 "......b..H.....",
 "...............",
 "...............",
 "...H..H..H.....",
 "...............",
 "...............",
 "...............",
 "...............",
 "..............."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 1; 
END
CASE(3)
	string board_[] = {"b..$...",
 "...H...",
 ".......",
 "b..b..b",
 "...H...",
 ".......",
 "b..b..b"}

;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 4; 
END
CASE(4)
	string board_[] = {"b..b..b",
 "..b..b.",
 ".......",
 "b..$bbb",
 ".b.....",
 "....b..",
 "b..b..b"}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = -1; 
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
