This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=4021"
#include "../Utility/template.hpp"
#include "../Datastructure/lazysegtree.hpp"
struct S{
ll idx;
ll x;
};
S op(S a,S b){
if(a.x==b.x){
if(a.idx>b.idx) return a;
return b;
}
if(a.x>b.x) return a;
return b;
}
S e(){
return {-1,(ll)(-1e18)};
}
using F = ll;
S mapping(F f,S a){
a.x+=f;
return a;
}
F comb(F f,F g){
return f+g;
}
F id(){
return 0;
}
using segtree = lazysegtree<S,op,e,F,mapping,comb,id>;
int main(){
int h,w;
cin>>h>>w;
vector<string> s(h);
rep(i,0, h) cin>>s.at(i);
vector<vector<ll>> dp(h+1,vector<ll>(w+1,0));
{
vector<vector<ll>> dp1(h+1,vector<ll>(w+1,0));
vector<vector<ll>> dp2(h+1,vector<ll>(w+1,0));
for(int i=h-1;i>=0;i--){
for(int j=w-1;j>=0;j--){
if(s.at(i).at(j)=='0') continue;
dp1.at(i).at(j)=dp1.at(i+1).at(j)+1;
dp2.at(i).at(j)=dp2.at(i).at(j+1)+1;
dp.at(i).at(j)=min(dp1.at(i).at(j),dp2.at(i).at(j));
}
}
}
vector<string> rs(h);
rep(i,0, h) rep(j,0, w){
rs.at(i)+=s.at(h-1-i).at(w-1-j);
}
vector<vector<ll>> rdp(h+1,vector<ll>(w+1,0));
{
vector<vector<ll>> rrdp(h+1,vector<ll>(w+1,0));
vector<vector<ll>> dp1(h+1,vector<ll>(w+1,0));
vector<vector<ll>> dp2(h+1,vector<ll>(w+1,0));
for(int i=h-1;i>=0;i--){
for(int j=w-1;j>=0;j--){
if(rs.at(i).at(j)=='0') continue;
dp1.at(i).at(j)=dp1.at(i+1).at(j)+1;
dp2.at(i).at(j)=dp2.at(i).at(j+1)+1;
rrdp.at(i).at(j)=min(dp1.at(i).at(j),dp2.at(i).at(j));
}
}
rep(i,0, h) rep(j,0, w){
rdp.at(i).at(j)=rrdp.at(h-1-i).at(w-1-j);
}
}
ll ans=0;
for(int k=-h+1;k<w;k++){
vector<S> tmp;
int d=0;
for(int i=0;i<h;i++){
int j=i+k;
if(j<0||j>=w) continue;
tmp.push_back({d,rdp.at(i).at(j)-d});
d++;
}
segtree seg(tmp);
d=0;
for(int i=0;i<h;i++){
int j=i+k;
if(j<0||j>=w) continue;
//if(dp.at(i).at(j)==0) continue;
auto ch = [&](S now) {
return now.x < 0;
};
ll v =seg.min_left(dp.at(i).at(j)+d,ch);
//cout << dp.at(i).at(j) << endl;
if(v > 0){
v--;
ans=max(ans,v-d+1);
}
d++;
seg.apply(0,tmp.size(),1);
}
}
cout<<ans<<endl;
}
#line 1 "verify/Datastructure_lazyseg_bina.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=4021"
#line 1 "Utility/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define TT template <typename T>
TT using vec = vector<T>;
template <class T1, class T2> bool chmin(T1 &x, T2 y) {
return x > y ? (x = y, true) : false;
}
template <class T1, class T2> bool chmax(T1 &x, T2 y) {
return x < y ? (x = y, true) : false;
}
struct io_setup {
io_setup() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(15);
}
} io_setup;
/*
@brief verify用テンプレート
*/
#line 1 "Datastructure/lazysegtree.hpp"
template <class S,
S (*op)(S, S),
S (*e)(),
class F,
S (*mp)(F, S),
F (*cm)(F, F),
F (*id)()>
struct lazysegtree {
int n;
int sz;
int log;
vec<S> d;
vec<F> lz;
lazysegtree(int n) : lazysegtree(vec<S>(n, e())) {}
lazysegtree(const vec<S> &v) : n((int)(v.size())) {
log = 1;
while ((1 << log) < n) log++;
sz = 1 << log;
d.resize(2 * sz, e());
lz.resize(2 * sz, id());
rep(i, 0, n) d[sz + i] = v[i];
rrep(i, 1, sz) update(i);
}
void update(int i) { d[i] = op(d[i << 1], d[i << 1 | 1]); }
void all_apply(int i, F f) {
d[i] = mp(f, d[i]);
if (i < sz) lz[i] = cm(f, lz[i]);
}
void push(int k) {
all_apply(k * 2, lz[k]);
all_apply(k * 2 + 1, lz[k]);
lz[k] = id();
}
S prod(int l, int r) {
assert(0 <= l && l <= r && r <= n);
if (l == r) return e();
l += sz;
r += sz;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push((r - 1) >> i);
}
S sml = e(), smr = e();
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
void apply(int l, int r, F f) {
assert(0 <= l && l <= r && r <= n);
if (l == r) return;
l += sz;
r += sz;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push((r - 1) >> i);
}
{
int l2 = l, r2 = r;
while (l < r) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
l >>= 1;
r >>= 1;
}
l = l2;
r = r2;
}
for (int i = 1; i <= log; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
// 以下、必要になったら書く(全て独立)
template <class G> int max_right(int l, G g) {
assert(0 <= l && l <= n);
assert(g(e()));
if (l == n) return n;
l += sz;
for (int i = log; i >= 1; i--) push(l >> i);
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!g(op(sm, d[l]))) {
while (l < sz) {
push(l);
l = (2 * l);
if (g(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - sz;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return n;
}
template <class G> int min_left(int r, G g) {
assert(0 <= r && r <= n);
assert(g(e()));
if (r == 0) return 0;
r += sz;
for (int i = log; i >= 1; i--) push((r - 1) >> i);
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!g(op(d[r], sm))) {
while (r < sz) {
push(r);
r = (2 * r + 1);
if (g(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - sz;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
void set(int p, S x) {
assert(0 <= p && p < n);
p += sz;
rrep(i, 1, log + 1) push(p >> i);
d[p] = x;
rep(i, 1, log + 1) update(p >> i);
}
S all_prod() { return d[1]; }
S get(int p) {
assert(0 <= p && p < n);
p += sz;
for (int i = log; i >= 1; i--) push(p >> i);
return d[p];
}
void apply(int p, F f) {
assert(0 <= p && p < n);
p += sz;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = mapping(f, d[p]);
for (int i = 1; i <= log; i++) update(p >> i);
}
};
/*
@brief lazysegtree
@docs doc/lazysegtree.md
*/
#line 5 "verify/Datastructure_lazyseg_bina.test.cpp"
struct S{
ll idx;
ll x;
};
S op(S a,S b){
if(a.x==b.x){
if(a.idx>b.idx) return a;
return b;
}
if(a.x>b.x) return a;
return b;
}
S e(){
return {-1,(ll)(-1e18)};
}
using F = ll;
S mapping(F f,S a){
a.x+=f;
return a;
}
F comb(F f,F g){
return f+g;
}
F id(){
return 0;
}
using segtree = lazysegtree<S,op,e,F,mapping,comb,id>;
int main(){
int h,w;
cin>>h>>w;
vector<string> s(h);
rep(i,0, h) cin>>s.at(i);
vector<vector<ll>> dp(h+1,vector<ll>(w+1,0));
{
vector<vector<ll>> dp1(h+1,vector<ll>(w+1,0));
vector<vector<ll>> dp2(h+1,vector<ll>(w+1,0));
for(int i=h-1;i>=0;i--){
for(int j=w-1;j>=0;j--){
if(s.at(i).at(j)=='0') continue;
dp1.at(i).at(j)=dp1.at(i+1).at(j)+1;
dp2.at(i).at(j)=dp2.at(i).at(j+1)+1;
dp.at(i).at(j)=min(dp1.at(i).at(j),dp2.at(i).at(j));
}
}
}
vector<string> rs(h);
rep(i,0, h) rep(j,0, w){
rs.at(i)+=s.at(h-1-i).at(w-1-j);
}
vector<vector<ll>> rdp(h+1,vector<ll>(w+1,0));
{
vector<vector<ll>> rrdp(h+1,vector<ll>(w+1,0));
vector<vector<ll>> dp1(h+1,vector<ll>(w+1,0));
vector<vector<ll>> dp2(h+1,vector<ll>(w+1,0));
for(int i=h-1;i>=0;i--){
for(int j=w-1;j>=0;j--){
if(rs.at(i).at(j)=='0') continue;
dp1.at(i).at(j)=dp1.at(i+1).at(j)+1;
dp2.at(i).at(j)=dp2.at(i).at(j+1)+1;
rrdp.at(i).at(j)=min(dp1.at(i).at(j),dp2.at(i).at(j));
}
}
rep(i,0, h) rep(j,0, w){
rdp.at(i).at(j)=rrdp.at(h-1-i).at(w-1-j);
}
}
ll ans=0;
for(int k=-h+1;k<w;k++){
vector<S> tmp;
int d=0;
for(int i=0;i<h;i++){
int j=i+k;
if(j<0||j>=w) continue;
tmp.push_back({d,rdp.at(i).at(j)-d});
d++;
}
segtree seg(tmp);
d=0;
for(int i=0;i<h;i++){
int j=i+k;
if(j<0||j>=w) continue;
//if(dp.at(i).at(j)==0) continue;
auto ch = [&](S now) {
return now.x < 0;
};
ll v =seg.min_left(dp.at(i).at(j)+d,ch);
//cout << dp.at(i).at(j) << endl;
if(v > 0){
v--;
ans=max(ans,v-d+1);
}
d++;
seg.apply(0,tmp.size(),1);
}
}
cout<<ans<<endl;
}