Problem:
B: Hokkaido University Hard
注意
問題設定は A 問題と同一のものですが、制約のみが異なりますのでご注意ください。
物語
北海道大学に合格し、新たな生活の始まりに心を躍らせるほむらちゃん。しかし彼女の前には、とてつもなく広いキャンパスが待ち受けていた...。
「え...次の授業に間に合わないんだけど...」
問題
北海道大学札幌キャンパスは異常に広いことで有名です。札幌キャンパスは縦に
H
マス、横に
W
マス並んだ長方形のマス目で表現されます。北から
i
マス、西から
j
マス進んだマスを
(i,j)
で表すことにします。キャンパス内にはいくつか建物があり、マス
(i,j)
で表される位置に建物がある場合は 'B' 、ない場合は '.' が
c_{i,j}
に書かれています。
北海道大学の新入生のほむらちゃんは、キャンパスのあまりの広さに驚愕し、建物間の移動が心配になってきました。そこで、建物のある
2
つのマスのうち、最も遠い組はどれくらい離れているのかが気になりました。ここで、
2
つのマスの組
(i,j) , (i',j')
の間の距離を
|i-i'|+|j-j'|
と定義します。
ほむらちゃんは、自分にはこの問題は難しいと思い、同級生のあなたに助けを求めてきました。ほむらちゃんの代わりに答えを求めてください。
入力形式
H
W
c_{11}
c_{12}
...
c_{1W}
:
c_{H1}
c_{H2}
...
c_{HW}
制約
2 \leq H,W \leq 10^3
H,W
は整数
c_{i,j}
は 'B' か '.' のいずれかである。
c_{i,j}
のうち少なくとも
2
つは 'B' である。
出力形式
答えを表す整数を
1
行に出力せよ。
入力例 1
3 3
B.B
..B
.BB
出力例 1
4
(1,1)
と
(3,3)
の
2
点間が最長です。
入力例 2
4 3
B..
B..
...
...
出力例 2
1
隣り合う位置が最長になる場合もあります。
入力例 3
6 6
...B..
B.B.B.
.B.B.B
...B.B
.B..B.
..B...
出力例 3
7

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll =long long;
using vll=vector<ll>;
using vvll=vector<vll>;
#define rep(i,n) for(ll i=0;i<n;i++)

int main() {
    ll H,W;
    cin>>H>>W;
    vll L(H,1e18),R(H,-1);
    rep(h,H){
        string S;
        cin>>S;
        rep(w,W)if(S[w]=='B'){
            L[h]=min(L[h],w);
            R[h]=max(R[h],w);
        }
    }
    ll an=0;
    rep(a,H)rep(b,H){
        if(R[a]==-1||R[b]==-1)continue;
        an=max(an,R[a]-L[b]+abs(a-b));
    }
    cout<<an<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3436
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Point {
    int x;
    int y;

    Point(int x, int y) : x(x), y(y) {}
};

int dist(const Point& lhs, const Point& rhs) {
    return abs(lhs.x - rhs.x) + abs(lhs.y - rhs.y);
}

int main() {
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for (int i = 0; i < h; i++) cin >> s[i];
    vector<Point> points;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == 'B') {
                points.emplace_back(j, i);
            }
        }
    }
    sort(points.begin(), points.end(), 
            [](const Point& lhs, const Point& rhs) 
            { if (lhs.x == rhs.x) return lhs.y < rhs.y; else return lhs.x < rhs.x;});
    vector<Point> corner;
    corner.emplace_back(points.front());
    corner.emplace_back(points.back());
    sort(points.begin(), points.end(), 
            [](const Point& lhs, const Point& rhs) 
            { if (lhs.y == rhs.y) return lhs.x > rhs.x; else return lhs.y < rhs.y;});
    corner.emplace_back(points.front());
    corner.emplace_back(points.back());
    int ans = -1;
    for (auto p: corner) {
        for (auto q: points) {
            ans = max(ans, dist(p, q));
        }
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 80
memory_kb: 13792
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Point {
    int x;
    int y;

    Point(int x, int y) : x(x), y(y) {}
};

int dist(const Point& lhs, const Point& rhs) {
    return abs(lhs.x - rhs.x) + abs(lhs.y - rhs.y);
}

int main() {
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for (int i = 0; i < h; i++) cin >> s[i];
    vector<Point> points;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == 'B') {
                points.emplace_back(j, i);
            }
        }
    }
    sort(points.begin(), points.end(), 
            [](const Point& lhs, const Point& rhs) 
            { if (lhs.x == rhs.x) return lhs.y < rhs.y; else return lhs.x < rhs.x;});
    vector<Point> corner;
    corner.emplace_back(points.front());
    corner.emplace_back(points.back());
    sort(points.begin(), points.end(), 
            [](const Point& lhs, const Point& rhs) 
            { if (lhs.y == rhs.y) return lhs.x < rhs.x; else return lhs.y < rhs.y;});
    corner.emplace_back(points.front());
    corner.emplace_back(points.back());
    int ans = -1;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            ans = max(ans, dist(corner[i], corner[j]));
        }
    }
    cout << ans << '\n';
}
accuracy: 0.12903225806451613
time_ms: 20
memory_kb: 5464
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)

using namespace std;
using ll = long long;
using P = pair<int,int>;
const int mod = 998244353;
const int inf = (1<<30);

const int dy[4] = {-1, 0, 0, 1};
const int dx[4] = {0, -1, 1, 0};

int main(){
    int h,w; cin>>h>>w;
    vector<int> maxw(h,-1),minw(h,inf);

    rep(i,h){
        rep(j,w){
            char c; cin>>c;
            if(c == 'B'){
                maxw[i] = max(maxw[i],j);
                minw[i] = min(minw[i],j);
            }
        }
    }
    int ans = 0;
    rep(i,h){
        rep(i2,h){
            if(maxw[i] == -1 || maxw[i2] == -1) continue;

            ans = max(ans,max(abs(maxw[i]-minw[i2]),abs(maxw[i2]-minw[i])) + abs(i-i2));
        }
    }
    cout<<ans<<endl;
    
}
accuracy: 1
time_ms: 20
memory_kb: 3452
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
	int H, W; cin >> H >> W;
	vector G(H, vector(W, '_'));
	for (auto& g : G) for (auto& c : g) cin >> c;

	const pair<int, int> INF{ 10000000, -1 };

	vector<pair<int, int>> info(W, INF);
	for (int i = 0 ; i < W ; i++) {
			for (int j = 0 ; j < H ; j++) {
					if (G[j][i] == 'B') {
							info[i].first = min(info[i].first, j);
							info[i].second = max(info[i].second, j);
					}
			}
	}

	auto f = [](int y1, int x1, int y2, int x2) -> int {
			return abs(y1 - y2) + abs(x1 - x2);
	};

	int ans = 0;
	for (int i = 0 ; i < W ; i++) {
			if (info[i] == INF) continue;
			for (int j = 0 ; j < W ; j++) {
					if (info[j] == INF) continue;
					ans = max(ans, f(info[i].first, i, info[j].first, j));
					ans = max(ans, f(info[i].first, i, info[j].second, j));
					ans = max(ans, f(info[i].second, i, info[j].first, j));
					ans = max(ans, f(info[i].second, i, info[j].second, j));
			}
	}

	cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4148
final_rank: 8

code_snippet: #include<bits/stdc++.h>

using namespace std;

int func(){
  int h;
  int w;
  cin >> h >> w;
  vector<int> mulx{1,-1,1,-1};
  vector<int> muly{1,1,-1,-1};
  using P = pair<int,int>;
  vector<P> most_far(4);
  vector<int> most_dist(4,-1e9);
  vector<P> poss;
  for(int i=0;i<h;++i){
    for(int j=0;j<w;++j){
      char c;
      cin >> c;
      if(c == '.')continue;
      for(int k=0;k<4;++k){
        int p = mulx[k] * j + muly[k] * i;
        if(most_dist[k] < p){
          most_dist[k] = p;
          most_far[k] = P(i,j);
        }
      }
      poss.push_back(P(i,j));
    }
  }
  int res = -1e9;
  for(auto i:poss){
    for(auto j:most_far){
      res = max(res, abs(i.first-j.first) + abs(i.second - j.second));
    }
  }
  return res;
}



int main(){
  cout << func() << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 12856
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=9167167167167167167;
const int INF=10000000;

#define rep(i,a) for (int i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int n,m;
	cin>>n>>m;
	vector<int> p,q;
	rep(i,n) rep(j,m){
		char c;
		cin>>c;
		if(c=='B') p.push_back(i+j),q.push_back(i-j);
	}
	So(p),So(q);
	int S=(int)(p.size())-1;
	cout<<max(p[S]-p[0],q[S]-q[0])<<"\n";
}
accuracy: 1
time_ms: 30
memory_kb: 11216
final_rank: 14

code_snippet: #line 2 "/home/yuruhiya/programming/library/template/template.cpp"
#include <bits/stdc++.h>
#line 4 "/home/yuruhiya/programming/library/template/constants.cpp"
#include <string_view>
#line 7 "/home/yuruhiya/programming/library/template/constants.cpp"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rfor(i, m, n) for (int i = (m); i >= (n); --i)
#define unless(c) if (!(c))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)

using namespace std;
using ll = long long;
using LD = long double;
using VB = vector<bool>;
using VVB = vector<VB>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using VD = vector<LD>;
using PII = pair<int, int>;
using VP = vector<PII>;
using PLL = pair<ll, ll>;
using VPL = vector<PLL>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000000;
constexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;
constexpr long double PI = 3.14159265358979323846, EPS = 1e-12;
namespace CharacterClass {
	constexpr string_view
	    digit = "0123456789",
	    xdigit = "0123456789ABCDEFabcdef", lower = "abcdefghijklmnopqrstuvwxyz",
	    upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
	    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
	    alnum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
	    word = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz",
	    punct = R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)",
	    graph =
	        R"(!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~)",
	    print =
	        R"( !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~)",
	    blank = " \t", space = " \t\n\r\f\v";
}  // namespace CharacterClass
#line 7 "/home/yuruhiya/programming/library/template/Input.cpp"
using namespace std;

#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
class Scanner {
	static int gc() {
		return getchar_unlocked();
	}
	static char next_char() {
		char c;
		read(c);
		return c;
	}
	template <class T> static void read(T& v) {
		cin >> v;
	}
	static void read(char& v) {
		while (isspace(v = gc()))
			;
	}
	static void read(bool& v) {
		v = next_char() != '0';
	}
	static void read(string& v) {
		v.clear();
		for (char c = next_char(); !isspace(c); c = gc()) v += c;
	}
	static void read(int& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(long long& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(double& v) {
		v = 0;
		double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	static void read(long double& v) {
		v = 0;
		long double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	template <class T, class U> static void read(pair<T, U>& v) {
		read(v.first);
		read(v.second);
	}
	template <class T> static void read(vector<T>& v) {
		for (auto& e : v) read(e);
	}
	template <size_t N = 0, class T> static void read_tuple_impl(T& v) {
		if constexpr (N < tuple_size_v<T>) {
			read(get<N>(v));
			read_tuple_impl<N + 1>(v);
		}
	}
	template <class... T> static void read(tuple<T...>& v) {
		read_tuple_impl(v);
	}
	struct ReadVectorHelper {
		size_t n;
		ReadVectorHelper(size_t _n) : n(_n) {}
		template <class T> operator vector<T>() {
			vector<T> v(n);
			read(v);
			return v;
		}
	};
	struct Read2DVectorHelper {
		size_t n, m;
		Read2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}
		template <class T> operator vector<vector<T>>() {
			vector<vector<T>> v(n, vector<T>(m));
			read(v);
			return v;
		}
	};

public:
	string read_line() const {
		string v;
		for (char c = gc(); c != '\n' && c != '\0'; c = gc()) v += c;
		return v;
	}
	template <class T> T read() const {
		T v;
		read(v);
		return v;
	}
	template <class T> vector<T> read_vector(size_t n) const {
		vector<T> a(n);
		read(a);
		return a;
	}
	template <class T> operator T() const {
		return read<T>();
	}
	int operator--(int) const {
		return read<int>() - 1;
	}
	ReadVectorHelper operator[](size_t n) const {
		return ReadVectorHelper(n);
	}
	Read2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {
		return Read2DVectorHelper(nm);
	}
	void operator()() const {}
	template <class H, class... T> void operator()(H&& h, T&&... t) const {
		read(h);
		operator()(forward<T>(t)...);
	}

private:
	template <template <class...> class, class...> struct Multiple;
	template <template <class...> class V, class Head, class... Tail>
	struct Multiple<V, Head, Tail...> {
		template <class... Args> using vec = V<vector<Head>, Args...>;
		using type = typename Multiple<vec, Tail...>::type;
	};
	template <template <class...> class V> struct Multiple<V> { using type = V<>; };
	template <class... T> using multiple_t = typename Multiple<tuple, T...>::type;
	template <size_t N = 0, class T> void multiple_impl(T& t) const {
		if constexpr (N < tuple_size_v<T>) {
			auto& vec = get<N>(t);
			using V = typename remove_reference_t<decltype(vec)>::value_type;
			vec.push_back(read<V>());
			multiple_impl<N + 1>(t);
		}
	}

public:
	template <class... T> auto multiple(size_t h) const {
		multiple_t<T...> result;
		while (h--) multiple_impl(result);
		return result;
	}
} in;
#define inputs(T, ...) \
	T __VA_ARGS__;     \
	in(__VA_ARGS__)
#define ini(...) inputs(int, __VA_ARGS__)
#define inl(...) inputs(long long, __VA_ARGS__)
#define ins(...) inputs(string, __VA_ARGS__)
#line 7 "/home/yuruhiya/programming/library/template/Output.cpp"
#include <charconv>
#line 10 "/home/yuruhiya/programming/library/template/Output.cpp"
using namespace std;

struct BoolStr {
	const char *t, *f;
	BoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
	const char *d, *l;
	DivStr(const char* _d, const char* _l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"),
    no_endl(" ", "");
class Printer {
	BoolStr B{Yes};
	DivStr D{spc};

public:
	void print(int v) const {
		char buf[12]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void print(long long v) const {
		char buf[21]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void print(bool v) const {
		print(v ? B.t : B.f);
	}
	void print(vector<bool>::reference v) const {
		print(v ? B.t : B.f);
	}
	void print(char v) const {
		putchar_unlocked(v);
	}
	void print(const char* v) const {
		fwrite_unlocked(v, 1, strlen(v), stdout);
	}
	void print(double v) const {
		printf("%.20f", v);
	}
	void print(long double v) const {
		printf("%.20Lf", v);
	}
	template <class T> void print(const T& v) const {
		cout << v;
	}
	template <class T, class U> void print(const pair<T, U>& v) const {
		print(v.first);
		print(D.d);
		print(v.second);
	}
	template <class InputIterater>
	void print_range(const InputIterater& begin, const InputIterater& end) const {
		for (InputIterater i = begin; i != end; ++i) {
			if (i != begin) print(D.d);
			print(*i);
		}
	}
	template <class T> void print(const vector<T>& v) const {
		print_range(v.begin(), v.end());
	}
	template <class T, size_t N> void print(const array<T, N>& v) const {
		print_range(v.begin(), v.end());
	}
	template <class T> void print(const vector<vector<T>>& v) const {
		for (size_t i = 0; i < v.size(); ++i) {
			if (i) print(D.l);
			print(v[i]);
		}
	}

	Printer() = default;
	Printer(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}
	Printer& operator()() {
		print(D.l);
		return *this;
	}
	template <class H> Printer& operator()(H&& h) {
		print(h);
		print(D.l);
		return *this;
	}
	template <class H, class... T> Printer& operator()(H&& h, T&&... t) {
		print(h);
		print(D.d);
		return operator()(forward<T>(t)...);
	}
	template <class InputIterator>
	Printer& range(const InputIterator& begin, const InputIterator& end) {
		print_range(begin, end);
		print(D.l);
		return *this;
	}
	template <class T> Printer& range(const T& a) {
		range(a.begin(), a.end());
		return *this;
	}
	template <class... T> void exit(T&&... t) {
		operator()(forward<T>(t)...);
		std::exit(EXIT_SUCCESS);
	}
	Printer& flush() {
		fflush_unlocked(stdout);
		return *this;
	}
	Printer& set(const BoolStr& b) {
		B = b;
		return *this;
	}
	Printer& set(const DivStr& d) {
		D = d;
		return *this;
	}
	Printer& set(const char* t, const char* f) {
		B = BoolStr(t, f);
		return *this;
	}
} out;
#line 3 "/home/yuruhiya/programming/library/template/Step.cpp"
using namespace std;

template <class T> struct Step {
	using value_type = T;

	class iterator {
		value_type a, b, c;

	public:
		constexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}
		constexpr iterator(value_type _b, value_type _c, value_type _s)
		    : a(_b), b(_c), c(_s) {}
		constexpr iterator& operator++() {
			--b;
			a += c;
			return *this;
		}
		constexpr iterator operator++(int) {
			iterator tmp = *this;
			--b;
			a += c;
			return tmp;
		}
		constexpr const value_type& operator*() const {
			return a;
		}
		constexpr const value_type* operator->() const {
			return &a;
		}
		constexpr bool operator==(const iterator& i) const {
			return b == i.b;
		}
		constexpr bool operator!=(const iterator& i) const {
			return !(b == i.b);
		}
		constexpr value_type start() const {
			return a;
		}
		constexpr value_type size() const {
			return b;
		}
		constexpr value_type step() const {
			return c;
		}
	};
	constexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}
	constexpr iterator begin() const {
		return be;
	}
	constexpr iterator end() const {
		return en;
	}
	constexpr value_type start() const {
		return be.start();
	}
	constexpr value_type size() const {
		return be.size();
	}
	constexpr value_type step() const {
		return be.step();
	}
	constexpr value_type sum() const {
		return start() * size() + step() * (size() * (size() - 1) / 2);
	}
	operator vector<value_type>() const {
		return to_a();
	}
	auto to_a() const {
		vector<value_type> result;
		result.reserve(size());
		for (auto i : *this) {
			result.push_back(i);
		}
		return result;
	}

private:
	iterator be, en;
};
template <class T> constexpr auto step(T a) {
	return Step<T>(0, a, 1);
}
template <class T> constexpr auto step(T a, T b) {
	return Step<T>(a, b - a, 1);
}
template <class T> constexpr auto step(T a, T b, T c) {
	return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);
}
#line 8 "/home/yuruhiya/programming/library/template/Ruby.cpp"
using namespace std;

template <class F> struct Callable {
	F func;
	Callable(const F& f) : func(f) {}
};
template <class T, class F> auto operator|(const T& v, const Callable<F>& c) {
	return c.func(v);
}

struct Sort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {
		sort(begin(v), end(v));
		return v;
	}
} Sort;
struct SortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) < f(j); });
			return v;
		});
	}
} SortBy;
struct RSort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(rbegin(v), rend(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {
		sort(rbegin(v), rend(v));
		return v;
	}
} RSort;
struct RSortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) > f(j); });
			return v;
		});
	}
} RSortBy;
struct Reverse_impl {
	template <class T> friend auto operator|(T v, const Reverse_impl& c) {
		reverse(begin(v), end(v));
		return v;
	}
} Reverse;
struct Unique_impl {
	template <class T> friend auto operator|(T v, const Unique_impl& c) {
		v.erase(unique(begin(v), end(v), end(v)));
		return v;
	}
} Unique;
struct Uniq_impl {
	template <class T> friend auto operator|(T v, const Uniq_impl& c) {
		sort(begin(v), end(v));
		v.erase(unique(begin(v), end(v)), end(v));
		return v;
	}
} Uniq;
struct Rotate_impl {
	auto operator()(int&& left) {
		return Callable([&](auto v) {
			int s = static_cast<int>(size(v));
			assert(-s <= left && left <= s);
			if (0 <= left) {
				rotate(begin(v), begin(v) + left, end(v));
			} else {
				rotate(begin(v), end(v) + left, end(v));
			}
			return v;
		});
	}
} Rotate;
struct Max_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *max_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Max_impl& c) {
		return *max_element(begin(v), end(v));
	}
} Max;
struct Min_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *min_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Min_impl& c) {
		return *min_element(begin(v), end(v));
	}
} Min;
struct MaxPos_impl {
	template <class T> friend auto operator|(T v, const MaxPos_impl& c) {
		return max_element(begin(v), end(v)) - begin(v);
	}
} MaxPos;
struct MinPos_impl {
	template <class T> friend auto operator|(T v, const MinPos_impl& c) {
		return min_element(begin(v), end(v)) - begin(v);
	}
} MinPos;
struct MaxBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_it = begin(v);
			auto max_val = f(*max_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_it = it;
					max_val = val;
				}
			}
			return *max_it;
		});
	}
} MaxBy;
struct MinBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_it = begin(v);
			auto min_val = f(*min_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_it = it;
					min_val = val;
				}
			}
			return *min_it;
		});
	}
} MinBy;
struct MaxOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_val = val;
				}
			}
			return max_val;
		});
	}
} MaxOf;
struct MinOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_val = val;
				}
			}
			return min_val;
		});
	}
} MinOf;
struct Count_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return count(begin(v), end(v), val); });
	}
} Count;
struct CountIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return count_if(begin(v), end(v), f); });
	}
} CountIf;
struct Index_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find(begin(v), end(v), val);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} Index;
struct IndexIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} IndexIf;
struct FindIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<typename decltype(v)::value_type> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(*result) : nullopt;
		});
	}
} FindIf;
struct Sum_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			return accumulate(next(begin(v)), end(v), f(*begin(v)),
			                  [&](const auto& a, const auto& b) { return a + f(b); });
		});
	}
	template <class T> friend auto operator|(T v, const Sum_impl& c) {
		return accumulate(begin(v), end(v), typename T::value_type{});
	}
} Sum;
struct Includes {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });
	}
} Includes;
struct IncludesIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });
	}
} IncludesIf;
struct RemoveIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) {
			v.erase(remove_if(begin(v), end(v), f), end(v));
			return v;
		});
	}
} RemoveIf;
struct Each_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				f(i);
			}
		});
	}
} Each;
struct Select_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using value_type = typename decltype(v)::value_type;
			vector<value_type> result;
			for (const auto& i : v) {
				if (f(i)) result.push_back(i);
			}
			return result;
		});
	}
} Select;
struct Map_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using result_type = invoke_result_t<F, typename decltype(v)::value_type>;
			vector<result_type> result;
			result.reserve(size(v));
			for (const auto& i : v) {
				result.push_back(f(i));
			}
			return result;
		});
	}
} Map;
struct Indexed_impl {
	template <class T> friend auto operator|(const T& v, Indexed_impl& c) {
		using value_type = typename T::value_type;
		vector<pair<value_type, int>> result;
		result.reserve(size(v));
		int index = 0;
		for (const auto& i : v) {
			result.emplace_back(i, index++);
		}
		return result;
	}
} Indexed;
struct AllOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (!f(i)) return false;
			}
			return true;
		});
	}
} AllOf;
struct AnyOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return true;
			}
			return false;
		});
	}
} AnyOf;
struct NoneOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return false;
			}
			return true;
		});
	}
} NoneOf;

struct Tally_impl {
	template <class F> auto operator()(size_t max_val) {
		return Callable([&](auto v) {
			vector<size_t> result(max_val);
			for (const auto& i : v) {
				result[static_cast<size_t>(i)]++;
			}
			return result;
		});
	}
	template <class T, class value_type = typename T::value_type>
	friend auto operator|(const T& v, Tally_impl& c) {
		map<value_type, size_t> result;
		for (const auto& i : v) {
			result[i]++;
		}
		return result;
	}
} Tally;

template <class T> auto operator*(const vector<T>& a, size_t n) {
	T result;
	for (size_t i = 0; i < n; ++i) {
		result.insert(result.end(), a.begin(), a.end());
	}
	return result;
}
auto operator*(string a, size_t n) {
	string result;
	for (size_t i = 0; i < n; ++i) {
		result += a;
	}
	return result;
}
template <class T, class U> auto& operator<<(vector<T>& a, const U& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T> auto& operator<<(string& a, const T& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T, class U> auto operator+(vector<T> a, const U& b) {
	a << b;
	return a;
}
template <class T> auto operator+(string a, const T& b) {
	a << b;
	return a;
}
#line 7 "/home/yuruhiya/programming/library/template/functions.cpp"
using namespace std;

template <class T = long long> constexpr T TEN(size_t n) {
	T result = 1;
	for (size_t i = 0; i < n; ++i) result *= 10;
	return result;
}
template <class T, class U,
          enable_if_t<is_integral_v<T> && is_integral_v<U>, nullptr_t> = nullptr>
constexpr auto div_ceil(T n, U m) {
	return (n + m - 1) / m;
}
template <class T, class U> constexpr auto div_ceil2(T n, U m) {
	return div_ceil(n, m) * m;
}
template <class T> constexpr T triangle(T n) {
	return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
}
template <class T> constexpr T nC2(T n) {
	return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);
}
template <class T, class U> constexpr auto middle(const T& l, const U& r) {
	return l + (r - l) / 2;
}
template <class T, class U, class V>
constexpr bool in_range(const T& v, const U& lower, const V& upper) {
	return lower <= v && v < upper;
}
template <class T, enable_if_t<is_integral_v<T>, nullptr_t> = nullptr>
constexpr bool is_square(T n) {
	T s = sqrt(n);
	return s * s == n || (s + 1) * (s + 1) == n;
}
template <class T = long long> constexpr T BIT(int b) {
	return T(1) << b;
}
template <class T> constexpr int BIT(T x, int i) {
	return (x & (1 << i)) ? 1 : 0;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Pow(T a, U n) {
	assert(n >= 0);
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result *= a;
			n--;
		} else {
			a *= a;
			n >>= 1;
		}
	}
	return result;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Powmod(T a, U n, T mod) {
	assert(n >= 0);
	if (a > mod) a %= mod;
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result = result * a % mod;
			n--;
		} else {
			a = a * a % mod;
			n >>= 1;
		}
	}
	return result;
}
template <class T> bool chmax(T& a, const T& b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> bool chmin(T& a, const T& b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> int sz(const T& v) {
	return v.size();
}
template <class T, class U> int lower_index(const T& a, const U& v) {
	return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> int upper_index(const T& a, const U& v) {
	return upper_bound(all(a), v) - a.begin();
}
template <class T> auto Slice(const T& v, size_t i, size_t len) {
	return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();
}
template <class T, class U = typename T::value_type> U Gcdv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);
}
template <class T, class U = typename T::value_type> U Lcmv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);
}
namespace internal {
	template <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {
		if constexpr (N == 1) {
			return vector(sizes[0], init);
		} else {
			int size = sizes[N - 1];
			sizes.pop_back();
			return vector(size, make_vector<T, N - 1>(sizes, init));
		}
	}
}  // namespace internal
template <class T, size_t N>
auto make_vector(const int (&sizes)[N], const T& init = T()) {
	vector s(rbegin(sizes), rend(sizes));
	return internal::make_vector<T, N>(s, init);
}
#line 9 "/home/yuruhiya/programming/library/template/template.cpp"
#if __has_include(<library/dump.hpp>)
#include <library/dump.hpp>
#define LOCAL
#else
#define dump(...) ((void)0)
#endif

template <class T> constexpr T oj_local(const T& oj, const T& local) {
#ifndef LOCAL
	return oj;
#else
	return local;
#endif
}
#line 2 "a.cpp"

int main() {
	ini(h, w);
	VS s = in[h];
	VI a, b;
	rep(i, h) rep(j, w) {
		if (s[i][j] == 'B') {
			a.push_back(i + j);
			b.push_back(i - j);
		}
	}
	dump(a, b);
	out(max((a | Max) - (a | Min), (b | Max) - (b | Min)));
}
accuracy: 1
time_ms: 10
memory_kb: 20252
final_rank: 17

code_snippet: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define MOD 1000000007

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char> > c(h, vector<char>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> c[i][j];
    }
  }
  int xmax = 0, xmin = h + w, ymax = 0, ymin = h + w;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (c[i][j] == 'B') {
        xmax = max(xmax, i + j);
        xmin = min(xmin, i + j);
        ymax = max(ymax, i - j);
        ymin = min(ymin, i - j);
      }
    }
  }
  cout << max(xmax - xmin, ymax - ymin) << endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3560
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> fi(H);
    for (int i = 0; i < H; ++i) cin >> fi[i];
    int minS = H+W, maxS = 0, minD = H+W, maxD = -H-W;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            if (fi[i][j] != 'B') continue;
            minS = min(minS, i + j);
            maxS = max(maxS, i + j);
            minD = min(minD, i - j);
            maxD = max(maxD, i - j);
        }
    }
    cout << max(maxS - minS, maxD - minD) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4644
final_rank: 3

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
template <typename T> using posteriority_queue = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
int popcount(int val) { return __builtin_popcount(val); }
int popcountll(ll val) { return __builtin_popcountll(val); }
template <typename T> void unique(vector<T> &a) { a.erase(unique(ALL(a)), a.end()); }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
vector<vector<T> > rot(const vector<vector<T> > &grid, int angle, T basis = ' ') {
  int h = grid.size(), w = grid.front().size();
  vector<vector<T> > rotated_grid;
  if (angle == 45) {
    rotated_grid.assign(h + w - 1, vector<T>(h + w - 1, basis));
    REP(i, h) REP(j, w) rotated_grid[i + j][i - j + w - 1] = grid[i][j];
  } else if (angle == 90) {
    rotated_grid.assign(w, vector<T>(h));
    REP(i, h) REP(j, w) rotated_grid[w - 1 - j][i] = grid[i][j];
  } else {
    assert(false);
  }
  return rotated_grid;
}

int main() {
  int h, w; cin >> h >> w;
  vector<vector<char> > c(h, vector<char>(w)); REP(i, h) REP(j, w) cin >> c[i][j];
  c = rot(c, 45);
  h = c.size(); w = c.front().size();
  set<int> hor, ver;
  REP(i, h) REP(j, w) {
    if (c[i][j] == 'B') {
      hor.emplace(i);
      ver.emplace(j);
    }
  }
  int ans = 0;
  if (hor.size() >= 2) chmax(ans, *hor.rbegin() - *hor.begin());
  if (ver.size() >= 2) chmax(ans, *ver.rbegin() - *ver.begin());
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 6508
final_rank: 15

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 3010

char c[SIZE][SIZE];

set<pair<int,int>> ss;

int main(){
  int H, W;

  scanf("%d%d", &H, &W);

  for (int i=0; i<H; i++) {
    scanf("%s", c[i]);

    int l = INF, r = -1;
    for (int j=0; j<W; j++) {
      if (c[i][j] == 'B') {
        l = min(l, j);
        r = max(r, j);
      }
    }

    if (l != INF) {
      ss.insert({i, l});
      ss.insert({i, r});
    }
  }

  for (int j=0; j<W; j++) {
    int u = INF, d = -1;
    for (int i=0; i<H; i++) {
      if (c[i][j] == 'B') {
        u = min(u, i);
        d = max(d, i);
      }
    }
    if (u != INF) {
      ss.insert({u, j});
      ss.insert({u, j});
    }
  }

  int ans = 0;

  for (auto p1 : ss) {
    for (auto p2 : ss) {
      ans = max(ans, abs(p1.first-p2.first) + abs(p1.second-p2.second));
    }
  }

  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 6024
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  Int h,w;
  cin>>h>>w;
  vector<string> st(h);
  for(Int i=0;i<h;i++) cin>>st[i];

  vector<Int> us,vs;
  for(Int i=0;i<h;i++){
    for(Int j=0;j<w;j++){
      if(st[i][j]!='B') continue;
      us.emplace_back(i+j);
      vs.emplace_back(i-j);
    }
  }
  sort(us.begin(),us.end());
  sort(vs.begin(),vs.end());

  cout<<max(us.back()-us.front(),vs.back()-vs.front())<<endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 16036
final_rank: 18

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 2953.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 30;
const int MAX_W = 30;
const int MAX_N = MAX_H * MAX_W;

/* typedef */

template <typename T>
struct Pt {
  T x, y;

  Pt() {}
  Pt(T _x, T _y) : x(_x), y(_y) {}
  Pt(const Pt<T> &pt) : x(pt.x), y(pt.y) {}

  Pt<T> operator+(const Pt<T> pt) const { return Pt<T>(x + pt.x, y + pt.y); }
  Pt<T> operator-() const { return Pt<T>(-x, -y); }
  Pt<T> operator-(const Pt<T> pt) const { return Pt<T>(x - pt.x, y - pt.y); }
  Pt<T> operator*(T t) const { return Pt<T>(x * t, y * t); }
  Pt<T> operator/(T t) const { return Pt<T>(x / t, y / t); }
  T dot(Pt<T> v) const { return x * v.x + y * v.y; }
  T cross(Pt<T> v) const { return x * v.y - y * v.x; }

  bool operator==(const Pt<T> pt) const { return x == pt.x && y == pt.y; }
  bool operator<(const Pt<T> &pt) const {
    return x < pt.x || (x == pt.x && y < pt.y);
  }

  int md(const Pt<T> p) const { return abs(x - p.x) + abs(y - p.y); }

  void print() { printf("(%d,%d)", x, y); }
};

typedef Pt<int> pt;
typedef vector<pt> vpt;

/* global variables */

char s[MAX_W + 4];

/* subroutines */

// convex_hull()
//   make a convex_hull 'chs' from a set of points 'ps'
//   Note: ps must be sorted, and must contain at least 2 points
void convex_hull(const vpt& ps, vpt& chs) {
  int n = ps.size();
  vpt lhs, uhs;

  lhs.push_back(ps[0]);
  lhs.push_back(ps[1]);
  for (int i = 2; i < n; i++) {
    while (lhs.size() >= 2) {
      int ln = lhs.size();
      pt &lh0 = lhs[ln - 2], &lh1 = lhs[ln - 1];
      if ((lh1 - lh0).cross(ps[i] - lh1) > 0) break;
      lhs.pop_back();
    }
    lhs.push_back(ps[i]);
  }

  uhs.push_back(ps[n - 1]);
  uhs.push_back(ps[n - 2]);
  for (int i = n - 3; i >= 0; i--) {
    while (uhs.size() >= 2) {
      int un = uhs.size();
      pt &uh0 = uhs[un - 2], &uh1 = uhs[un - 1];
      if ((uh1 - uh0).cross(ps[i] - uh1) > 0) break;
      uhs.pop_back();
    }
    uhs.push_back(ps[i]);
  }

  lhs.pop_back();
  uhs.pop_back();

  chs.clear();
  chs.reserve(lhs.size() + uhs.size());
  chs.assign(lhs.begin(), lhs.end());
  chs.insert(chs.end(), uhs.begin(), uhs.end());
}

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);

  vpt ps;
  for (int y = 0; y < h; y++) {
    scanf("%s", s);
    for (int x = 0; x < w; x++)
      if (s[x] == 'B') ps.push_back(pt(x, y));
  }

  vpt chs;
  convex_hull(ps, chs);
  //printf("ps=%lu, chs=%lu\n", ps.size(), chs.size());

  int maxd = 0, n = chs.size();
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      int d = chs[i].md(chs[j]);
      if (maxd < d) maxd = d;
    }

  printf("%d\n", maxd);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 11172
final_rank: 10

code_snippet: #include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
using namespace std;

int main(){
	int H, W;
	cin >> H >> W;
	string   c[1000];
	int side[1000][2];
	for (int i = 0; i < H; i++) {
		side[i][0] = 99999;
		side[i][1] = -1;
			cin >> c[i];
			for (int j = 0; j < W; j++) {
				if (c[i][j] == 'B') {
					side[i][0] = min(side[i][0], j);
					side[i][1] = max(side[i][1], j);
				}
			}
	}
	int answer = 0;
	for (int i = 0; i < H; i++) {
		for (int k = 0; k < H; k++) {
			if (side[i][0] != 99999 && side[k][1] != -1) {
			answer = max(answer, abs(i - k) + abs(side[i][0] - side[k][1]));
		}
	}
	}
	cout << answer << endl;

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4044
final_rank: 7

code_snippet: #include <cassert>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
/*-------------------------------------------------*/
template <typename T>
vector<vector<T> > rot(const vector<vector<T> > &grid, int angle) {
  int h = grid.size(), w = grid.front().size();
  if (angle == 45) {
    vector<vector<T> > rotated_grid(h + w - 1, vector<char>(h + w - 1, ' '));
    REP(i, h) REP(j, w) rotated_grid[i + j][i - j + w - 1] = grid[i][j];
    return rotated_grid;
  } else if (angle == 90) {
    vector<vector<T> > rotated_grid(w, vector<T>(h));
    REP(i, h) REP(j, w) rotated_grid[w - 1 - j][i] = grid[i][j];
    return rotated_grid;
  } else {
    assert(false);
  }
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int h, w; cin >> h >> w;
  vector<vector<char> > c(h, vector<char>(w)); REP(i, h) REP(j, w) cin >> c[i][j];
  c = rot(c, 45);
  h = c.size(); w = c.front().size();
  set<int> yoko, tate;
  REP(i, h) REP(j, w) if (c[i][j] == 'B') {
    yoko.emplace(i);
    tate.emplace(j);
  }
  int ans = 0;
  if (yoko.size() >= 2) ans = max(ans, *yoko.rbegin() - *yoko.begin());
  if (tate.size() >= 2) ans = max(ans, *tate.rbegin() - *tate.begin());
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 6560
final_rank: 16

code_snippet: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


signed main(void) {
    int h,w;
    cin >> h >> w;
    vs s(h);
    rep(i,h)cin >> s[i];
    vp v;
    rep(i,h){
        int j = 0;
        while(j < w){
            if(s[i][j] == 'B'){
                v.push_back(pii(i,j));
                break;
            }
            j++;
        }
        j = w-1;
        while(j >= 0){
            if(s[i][j] == 'B'){
                v.push_back(pii(i,j));
                break;
            }
            j--;
        }
    }
    rep(j,w){
        int i = 0;
        while(i < h){
            if(s[i][j] == 'B'){
                v.push_back(pii(i,j));
                break;
            }
            i++;
        }
        i = h-1;
        while(i >= 0){
            if(s[i][j] == 'B'){
                v.push_back(pii(i,j));
                break;
            }
            i--;
        }
    }
    int ans = 0;
    rep(i,v.size())rep(j,i){
        ans = max(ans, abs(v[i].first-v[j].first)+abs(v[i].second-v[j].second));
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3972
final_rank: 6

code_snippet: #include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        cout << v[i] << " \n"[i == sz - 1];
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    vector<int> x, y;
    for (int i = 0; i < h; i++)
    {
        cin >> s[i];
        for (int j = 0; j < w; j++)
        {
            if (s[i][j] == 'B')
            {
                x.push_back(i + j);
                y.push_back(i - j);
            }
        }
    }
    int sz = x.size();
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    int ret = max(x[sz - 1] - x[0], y[sz - 1] - y[0]);
    cout << ret << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 9364
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 1005

struct LOC{
	LOC(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	bool operator<(const struct LOC &arg)const{

		if(row != arg.row){

			return row < arg.row;
		}else{
			return col < arg.col;
		}
	}
	bool operator==(const struct LOC &arg) const{

		return row == arg.row && col == arg.col;
	}
	int row,col;
};

int H,W;
int min_row[SIZE],max_row[SIZE],min_col[SIZE],max_col[SIZE];
char table[SIZE][SIZE];
vector<LOC> V;

int main(){

	scanf("%d %d",&H,&W);

	for(int row = 0; row < H; row++){

		min_col[row] = BIG_NUM;
		max_col[row] = -BIG_NUM;
	}

	for(int col = 0; col < W; col++){

		min_row[col] = BIG_NUM;
		max_row[col] = -BIG_NUM;
	}

	for(int row = 0; row < H; row++){

		scanf("%s",table[row]);

		for(int col = 0; col < W; col++){

			if(table[row][col] == 'B'){

				min_col[row] = min(min_col[row],col);
				max_col[row] = max(max_col[row],col);

				min_row[col] = min(min_row[col],row);
				max_row[col] = max(max_row[col],row);
			}
		}
	}

	for(int row = 0; row < H; row++){

		if(abs(min_col[row]) != BIG_NUM){

			V.push_back(LOC(row,min_col[row]));
		}
		if(abs(max_col[row]) != BIG_NUM){

			V.push_back(LOC(row,max_col[row]));
		}
	}

	for(int col = 0; col < W; col++){

		if(abs(min_row[col]) != BIG_NUM){

			V.push_back(LOC(min_row[col],col));
		}
		if(abs(max_row[col]) != BIG_NUM){

			V.push_back(LOC(max_row[col],col));
		}
	}

	sort(V.begin(),V.end());
	V.erase(unique(V.begin(),V.end()),V.end());

	int ans = 0;

	for(int i = 0; i < V.size()-1; i++){
		for(int k = i+1; k < V.size(); k++){

			ans = max(ans,abs(V[i].row-V[k].row)+abs(V[i].col-V[k].col));
		}
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4196
final_rank: 2

code_snippet: // Undone
#include <bits/stdc++.h>
using namespace std;
int H, W; 
int main() {
  cin >> H >> W;
  vector< pair<int, int> > v;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == 'B') {
        v.push_back(make_pair(i, j));
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < v.size(); i++) {
    int dist_max;
    if (v[i].first < H/2) {
      if (v[i].second < W/2) {
        dist_max = (H-v[i].first-1)+(W-v[i].second-1);
      } else {
        dist_max = (H-v[i].first-1)+v[i].second;
      }
    } else {
      if (v[i].second < W/2) {
        dist_max = v[i].first + (W-v[i].second-1);
      } else {
        dist_max = v[i].first+v[i].second;
      }
    }

    if (dist_max < ans) continue;

    for (int j = 0; j < v.size(); j++) {
      if (i == j) continue;
      if (dist_max < ans) break;
      ans = max(ans, abs(v[i].first-v[j].first)+abs(v[i].second-v[j].second));
    }
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 11080
final_rank: 13